Skip to content

Commit

Permalink
Add data migration
Browse files Browse the repository at this point in the history
  • Loading branch information
danmindru committed Dec 19, 2024
1 parent d198e03 commit ee496ad
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/ui/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 });
Expand Down Expand Up @@ -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 (
<GlobalStore.Provider value={state}>
<PreferenceLoader />
Expand Down

0 comments on commit ee496ad

Please sign in to comment.