From 055f581ca58b62132c1eff23453031d9df9dbf0e Mon Sep 17 00:00:00 2001 From: diondiondion Date: Fri, 31 Oct 2025 15:20:59 +0100 Subject: [PATCH] Fix initially selected language in Rules panel, hide selector when no alternative translations exist (#36672) --- .../features/about/components/rules.tsx | 68 +++++++++++++------ 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/app/javascript/mastodon/features/about/components/rules.tsx b/app/javascript/mastodon/features/about/components/rules.tsx index e413adb310..078fb68c08 100644 --- a/app/javascript/mastodon/features/about/components/rules.tsx +++ b/app/javascript/mastodon/features/about/components/rules.tsx @@ -32,16 +32,38 @@ interface Rule extends BaseRule { translations?: Record; } +function getDefaultSelectedLocale( + currentUiLocale: string, + localeOptions: SelectItem[], +) { + const preciseMatch = localeOptions.find( + (option) => option.value === currentUiLocale, + ); + if (preciseMatch) { + return preciseMatch.value; + } + + const partialLocale = currentUiLocale.split('-')[0]; + const partialMatch = localeOptions.find( + (option) => option.value.split('-')[0] === partialLocale, + ); + + return partialMatch?.value ?? 'default'; +} + export const RulesSection: FC = ({ isLoading = false }) => { const intl = useIntl(); - const [locale, setLocale] = useState(intl.locale); - const rules = useAppSelector((state) => rulesSelector(state, locale)); const localeOptions = useAppSelector((state) => localeOptionsSelector(state, intl), ); + const [selectedLocale, setSelectedLocale] = useState(() => + getDefaultSelectedLocale(intl.locale, localeOptions), + ); + const rules = useAppSelector((state) => rulesSelector(state, selectedLocale)); + const handleLocaleChange: ChangeEventHandler = useCallback( (e) => { - setLocale(e.currentTarget.value); + setSelectedLocale(e.currentTarget.value); }, [], ); @@ -74,25 +96,27 @@ export const RulesSection: FC = ({ isLoading = false }) => { ))} -
- - -
+ {localeOptions.length > 1 && ( +
+ + +
+ )} ); };