|
| 1 | +import { useRef, useState } from "react"; |
| 2 | + |
| 3 | +import type { SDCPN } from "../../../src/core/types/sdcpn"; |
| 4 | +import { isSDCPNEqual } from "../../../src/petrinaut"; |
| 5 | + |
| 6 | +export type HistoryEntry = { |
| 7 | + sdcpn: SDCPN; |
| 8 | + timestamp: string; |
| 9 | +}; |
| 10 | + |
| 11 | +const MAX_HISTORY = 50; |
| 12 | +const DEBOUNCE_MS = 500; |
| 13 | + |
| 14 | +export function useUndoRedo(initialSDCPN: SDCPN) { |
| 15 | + const historyRef = useRef<HistoryEntry[]>([ |
| 16 | + { sdcpn: initialSDCPN, timestamp: new Date().toISOString() }, |
| 17 | + ]); |
| 18 | + const currentIndexRef = useRef(0); |
| 19 | + const debounceTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 20 | + |
| 21 | + /** |
| 22 | + * Snapshot of render-visible values derived from the refs. |
| 23 | + * Updated via `bump()` after every mutation so consumers re-render |
| 24 | + * without reading refs during render. |
| 25 | + */ |
| 26 | + const [snapshot, setSnapshot] = useState({ |
| 27 | + currentIndex: 0, |
| 28 | + historyLength: 1, |
| 29 | + }); |
| 30 | + const bump = () => |
| 31 | + setSnapshot({ |
| 32 | + currentIndex: currentIndexRef.current, |
| 33 | + historyLength: historyRef.current.length, |
| 34 | + }); |
| 35 | + |
| 36 | + const canUndo = snapshot.currentIndex > 0; |
| 37 | + const canRedo = snapshot.currentIndex < snapshot.historyLength - 1; |
| 38 | + |
| 39 | + const pushState = (sdcpn: SDCPN) => { |
| 40 | + const current = historyRef.current[currentIndexRef.current]; |
| 41 | + |
| 42 | + // No-op detection |
| 43 | + if (current && isSDCPNEqual(current.sdcpn, sdcpn)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // Debounce: coalesce rapid mutations into one entry |
| 48 | + if (debounceTimerRef.current !== null) { |
| 49 | + clearTimeout(debounceTimerRef.current); |
| 50 | + // Replace the entry at currentIndex (it was a pending debounced entry) |
| 51 | + historyRef.current[currentIndexRef.current] = { |
| 52 | + sdcpn, |
| 53 | + timestamp: new Date().toISOString(), |
| 54 | + }; |
| 55 | + |
| 56 | + debounceTimerRef.current = setTimeout(() => { |
| 57 | + debounceTimerRef.current = null; |
| 58 | + }, DEBOUNCE_MS); |
| 59 | + |
| 60 | + bump(); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Truncate any redo entries |
| 65 | + historyRef.current = historyRef.current.slice( |
| 66 | + 0, |
| 67 | + currentIndexRef.current + 1, |
| 68 | + ); |
| 69 | + |
| 70 | + // Push new entry |
| 71 | + historyRef.current.push({ |
| 72 | + sdcpn, |
| 73 | + timestamp: new Date().toISOString(), |
| 74 | + }); |
| 75 | + |
| 76 | + // Enforce max history size |
| 77 | + if (historyRef.current.length > MAX_HISTORY) { |
| 78 | + historyRef.current = historyRef.current.slice( |
| 79 | + historyRef.current.length - MAX_HISTORY, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + currentIndexRef.current = historyRef.current.length - 1; |
| 84 | + |
| 85 | + // Start debounce window |
| 86 | + debounceTimerRef.current = setTimeout(() => { |
| 87 | + debounceTimerRef.current = null; |
| 88 | + }, DEBOUNCE_MS); |
| 89 | + |
| 90 | + bump(); |
| 91 | + }; |
| 92 | + |
| 93 | + const clearDebounce = () => { |
| 94 | + if (debounceTimerRef.current !== null) { |
| 95 | + clearTimeout(debounceTimerRef.current); |
| 96 | + debounceTimerRef.current = null; |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + const undo = (): SDCPN | null => { |
| 101 | + if (currentIndexRef.current <= 0) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + clearDebounce(); |
| 105 | + currentIndexRef.current -= 1; |
| 106 | + bump(); |
| 107 | + return historyRef.current[currentIndexRef.current]!.sdcpn; |
| 108 | + }; |
| 109 | + |
| 110 | + const redo = (): SDCPN | null => { |
| 111 | + if (currentIndexRef.current >= historyRef.current.length - 1) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + clearDebounce(); |
| 115 | + currentIndexRef.current += 1; |
| 116 | + bump(); |
| 117 | + return historyRef.current[currentIndexRef.current]!.sdcpn; |
| 118 | + }; |
| 119 | + |
| 120 | + const goToIndex = (index: number): SDCPN | null => { |
| 121 | + if (index < 0 || index >= historyRef.current.length) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + clearDebounce(); |
| 125 | + currentIndexRef.current = index; |
| 126 | + bump(); |
| 127 | + return historyRef.current[index]!.sdcpn; |
| 128 | + }; |
| 129 | + |
| 130 | + const reset = (sdcpn: SDCPN) => { |
| 131 | + historyRef.current = [{ sdcpn, timestamp: new Date().toISOString() }]; |
| 132 | + currentIndexRef.current = 0; |
| 133 | + if (debounceTimerRef.current !== null) { |
| 134 | + clearTimeout(debounceTimerRef.current); |
| 135 | + debounceTimerRef.current = null; |
| 136 | + } |
| 137 | + bump(); |
| 138 | + }; |
| 139 | + |
| 140 | + return { |
| 141 | + pushState, |
| 142 | + undo, |
| 143 | + redo, |
| 144 | + goToIndex, |
| 145 | + canUndo, |
| 146 | + canRedo, |
| 147 | + history: historyRef, |
| 148 | + currentIndex: snapshot.currentIndex, |
| 149 | + reset, |
| 150 | + }; |
| 151 | +} |
0 commit comments