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

feat: add indexDB #5324

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
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);
}
lloydzhou marked this conversation as resolved.
Show resolved Hide resolved
}

public async setItem(name: string, value: string): Promise<void> {
try {
await set(name, value);
} catch (error) {
localStorage.setItem(name, value);
}
lloydzhou marked this conversation as resolved.
Show resolved Hide resolved
}

public async removeItem(name: string): Promise<void> {
try {
await del(name);
} catch (error) {
localStorage.removeItem(name);
}
lloydzhou marked this conversation as resolved.
Show resolved Hide resolved
}

public async clear(): Promise<void> {
try {
await clear();
} catch (error) {
localStorage.clear();
}
lloydzhou marked this conversation as resolved.
Show resolved Hide resolved
}
}
lloydzhou marked this conversation as resolved.
Show resolved Hide resolved

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 { indexDBStorage } from "@/app/utils/indexDB-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(() => indexDBStorage);
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
Loading