Unverified Commit 2fc67eb1 authored by Licho's avatar Licho Committed by GitHub

fix: language cookie problem (#2105)

* refactor: package uri wrong, and trivial optimization * fix: language problem Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent e65d5f78
...@@ -41,6 +41,7 @@ const GlobalHeaderRight: React.FC = () => { ...@@ -41,6 +41,7 @@ const GlobalHeaderRight: React.FC = () => {
const [langCache, setLangCache] = useCookie(STORY_LANGUAGE, language); const [langCache, setLangCache] = useCookie(STORY_LANGUAGE, language);
useEffect(() => { useEffect(() => {
setLangCache(language);
(async () => await setInitialState((initialStateType) => ({ (async () => await setInitialState((initialStateType) => ({
...initialStateType, ...initialStateType,
locale: language, locale: language,
...@@ -52,11 +53,6 @@ const GlobalHeaderRight: React.FC = () => { ...@@ -52,11 +53,6 @@ const GlobalHeaderRight: React.FC = () => {
})))(); })))();
}, [theme, language]); }, [theme, language]);
function changeHandler(value: boolean) {
setTheme(value ? THEME.dark : THEME.light)
setLangCache(STORY_LANGUAGE, language);
}
if (!initialState || !initialState.settings) { if (!initialState || !initialState.settings) {
return null; return null;
} }
...@@ -138,7 +134,7 @@ const GlobalHeaderRight: React.FC = () => { ...@@ -138,7 +134,7 @@ const GlobalHeaderRight: React.FC = () => {
checked={theme === THEME.dark} checked={theme === THEME.dark}
checkedChildren={<ThemeCloud/>} checkedChildren={<ThemeCloud/>}
unCheckedChildren={<ThemeStar/>} unCheckedChildren={<ThemeStar/>}
onChange={changeHandler}/> onChange={(value) => setTheme(value ? THEME.dark : THEME.light)}/>
</> </>
); );
}; };
......
...@@ -18,22 +18,22 @@ ...@@ -18,22 +18,22 @@
import {useEmotionCss} from "@ant-design/use-emotion-css"; import {useEmotionCss} from "@ant-design/use-emotion-css";
import {SelectLang, useModel} from '@@/exports'; import {SelectLang, useModel} from '@@/exports';
import React, {useEffect} from 'react'; import React, {useEffect} from 'react';
import {getLocalStorageLanguage, setCookieByKey} from '@/utils/function'; import {LANGUAGE_KEY, LANGUAGE_ZH, STORY_LANGUAGE} from '@/services/constants';
import {STORY_LANGUAGE} from '@/services/constants'; import useCookie from "react-use-cookie";
import {useLocalStorage} from "@/utils/hook/useLocalStorage";
const LangSwitch = () => { const LangSwitch = () => {
const {initialState, setInitialState} = useModel("@@initialState"); const {initialState, setInitialState} = useModel("@@initialState");
const [language, setLanguage] = useLocalStorage(LANGUAGE_KEY, LANGUAGE_ZH);
const [langCache, setLangCache] = useCookie(STORY_LANGUAGE, language);
useEffect(() => { useEffect(() => {
const lang = getLocalStorageLanguage(); setLangCache(language)
if (lang) {
setCookieByKey(STORY_LANGUAGE, lang);
setInitialState((s) => ({ setInitialState((s) => ({
...s, ...s,
locale: lang, locale: language,
})); }));
}
}, [initialState]); }, [initialState]);
......
...@@ -5,7 +5,7 @@ export function useLocalStorage(key: string, initialValue: any) { ...@@ -5,7 +5,7 @@ export function useLocalStorage(key: string, initialValue: any) {
const readValue = React.useCallback(() => { const readValue = React.useCallback(() => {
try { try {
const item = window.localStorage.getItem(key); const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue; return item ?? initialValue;
} catch (error) { } catch (error) {
console.warn(error); console.warn(error);
return initialValue; return initialValue;
...@@ -19,7 +19,7 @@ export function useLocalStorage(key: string, initialValue: any) { ...@@ -19,7 +19,7 @@ export function useLocalStorage(key: string, initialValue: any) {
try { try {
const nextState = const nextState =
typeof value === "function" ? value(localState) : value; typeof value === "function" ? value(localState) : value;
window.localStorage.setItem(key, JSON.stringify(nextState)); window.localStorage.setItem(key, nextState);
setLocalState(nextState); setLocalState(nextState);
window.dispatchEvent(new Event("local-storage")); window.dispatchEvent(new Event("local-storage"));
} catch (e) { } catch (e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment