From 1857362d0357dd708bda837624c60e5b4449e847 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 17 Jan 2024 22:16:12 +0800 Subject: [PATCH] fix: locale and appearance setting --- web/src/App.tsx | 18 ++++++++++++++---- .../components/Settings/PreferencesSection.tsx | 5 ++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/web/src/App.tsx b/web/src/App.tsx index 48087748..991dbb61 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -16,6 +16,7 @@ const App = () => { const userStore = useUserStore(); const [loading, setLoading] = useState(true); const { appearance, locale, systemStatus } = globalStore.state; + const userSetting = userStore.userSetting; // Redirect to sign up page if no host. useEffect(() => { @@ -81,9 +82,18 @@ const App = () => { link.href = systemStatus.customizedProfile.logoUrl || "/logo.webp"; }, [systemStatus.customizedProfile]); + useEffect(() => { + if (!userSetting) { + return; + } + + globalStore.setLocale(userSetting.locale); + globalStore.setAppearance(userSetting.appearance as Appearance); + }, [userSetting?.locale, userSetting?.appearance]); + useEffect(() => { const { locale: storageLocale } = storage.get(["locale"]); - const currentLocale = userStore.userSetting?.locale || storageLocale || locale; + const currentLocale = storageLocale || locale; i18n.changeLanguage(currentLocale); document.documentElement.setAttribute("lang", currentLocale); if (currentLocale === "ar") { @@ -94,11 +104,11 @@ const App = () => { storage.set({ locale: currentLocale, }); - }, [locale, userStore.userSetting?.locale]); + }, [locale]); useEffect(() => { const { appearance: storageAppearance } = storage.get(["appearance"]); - let currentAppearance = (userStore.userSetting?.appearance || storageAppearance || appearance) as Appearance; + let currentAppearance = (storageAppearance || appearance) as Appearance; if (currentAppearance === "system") { currentAppearance = getSystemColorScheme(); } @@ -106,7 +116,7 @@ const App = () => { storage.set({ appearance: currentAppearance, }); - }, [appearance, userStore.userSetting?.appearance]); + }, [appearance]); useEffect(() => { const root = document.documentElement; diff --git a/web/src/components/Settings/PreferencesSection.tsx b/web/src/components/Settings/PreferencesSection.tsx index 721d3f0f..34210c42 100644 --- a/web/src/components/Settings/PreferencesSection.tsx +++ b/web/src/components/Settings/PreferencesSection.tsx @@ -18,7 +18,6 @@ const PreferencesSection = () => { const t = useTranslate(); const globalStore = useGlobalStore(); const userStore = useUserStore(); - const { appearance, locale } = globalStore.state; const setting = userStore.userSetting as UserSetting; const [telegramUserId, setTelegramUserId] = useState(setting.telegramUserId); @@ -75,11 +74,11 @@ const PreferencesSection = () => {

{t("common.basic")}

{t("common.language")} - +
{t("setting.preference-section.theme")} - +

{t("setting.preference")}