Fix slow re-renders when toggling content languages (#10128)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -34,6 +34,23 @@ export function LanguageSettingsScreen({}: Props) {
|
|||||||
const langPrefs = useLanguagePrefs()
|
const langPrefs = useLanguagePrefs()
|
||||||
const setLangPrefs = useLanguagePrefsApi()
|
const setLangPrefs = useLanguagePrefsApi()
|
||||||
|
|
||||||
|
// changing langPrefs causes a slow re-render, so we use a local state copy
|
||||||
|
// and update that first to drive the UI on this screen to keep it snappy
|
||||||
|
const [contentLanguages, _setContentLanguages] = useState(
|
||||||
|
langPrefs.contentLanguages,
|
||||||
|
)
|
||||||
|
const setContentLanguages = useCallback(
|
||||||
|
(languages: string[]) => {
|
||||||
|
_setContentLanguages(languages)
|
||||||
|
// TODO: try using startTransition/useOptimistic when we switch to New Arch
|
||||||
|
// Old arch doesn't support concurrent react features so use rAF instead
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
setLangPrefs.setContentLanguages(languages)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
[setLangPrefs],
|
||||||
|
)
|
||||||
|
|
||||||
const contentLanguagePrefsControl = useDialogControl()
|
const contentLanguagePrefsControl = useDialogControl()
|
||||||
|
|
||||||
const onChangePrimaryLanguage = useCallback(
|
const onChangePrimaryLanguage = useCallback(
|
||||||
@@ -64,13 +81,13 @@ export function LanguageSettingsScreen({}: Props) {
|
|||||||
return [
|
return [
|
||||||
...new Set([
|
...new Set([
|
||||||
...recentLanguages,
|
...recentLanguages,
|
||||||
...langPrefs.contentLanguages,
|
...contentLanguages,
|
||||||
...langPrefs.primaryLanguage,
|
...langPrefs.primaryLanguage,
|
||||||
]),
|
]),
|
||||||
]
|
]
|
||||||
.map(lang => LANGUAGES.find(l => l.code2 === lang))
|
.map(lang => LANGUAGES.find(l => l.code2 === lang))
|
||||||
.filter(x => !!x)
|
.filter(x => !!x)
|
||||||
}, [recentLanguages, langPrefs.contentLanguages, langPrefs.primaryLanguage])
|
}, [recentLanguages, contentLanguages, langPrefs.primaryLanguage])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout.Screen testID="PreferencesLanguagesScreen">
|
<Layout.Screen testID="PreferencesLanguagesScreen">
|
||||||
@@ -167,7 +184,7 @@ export function LanguageSettingsScreen({}: Props) {
|
|||||||
</Trans>
|
</Trans>
|
||||||
</Text>
|
</Text>
|
||||||
|
|
||||||
{langPrefs.contentLanguages.length === 0 && (
|
{contentLanguages.length === 0 && (
|
||||||
<Admonition type="info">
|
<Admonition type="info">
|
||||||
<Trans>All languages will be shown in your feeds.</Trans>
|
<Trans>All languages will be shown in your feeds.</Trans>
|
||||||
</Admonition>
|
</Admonition>
|
||||||
@@ -176,8 +193,8 @@ export function LanguageSettingsScreen({}: Props) {
|
|||||||
<View style={[a.w_full, web({maxWidth: 400})]}>
|
<View style={[a.w_full, web({maxWidth: 400})]}>
|
||||||
<Toggle.Group
|
<Toggle.Group
|
||||||
label={_(msg`Select content languages`)}
|
label={_(msg`Select content languages`)}
|
||||||
values={langPrefs.contentLanguages}
|
values={contentLanguages}
|
||||||
onChange={setLangPrefs.setContentLanguages}>
|
onChange={setContentLanguages}>
|
||||||
<Toggle.PanelGroup>
|
<Toggle.PanelGroup>
|
||||||
{possibleLanguages.map((language, index) => {
|
{possibleLanguages.map((language, index) => {
|
||||||
const name = languageName(language, langPrefs.appLanguage)
|
const name = languageName(language, langPrefs.appLanguage)
|
||||||
@@ -220,9 +237,9 @@ export function LanguageSettingsScreen({}: Props) {
|
|||||||
feeds.
|
feeds.
|
||||||
</Trans>
|
</Trans>
|
||||||
}
|
}
|
||||||
currentLanguages={langPrefs.contentLanguages}
|
currentLanguages={contentLanguages}
|
||||||
onSelectLanguages={languages => {
|
onSelectLanguages={languages => {
|
||||||
setLangPrefs.setContentLanguages(languages)
|
setContentLanguages(languages)
|
||||||
setRecentLanguages(recent => [
|
setRecentLanguages(recent => [
|
||||||
...new Set([...recent, ...languages]),
|
...new Set([...recent, ...languages]),
|
||||||
])
|
])
|
||||||
|
|||||||
Reference in New Issue
Block a user