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 = () => {
const [langCache, setLangCache] = useCookie(STORY_LANGUAGE, language);
useEffect(() => {
setLangCache(language);
(async () => await setInitialState((initialStateType) => ({
...initialStateType,
locale: language,
......@@ -52,11 +53,6 @@ const GlobalHeaderRight: React.FC = () => {
})))();
}, [theme, language]);
function changeHandler(value: boolean) {
setTheme(value ? THEME.dark : THEME.light)
setLangCache(STORY_LANGUAGE, language);
}
if (!initialState || !initialState.settings) {
return null;
}
......@@ -138,7 +134,7 @@ const GlobalHeaderRight: React.FC = () => {
checked={theme === THEME.dark}
checkedChildren={<ThemeCloud/>}
unCheckedChildren={<ThemeStar/>}
onChange={changeHandler}/>
onChange={(value) => setTheme(value ? THEME.dark : THEME.light)}/>
</>
);
};
......
......@@ -18,22 +18,22 @@
import {useEmotionCss} from "@ant-design/use-emotion-css";
import {SelectLang, useModel} from '@@/exports';
import React, {useEffect} from 'react';
import {getLocalStorageLanguage, setCookieByKey} from '@/utils/function';
import {STORY_LANGUAGE} from '@/services/constants';
import {LANGUAGE_KEY, LANGUAGE_ZH, STORY_LANGUAGE} from '@/services/constants';
import useCookie from "react-use-cookie";
import {useLocalStorage} from "@/utils/hook/useLocalStorage";
const LangSwitch = () => {
const {initialState, setInitialState} = useModel("@@initialState");
const [language, setLanguage] = useLocalStorage(LANGUAGE_KEY, LANGUAGE_ZH);
const [langCache, setLangCache] = useCookie(STORY_LANGUAGE, language);
useEffect(() => {
const lang = getLocalStorageLanguage();
if (lang) {
setCookieByKey(STORY_LANGUAGE, lang);
setLangCache(language)
setInitialState((s) => ({
...s,
locale: lang,
locale: language,
}));
}
}, [initialState]);
......
......@@ -5,7 +5,7 @@ export function useLocalStorage(key: string, initialValue: any) {
const readValue = React.useCallback(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
return item ?? initialValue;
} catch (error) {
console.warn(error);
return initialValue;
......@@ -19,7 +19,7 @@ export function useLocalStorage(key: string, initialValue: any) {
try {
const nextState =
typeof value === "function" ? value(localState) : value;
window.localStorage.setItem(key, JSON.stringify(nextState));
window.localStorage.setItem(key, nextState);
setLocalState(nextState);
window.dispatchEvent(new Event("local-storage"));
} 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