Skip to content

Commit 3f0e896

Browse files
committed
fix(undo-redo): clear IndexedDB key on sign-out
`clearUserData` previously cleared `localStorage` only. After moving workflow-undo-redo persistence to IndexedDB, undo/redo history could survive across user sessions on the same device. Add `clearPersistedUndoRedo()` to the adapter and invoke it from `clearUserData` so sign-out removes the IndexedDB entry as well. The migration flag is left intact so the one-time migration does not re-run on the next sign-in. Signed-off-by: JaeHyung Jang <jaehyung.jang@navercorp.com>
1 parent 54a70cc commit 3f0e896

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

apps/sim/stores/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { environmentKeys } from '@/hooks/queries/environment'
66
import { useExecutionStore } from '@/stores/execution'
77
import { useMothershipDraftsStore } from '@/stores/mothership-drafts/store'
88
import { consolePersistence, useTerminalConsoleStore } from '@/stores/terminal'
9+
import { clearPersistedUndoRedo } from '@/stores/undo-redo/storage'
910
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
1011
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
1112
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
@@ -55,6 +56,9 @@ export async function clearUserData(): Promise<void> {
5556
const keysToRemove = Object.keys(localStorage).filter((key) => !keysToKeep.includes(key))
5657
keysToRemove.forEach((key) => localStorage.removeItem(key))
5758

59+
// Persisted undo/redo lives in IndexedDB; remove it as well.
60+
await clearPersistedUndoRedo()
61+
5862
logger.info('User data cleared successfully')
5963
} catch (error) {
6064
logger.error('Error clearing user data:', { error })

apps/sim/stores/undo-redo/storage.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,34 @@ describe('undo-redo IndexedDB storage adapter', () => {
154154
expect(value).toBeNull()
155155
})
156156
})
157+
158+
describe('clearPersistedUndoRedo', () => {
159+
it('deletes the undo-redo key from IndexedDB', async () => {
160+
const { clearPersistedUndoRedo, migrationReady } = await loadFreshModule()
161+
await migrationReady
162+
idbStore.set(STORE_KEY, 'present')
163+
164+
await clearPersistedUndoRedo()
165+
166+
expect(idbStore.has(STORE_KEY)).toBe(false)
167+
})
168+
169+
it('leaves the migration flag intact so migration does not re-run', async () => {
170+
const { clearPersistedUndoRedo, migrationReady } = await loadFreshModule()
171+
await migrationReady
172+
idbStore.set(STORE_KEY, 'present')
173+
174+
await clearPersistedUndoRedo()
175+
176+
expect(idbStore.get(MIGRATION_KEY)).toBe(true)
177+
})
178+
179+
it('swallows IndexedDB errors so sign-out does not crash', async () => {
180+
const { clearPersistedUndoRedo, migrationReady } = await loadFreshModule()
181+
await migrationReady
182+
183+
idbDel.mockRejectedValueOnce(new Error('idb delete failed'))
184+
await expect(clearPersistedUndoRedo()).resolves.toBeUndefined()
185+
})
186+
})
157187
})

apps/sim/stores/undo-redo/storage.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ async function awaitMigration(): Promise<void> {
5555
}
5656
}
5757

58+
/**
59+
* Removes the persisted undo/redo payload from IndexedDB.
60+
*
61+
* Called from `clearUserData` on sign-out so undo history does not
62+
* survive across user sessions on the same device.
63+
*/
64+
export async function clearPersistedUndoRedo(): Promise<void> {
65+
if (typeof window === 'undefined') return
66+
await awaitMigration()
67+
68+
try {
69+
await del(STORE_KEY)
70+
} catch (error) {
71+
logger.warn('Failed to clear persisted undo-redo', { error })
72+
}
73+
}
74+
5875
export const indexedDBStorage: StateStorage = {
5976
getItem: async (name: string): Promise<string | null> => {
6077
if (typeof window === 'undefined') return null

0 commit comments

Comments
 (0)