Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 28, 2024
2 parents 2338979 + b6bb167 commit d25e63c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { createPersistStore } from "../utils/store";
import { collectModelsWithDefaultModel } from "../utils/model";
import { useAccessStore } from "./access";
import { isDalle3 } from "../utils";
import { indexedDBStorage } from "@/app/utils/indexedDB-storage";

export type ChatMessage = RequestMessage & {
date: string;
Expand Down Expand Up @@ -665,7 +666,8 @@ export const useChatStore = createPersistStore(
set(() => ({ sessions }));
},

clearAllData() {
async clearAllData() {
await indexedDBStorage.clear();
localStorage.clear();
location.reload();
},
Expand Down
38 changes: 38 additions & 0 deletions app/utils/indexedDB-storage.ts
Original file line number Diff line number Diff line change
@@ -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<string | null> {
try {
return (await get(name)) || localStorage.getItem(name);
} catch (error) {
return localStorage.getItem(name);
}
}

public async setItem(name: string, value: string): Promise<void> {
try {
await set(name, value);
} catch (error) {
localStorage.setItem(name, value);
}
}

public async removeItem(name: string): Promise<void> {
try {
await del(name);
} catch (error) {
localStorage.removeItem(name);
}
}

public async clear(): Promise<void> {
try {
await clear();
} catch (error) {
localStorage.clear();
}
}
}

export const indexedDBStorage = new IndexedDBStorage();
4 changes: 3 additions & 1 deletion app/utils/store.ts
Original file line number Diff line number Diff line change
@@ -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 { indexedDBStorage } from "@/app/utils/indexedDB-storage";

type SecondParam<T> = T extends (
_f: infer _F,
Expand Down Expand Up @@ -31,6 +32,7 @@ export function createPersistStore<T extends object, M>(
) => M,
persistOptions: SecondParam<typeof persist<T & M & MakeUpdater<T>>>,
) {
persistOptions.storage = createJSONStorage(() => indexedDBStorage);
return create(
persist(
combine(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3926,6 +3926,11 @@ [email protected]:
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"
Expand Down

0 comments on commit d25e63c

Please sign in to comment.