diff --git a/web/src/editor/useUndoLast.ts b/web/src/editor/useUndoLast.ts index 279bcfe..a21c315 100644 --- a/web/src/editor/useUndoLast.ts +++ b/web/src/editor/useUndoLast.ts @@ -1,4 +1,5 @@ import { useCallback } from 'react' +import { toast } from '@/components/ui/toast' import { totalChanges, useGardenHistory, useUndo, type ChangeSet } from '@/lib/history' /** The newest change set still in effect — not already reverted, and not itself @@ -23,10 +24,16 @@ export function useUndoLast(gardenId: number, enabled: boolean) { const undoLast = useCallback(async () => { const fresh = await history.refetch() - const list = fresh.data?.pages.flatMap((p) => p.changeSets) ?? sets - const t = latestUndoable(list) + // A failed refetch keeps the STALE pages in `data` (react-query doesn't + // clear them), so falling through here would pick the step before the one + // just made. Undoing nothing is the only safe answer until it can be read. + if (fresh.status !== 'success') { + toast.error("Couldn't re-read the history, so nothing was undone — try again.") + return + } + const t = latestUndoable(fresh.data.pages.flatMap((p) => p.changeSets)) if (t) undo.undo(t) - }, [history, undo, sets]) + }, [history, undo]) return { history,