Skip to content

Commit

Permalink
🐛 fix: fix auto lang switch (#1305)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Feb 15, 2024
1 parent c67c25f commit 7a51329
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 30 deletions.
9 changes: 0 additions & 9 deletions src/layout/GlobalLayout/Locale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import useSWR from 'swr';

import { createI18nNext } from '@/locales/create';
import { normalizeLocale } from '@/locales/resources';
import { useOnFinishHydrationGlobal } from '@/store/global';
import { settingsSelectors } from '@/store/global/selectors';
import { isOnServerSide } from '@/utils/env';
import { switchLang } from '@/utils/switchLang';

const getAntdLocale = async (lang?: string) => {
let normalLang = normalizeLocale(lang);
Expand Down Expand Up @@ -50,12 +47,6 @@ const Locale = memo<LocaleLayoutProps>(({ children, defaultLang }) => {
});
}

useOnFinishHydrationGlobal((s) => {
if (settingsSelectors.currentSettings(s).language === 'auto') {
switchLang('auto');
}
}, []);

// handle i18n instance language change
useEffect(() => {
const handleLang = (e: string) => {
Expand Down
3 changes: 2 additions & 1 deletion src/layout/GlobalLayout/StoreHydration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { useResponsive } from 'antd-style';
import { useRouter } from 'next/navigation';
import { memo, useEffect } from 'react';

import { useEffectAfterGlobalHydrated, useGlobalStore } from '@/store/global';
import { useGlobalStore } from '@/store/global';
import { useEffectAfterGlobalHydrated } from '@/store/global/hooks/useEffectAfterHydrated';

const StoreHydration = memo(() => {
const [useFetchServerConfig, useFetchUserConfig] = useGlobalStore((s) => [
Expand Down
3 changes: 0 additions & 3 deletions src/store/global/hooks/index.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/store/global/hooks/useOnFinishHydrationGlobal.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/store/global/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './hooks';
export * from './store';
8 changes: 8 additions & 0 deletions src/store/global/slices/common/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import type { GlobalStore } from '@/store/global';
import type { GlobalServerConfig, GlobalSettings } from '@/types/settings';
import { merge } from '@/utils/merge';
import { setNamespace } from '@/utils/storeDebug';
import { switchLang } from '@/utils/switchLang';

import { settingsSelectors } from '../settings/selectors';

const n = setNamespace('common');

Expand Down Expand Up @@ -82,6 +85,11 @@ export const createCommonSlice: StateCreator<
if (!data) return;

set({ avatar: data.avatar, settings: data.settings }, false, n('fetchUserConfig', data));

const { language } = settingsSelectors.currentSettings(get());
if (language === 'auto') {
switchLang('auto');
}
},
revalidateOnFocus: false,
},
Expand Down

0 comments on commit 7a51329

Please sign in to comment.