fix: proper localstorage usage

This commit is contained in:
SukkaW 2024-09-07 21:18:49 +08:00
parent 2ff292cbfa
commit 4c84d63ff4
5 changed files with 39 additions and 12 deletions

View File

@ -48,7 +48,7 @@ import { Updater } from "../typing";
import { ModelConfigList } from "./model-config"; import { ModelConfigList } from "./model-config";
import { FileName, Path } from "../constant"; import { FileName, Path } from "../constant";
import { BUILTIN_MASK_STORE } from "../masks"; import { BUILTIN_MASK_STORE } from "../masks";
import { nanoid } from "nanoid"; import { useLocalStorage } from "foxact/use-local-storage";
import { import {
DragDropContext, DragDropContext,
Droppable, Droppable,
@ -426,16 +426,11 @@ export function MaskPage() {
const maskStore = useMaskStore(); const maskStore = useMaskStore();
const chatStore = useChatStore(); const chatStore = useChatStore();
const [filterLang, setFilterLang] = useState<Lang | undefined>( const [filterLang, setFilterLang] = useLocalStorage<Lang | null>(
() => localStorage.getItem("Mask-language") as Lang | undefined, "Mask-language",
null,
{ raw: true },
); );
useEffect(() => {
if (filterLang) {
localStorage.setItem("Mask-language", filterLang);
} else {
localStorage.removeItem("Mask-language");
}
}, [filterLang]);
const allMasks = maskStore const allMasks = maskStore
.getAll() .getAll()
@ -542,7 +537,7 @@ export function MaskPage() {
onChange={(e) => { onChange={(e) => {
const value = e.currentTarget.value; const value = e.currentTarget.value;
if (value === Locale.Settings.Lang.All) { if (value === Locale.Settings.Lang.All) {
setFilterLang(undefined); setFilterLang(null);
} else { } else {
setFilterLang(value as Lang); setFilterLang(value as Lang);
} }

View File

@ -82,6 +82,9 @@ merge(fallbackLang, targetLang);
export default fallbackLang as LocaleType; export default fallbackLang as LocaleType;
function getItem(key: string) { function getItem(key: string) {
if (typeof window === "undefined") {
return null;
}
try { try {
return localStorage.getItem(key); return localStorage.getItem(key);
} catch { } catch {
@ -90,6 +93,9 @@ function getItem(key: string) {
} }
function setItem(key: string, value: string) { function setItem(key: string, value: string) {
if (typeof window === "undefined") {
return null;
}
try { try {
localStorage.setItem(key, value); localStorage.setItem(key, value);
} catch {} } catch {}

View File

@ -3,6 +3,9 @@ import { get, set, del, clear } from "idb-keyval";
class IndexedDBStorage implements StateStorage { class IndexedDBStorage implements StateStorage {
public async getItem(name: string): Promise<string | null> { public async getItem(name: string): Promise<string | null> {
if (typeof window === "undefined") {
return null;
}
try { try {
const value = (await get(name)) || localStorage.getItem(name); const value = (await get(name)) || localStorage.getItem(name);
return value; return value;
@ -12,6 +15,9 @@ class IndexedDBStorage implements StateStorage {
} }
public async setItem(name: string, value: string): Promise<void> { public async setItem(name: string, value: string): Promise<void> {
if (typeof window === "undefined") {
return;
}
try { try {
const _value = JSON.parse(value); const _value = JSON.parse(value);
if (!_value?.state?._hasHydrated) { if (!_value?.state?._hasHydrated) {
@ -25,6 +31,9 @@ class IndexedDBStorage implements StateStorage {
} }
public async removeItem(name: string): Promise<void> { public async removeItem(name: string): Promise<void> {
if (typeof window === "undefined") {
return;
}
try { try {
await del(name); await del(name);
} catch (error) { } catch (error) {
@ -33,6 +42,9 @@ class IndexedDBStorage implements StateStorage {
} }
public async clear(): Promise<void> { public async clear(): Promise<void> {
if (typeof window === "undefined") {
return;
}
try { try {
await clear(); await clear();
} catch (error) { } catch (error) {

View File

@ -26,6 +26,7 @@
"@vercel/speed-insights": "^1.0.2", "@vercel/speed-insights": "^1.0.2",
"axios": "^1.7.5", "axios": "^1.7.5",
"emoji-picker-react": "^4.9.2", "emoji-picker-react": "^4.9.2",
"foxact": "^0.2.37",
"fuse.js": "^7.0.0", "fuse.js": "^7.0.0",
"heic2any": "^0.0.4", "heic2any": "^0.0.4",
"html-to-image": "^1.11.11", "html-to-image": "^1.11.11",

View File

@ -2378,7 +2378,7 @@ cli-truncate@^3.1.0:
slice-ansi "^5.0.0" slice-ansi "^5.0.0"
string-width "^5.0.0" string-width "^5.0.0"
client-only@0.0.1: client-only@0.0.1, client-only@^0.0.1:
version "0.0.1" version "0.0.1"
resolved "https://registry.yarnpkg.com/client-only/-/client-only-0.0.1.tgz#38bba5d403c41ab150bff64a95c85013cf73bca1" resolved "https://registry.yarnpkg.com/client-only/-/client-only-0.0.1.tgz#38bba5d403c41ab150bff64a95c85013cf73bca1"
integrity sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA== integrity sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==
@ -3627,6 +3627,14 @@ formdata-polyfill@^4.0.10:
dependencies: dependencies:
fetch-blob "^3.1.2" fetch-blob "^3.1.2"
foxact@^0.2.37:
version "0.2.37"
resolved "https://registry.yarnpkg.com/foxact/-/foxact-0.2.37.tgz#9af5c8b56c96bb70c84760e79c8e949a6fa28f04"
integrity sha512-nzK7n3JAnmCWO3GJXe4ry196s7wlOUnJVBn/RQ9Og1FJ/pEaRi94atLGFmzM0CVDgIbSFdMnH49PtkWjSqtMSw==
dependencies:
client-only "^0.0.1"
server-only "^0.0.1"
fs.realpath@^1.0.0: fs.realpath@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
@ -5778,6 +5786,11 @@ serialize-javascript@^6.0.1:
dependencies: dependencies:
randombytes "^2.1.0" randombytes "^2.1.0"
server-only@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/server-only/-/server-only-0.0.1.tgz#0f366bb6afb618c37c9255a314535dc412cd1c9e"
integrity sha512-qepMx2JxAa5jjfzxG79yPPq+8BuFToHd1hm7kI+Z4zAq1ftQiP7HcxMhDDItrbtwVeLg/cY2JnKnrcFkmiswNA==
shebang-command@^2.0.0: shebang-command@^2.0.0:
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea" resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"