From 4060e367ad90be23b9a94c241e2251d952520ea4 Mon Sep 17 00:00:00 2001 From: Dogtiti <499960698@qq.com> Date: Mon, 26 Aug 2024 21:13:35 +0800 Subject: [PATCH 1/5] feat: add indexDB --- app/utils/indexDB-storage.ts | 20 ++++++++++++++++++++ app/utils/store.ts | 4 +++- package.json | 1 + yarn.lock | 5 +++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 app/utils/indexDB-storage.ts diff --git a/app/utils/indexDB-storage.ts b/app/utils/indexDB-storage.ts new file mode 100644 index 00000000000..ef99279232c --- /dev/null +++ b/app/utils/indexDB-storage.ts @@ -0,0 +1,20 @@ +import { StateStorage } from "zustand/middleware"; +import { get, set, del } from "idb-keyval"; + +class IndexDBStorage implements StateStorage { + constructor() {} + + public async getItem(name: string): Promise { + return (await get(name)) || localStorage.getItem(name); + } + + public async setItem(name: string, value: string): Promise { + await set(name, value); + } + + public async removeItem(name: string): Promise { + await del(name); + } +} + +export const indexDBStorage = new IndexDBStorage(); diff --git a/app/utils/store.ts b/app/utils/store.ts index 684a1911279..1a7e0d24b77 100644 --- a/app/utils/store.ts +++ b/app/utils/store.ts @@ -1,7 +1,8 @@ import { create } from "zustand"; -import { combine, persist } from "zustand/middleware"; +import { combine, persist, createJSONStorage } from "zustand/middleware"; import { Updater } from "../typing"; import { deepClone } from "./clone"; +import { indexDBStorage } from "@/app/utils/indexDB-storage"; type SecondParam = T extends ( _f: infer _F, @@ -31,6 +32,7 @@ export function createPersistStore( ) => M, persistOptions: SecondParam>>, ) { + persistOptions.storage = createJSONStorage(() => indexDBStorage); return create( persist( combine( diff --git a/package.json b/package.json index eb0a5ef6735..1c6d78c208e 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "fuse.js": "^7.0.0", "heic2any": "^0.0.4", "html-to-image": "^1.11.11", + "idb-keyval": "^6.2.1", "lodash-es": "^4.17.21", "mermaid": "^10.6.1", "nanoid": "^5.0.3", diff --git a/yarn.lock b/yarn.lock index 793c845d722..1c7f834e876 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3926,6 +3926,11 @@ iconv-lite@0.6: dependencies: safer-buffer ">= 2.1.2 < 3.0.0" +idb-keyval@^6.2.1: + version "6.2.1" + resolved "https://registry.npmmirror.com/idb-keyval/-/idb-keyval-6.2.1.tgz#94516d625346d16f56f3b33855da11bfded2db33" + integrity sha512-8Sb3veuYCyrZL+VBt9LJfZjLUPWVvqn8tG28VqYNFCo43KHcKuq+b4EiXGeuaLAQWL2YmyDgMp2aSpH9JHsEQg== + ignore@^5.2.0: version "5.2.4" resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.2.4.tgz#a291c0c6178ff1b960befe47fcdec301674a6324" From 492b55c8939593f9eddef084f99e14a5d4a5033b Mon Sep 17 00:00:00 2001 From: Dogtiti <499960698@qq.com> Date: Mon, 26 Aug 2024 21:20:07 +0800 Subject: [PATCH 2/5] feat: add indexDB --- app/store/chat.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index 653926d1b02..ffaf8c9966f 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -27,6 +27,7 @@ import { createPersistStore } from "../utils/store"; import { collectModelsWithDefaultModel } from "../utils/model"; import { useAccessStore } from "./access"; import { isDalle3 } from "../utils"; +import { clear } from "idb-keyval"; export type ChatMessage = RequestMessage & { date: string; @@ -665,7 +666,8 @@ export const useChatStore = createPersistStore( set(() => ({ sessions })); }, - clearAllData() { + async clearAllData() { + await clear(); localStorage.clear(); location.reload(); }, From 0b758941a4104ee6fdcb58431ac7ebc5c69f2323 Mon Sep 17 00:00:00 2001 From: Dogtiti <499960698@qq.com> Date: Mon, 26 Aug 2024 21:23:21 +0800 Subject: [PATCH 3/5] feat: clear indexDB --- app/store/chat.ts | 4 ++-- app/utils/indexDB-storage.ts | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index ffaf8c9966f..a6e4ee796a7 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -27,7 +27,7 @@ import { createPersistStore } from "../utils/store"; import { collectModelsWithDefaultModel } from "../utils/model"; import { useAccessStore } from "./access"; import { isDalle3 } from "../utils"; -import { clear } from "idb-keyval"; +import { indexDBStorage } from "@/app/utils/indexDB-storage"; export type ChatMessage = RequestMessage & { date: string; @@ -667,7 +667,7 @@ export const useChatStore = createPersistStore( }, async clearAllData() { - await clear(); + await indexDBStorage.clear(); localStorage.clear(); location.reload(); }, diff --git a/app/utils/indexDB-storage.ts b/app/utils/indexDB-storage.ts index ef99279232c..5dee4c856cc 100644 --- a/app/utils/indexDB-storage.ts +++ b/app/utils/indexDB-storage.ts @@ -1,9 +1,7 @@ import { StateStorage } from "zustand/middleware"; -import { get, set, del } from "idb-keyval"; +import { get, set, del, clear } from "idb-keyval"; class IndexDBStorage implements StateStorage { - constructor() {} - public async getItem(name: string): Promise { return (await get(name)) || localStorage.getItem(name); } @@ -15,6 +13,10 @@ class IndexDBStorage implements StateStorage { public async removeItem(name: string): Promise { await del(name); } + + public async clear(): Promise { + await clear(); + } } export const indexDBStorage = new IndexDBStorage(); From c2fc0b49797ef8b016949d9051bfad140326bdef Mon Sep 17 00:00:00 2001 From: Dogtiti <499960698@qq.com> Date: Tue, 27 Aug 2024 09:57:07 +0800 Subject: [PATCH 4/5] feat: try catch indexedDB error --- app/store/chat.ts | 4 ++-- app/utils/indexDB-storage.ts | 22 -------------------- app/utils/indexedDB-storage.ts | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 24 deletions(-) delete mode 100644 app/utils/indexDB-storage.ts create mode 100644 app/utils/indexedDB-storage.ts diff --git a/app/store/chat.ts b/app/store/chat.ts index a6e4ee796a7..de2a6307850 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -27,7 +27,7 @@ import { createPersistStore } from "../utils/store"; import { collectModelsWithDefaultModel } from "../utils/model"; import { useAccessStore } from "./access"; import { isDalle3 } from "../utils"; -import { indexDBStorage } from "@/app/utils/indexDB-storage"; +import { indexedDBStorage } from "@/app/utils/indexedDB-storage"; export type ChatMessage = RequestMessage & { date: string; @@ -667,7 +667,7 @@ export const useChatStore = createPersistStore( }, async clearAllData() { - await indexDBStorage.clear(); + await indexedDBStorage.clear(); localStorage.clear(); location.reload(); }, diff --git a/app/utils/indexDB-storage.ts b/app/utils/indexDB-storage.ts deleted file mode 100644 index 5dee4c856cc..00000000000 --- a/app/utils/indexDB-storage.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { StateStorage } from "zustand/middleware"; -import { get, set, del, clear } from "idb-keyval"; - -class IndexDBStorage implements StateStorage { - public async getItem(name: string): Promise { - return (await get(name)) || localStorage.getItem(name); - } - - public async setItem(name: string, value: string): Promise { - await set(name, value); - } - - public async removeItem(name: string): Promise { - await del(name); - } - - public async clear(): Promise { - await clear(); - } -} - -export const indexDBStorage = new IndexDBStorage(); diff --git a/app/utils/indexedDB-storage.ts b/app/utils/indexedDB-storage.ts new file mode 100644 index 00000000000..51ee92b8185 --- /dev/null +++ b/app/utils/indexedDB-storage.ts @@ -0,0 +1,38 @@ +import { StateStorage } from "zustand/middleware"; +import { get, set, del, clear } from "idb-keyval"; + +class IndexedDBStorage implements StateStorage { + public async getItem(name: string): Promise { + try { + return (await get(name)) || localStorage.getItem(name); + } catch (error) { + return localStorage.getItem(name); + } + } + + public async setItem(name: string, value: string): Promise { + try { + await set(name, value); + } catch (error) { + localStorage.setItem(name, value); + } + } + + public async removeItem(name: string): Promise { + try { + await del(name); + } catch (error) { + localStorage.removeItem(name); + } + } + + public async clear(): Promise { + try { + await clear(); + } catch (error) { + localStorage.clear(); + } + } +} + +export const indexedDBStorage = new IndexedDBStorage(); From 7b6fe66f2a1a7f227f7116b72f9dd4e10207cd44 Mon Sep 17 00:00:00 2001 From: Dogtiti <499960698@qq.com> Date: Tue, 27 Aug 2024 10:05:37 +0800 Subject: [PATCH 5/5] feat: try catch indexedDB error --- app/utils/store.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/utils/store.ts b/app/utils/store.ts index 1a7e0d24b77..13bef6d5dfe 100644 --- a/app/utils/store.ts +++ b/app/utils/store.ts @@ -2,7 +2,7 @@ import { create } from "zustand"; import { combine, persist, createJSONStorage } from "zustand/middleware"; import { Updater } from "../typing"; import { deepClone } from "./clone"; -import { indexDBStorage } from "@/app/utils/indexDB-storage"; +import { indexedDBStorage } from "@/app/utils/indexedDB-storage"; type SecondParam = T extends ( _f: infer _F, @@ -32,7 +32,7 @@ export function createPersistStore( ) => M, persistOptions: SecondParam>>, ) { - persistOptions.storage = createJSONStorage(() => indexDBStorage); + persistOptions.storage = createJSONStorage(() => indexedDBStorage); return create( persist( combine(