Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: proper localstorage usage #5382

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions app/components/mask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { Updater } from "../typing";
import { ModelConfigList } from "./model-config";
import { FileName, Path } from "../constant";
import { BUILTIN_MASK_STORE } from "../masks";
import { nanoid } from "nanoid";
import { useLocalStorage } from "foxact/use-local-storage";
import {
DragDropContext,
Droppable,
Expand Down Expand Up @@ -426,16 +426,11 @@ export function MaskPage() {
const maskStore = useMaskStore();
const chatStore = useChatStore();

const [filterLang, setFilterLang] = useState<Lang | undefined>(
() => localStorage.getItem("Mask-language") as Lang | undefined,
const [filterLang, setFilterLang] = useLocalStorage<Lang | null>(
"Mask-language",
null,
{ raw: true },
);
useEffect(() => {
if (filterLang) {
localStorage.setItem("Mask-language", filterLang);
} else {
localStorage.removeItem("Mask-language");
}
}, [filterLang]);

const allMasks = maskStore
.getAll()
Expand Down Expand Up @@ -542,7 +537,7 @@ export function MaskPage() {
onChange={(e) => {
const value = e.currentTarget.value;
if (value === Locale.Settings.Lang.All) {
setFilterLang(undefined);
setFilterLang(null);
} else {
setFilterLang(value as Lang);
}
Expand Down
6 changes: 6 additions & 0 deletions app/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ merge(fallbackLang, targetLang);
export default fallbackLang as LocaleType;

function getItem(key: string) {
if (typeof window === "undefined") {
return null;
}
try {
return localStorage.getItem(key);
} catch {
Expand All @@ -90,6 +93,9 @@ function getItem(key: string) {
}

function setItem(key: string, value: string) {
if (typeof window === "undefined") {
return null;
}
try {
localStorage.setItem(key, value);
} catch {}
Expand Down
12 changes: 12 additions & 0 deletions app/utils/indexedDB-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { get, set, del, clear } from "idb-keyval";

class IndexedDBStorage implements StateStorage {
public async getItem(name: string): Promise<string | null> {
if (typeof window === "undefined") {
return null;
}
try {
const value = (await get(name)) || localStorage.getItem(name);
return value;
Expand All @@ -12,6 +15,9 @@ class IndexedDBStorage implements StateStorage {
}

public async setItem(name: string, value: string): Promise<void> {
if (typeof window === "undefined") {
return;
}
try {
const _value = JSON.parse(value);
if (!_value?.state?._hasHydrated) {
Expand All @@ -25,6 +31,9 @@ class IndexedDBStorage implements StateStorage {
}

public async removeItem(name: string): Promise<void> {
if (typeof window === "undefined") {
return;
}
try {
await del(name);
} catch (error) {
Expand All @@ -33,6 +42,9 @@ class IndexedDBStorage implements StateStorage {
}

public async clear(): Promise<void> {
if (typeof window === "undefined") {
return;
}
try {
await clear();
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@vercel/speed-insights": "^1.0.2",
"axios": "^1.7.5",
"emoji-picker-react": "^4.9.2",
"foxact": "^0.2.37",
"fuse.js": "^7.0.0",
"heic2any": "^0.0.4",
"html-to-image": "^1.11.11",
Expand Down
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,7 @@ cli-truncate@^3.1.0:
slice-ansi "^5.0.0"
string-width "^5.0.0"

[email protected]:
[email protected], client-only@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/client-only/-/client-only-0.0.1.tgz#38bba5d403c41ab150bff64a95c85013cf73bca1"
integrity sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==
Expand Down Expand Up @@ -3627,6 +3627,14 @@ formdata-polyfill@^4.0.10:
dependencies:
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:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -5778,6 +5786,11 @@ serialize-javascript@^6.0.1:
dependencies:
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:
version "2.0.0"
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"
Expand Down
Loading