Replace the UI with the Organic design handoff #124
@@ -1,4 +1,5 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
|
import { toast } from '@/components/ui/toast'
|
||||||
import { totalChanges, useGardenHistory, useUndo, type ChangeSet } from '@/lib/history'
|
import { totalChanges, useGardenHistory, useUndo, type ChangeSet } from '@/lib/history'
|
||||||
|
|
||||||
/** The newest change set still in effect — not already reverted, and not itself
|
/** 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 undoLast = useCallback(async () => {
|
||||||
const fresh = await history.refetch()
|
const fresh = await history.refetch()
|
||||||
const list = fresh.data?.pages.flatMap((p) => p.changeSets) ?? sets
|
// A failed refetch keeps the STALE pages in `data` (react-query doesn't
|
||||||
const t = latestUndoable(list)
|
// 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)
|
if (t) undo.undo(t)
|
||||||
}, [history, undo, sets])
|
}, [history, undo])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
history,
|
history,
|
||||||
|
|||||||
Reference in New Issue
Block a user