From 44ab0483b626ada24817c6f9e0ad9d7c0971e09d Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Tue, 14 May 2024 17:57:53 -0700 Subject: [PATCH] DChat: remove IDB migration --- src/common/state/store-chats.ts | 33 ++------------------------------- src/common/util/idbUtils.ts | 15 --------------- 2 files changed, 2 insertions(+), 46 deletions(-) diff --git a/src/common/state/store-chats.ts b/src/common/state/store-chats.ts index a1d3f226e..9119beeba 100644 --- a/src/common/state/store-chats.ts +++ b/src/common/state/store-chats.ts @@ -5,7 +5,7 @@ import { v4 as uuidv4 } from 'uuid'; import { DLLMId, getChatLLMId } from '~/modules/llms/store-llms'; -import { IDB_MIGRATION_INITIAL, idbStateStorage } from '../util/idbUtils'; +import { idbStateStorage } from '../util/idbUtils'; import { countModelTokens } from '../util/token-counter'; import { defaultSystemPurposeId, SystemPurposeId } from '../../data'; @@ -407,10 +407,7 @@ export const useChatStore = create()(devtools( storage: createJSONStorage(() => idbStateStorage), // Migrations - migrate: (persistedState: unknown, fromVersion: number): ConversationsStore => { - // -1 -> 3: migration loading from localStorage to IndexedDB - if (fromVersion === IDB_MIGRATION_INITIAL) - return _migrateLocalStorageData() as any; + migrate: (persistedState: unknown, _fromVersion: number): ConversationsStore => { // other: just proceed return persistedState as any; @@ -465,32 +462,6 @@ function getNextBranchTitle(currentTitle: string): string { return `(1) ${currentTitle}`; } -/** - * Returns the chats stored in the localStorage, and rename the key for - * backup/data loss prevention purposes - */ -function _migrateLocalStorageData(): ChatState | {} { - const key = 'app-chats'; - const value = localStorage.getItem(key); - if (!value) return {}; - try { - // parse the localStorage state - const localStorageState = JSON.parse(value)?.state; - - // backup and delete the localStorage key - const backupKey = `${key}-v2`; - localStorage.setItem(backupKey, value); - localStorage.removeItem(key); - - // match the state from localstorage - return { - conversations: localStorageState?.conversations ?? [], - }; - } catch (error) { - console.error('LocalStorage migration error', error); - return {}; - } -} /** * Convenience function to count the tokens in a DMessage object diff --git a/src/common/util/idbUtils.ts b/src/common/util/idbUtils.ts index b900a8ecf..fd42178fd 100644 --- a/src/common/util/idbUtils.ts +++ b/src/common/util/idbUtils.ts @@ -1,10 +1,6 @@ import type { StateStorage } from 'zustand/middleware'; import { del as idbDel, get as idbGet, set as idbSet } from 'idb-keyval'; -// used by the state storage middleware to detect data migration from the old state storage (localStorage) -// NOTE: remove past 2024-03-19 (6 months past release of this utility conversion) -export const IDB_MIGRATION_INITIAL = -1; - // set to true to enable debugging const DEBUG_SCHEDULER = false; @@ -130,17 +126,6 @@ export const idbStateStorage: StateStorage = { if (DEBUG_SCHEDULER) console.warn(' (read bytes:', value?.length?.toLocaleString(), ')'); - /* IMPORTANT! - * We modify the default behavior of `getItem` to return a {version: -1} object if a key is not found. - * This is to trigger the migration across state storage implementations, as Zustand would not call the - * 'migrate' function otherwise. - * See 'https://github.com/enricoros/big-agi/pull/158' for more details - */ - if (value === undefined) { - return JSON.stringify({ - version: IDB_MIGRATION_INITIAL, - }); - } return value || null; }, setItem: (name: string, value: string): void => {