From ee496adee6b4436217b4ccb7960b9839d674f032 Mon Sep 17 00:00:00 2001 From: Dan Mindru Date: Thu, 19 Dec 2024 15:38:54 +0100 Subject: [PATCH] Add data migration --- packages/ui/src/app/App.tsx | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/ui/src/app/App.tsx b/packages/ui/src/app/App.tsx index 9253bdb..173cafd 100644 --- a/packages/ui/src/app/App.tsx +++ b/packages/ui/src/app/App.tsx @@ -25,6 +25,8 @@ import { useStoredPreferences } from 'shared/hooks/useStoredPreferences'; import { ClobbrUIListItem } from 'models/ClobbrUIListItem'; import { useToastStore } from 'toasts/state/toastStore'; import { useFetchResults } from 'results/useFetchResults'; +import { getDb } from 'storage/storage'; +import { EDbStores } from 'storage/EDbStores'; const App = () => { const toasts = useToastStore((state) => state.toasts); @@ -49,6 +51,7 @@ const App = () => { const [resultStorageLoaded, setResultStorageLoaded] = useState(false); const [preferencesApplied, setPreferencesApplied] = useState(false); const [themeMode, setThemeMode] = useState(DEFAULT_GLOBAL_STORE.themeMode); + const [hasMigratedData, setHasMigratedData] = useState(false); const { preferences, preferencesLoaded } = useStoredPreferences(); const { state } = useClobbrState({ initialState: DEFAULT_GLOBAL_STORE }); @@ -168,6 +171,35 @@ const App = () => { [is2xl, is3xl] ); + useEffect(() => { + // Migrate indexdb data once and then clear it. + if (hasMigratedData) { + return; + } + + const migrateData = async () => { + const resultDb = getDb(EDbStores.RESULT_STORE_NAME); + const existingResultList = await resultDb.getItem(SK.RESULT.LIST); + + if (!existingResultList?.length) { + console.warn('Skipping data migration'); + return; + } + + (window as any).electronAPI.migrateData(existingResultList); + const results = await (window as any).electronAPI.getResults(); + state.results.setList(results); + + await resultDb.clear(); + + const resultLogDb = getDb(EDbStores.RESULT_LOGS_STORE_NAME); + await resultLogDb.clear(); + }; + + setHasMigratedData(true); + migrateData(); + }, []); + return (