| 36 | } |
| 37 | |
| 38 | export function useTableUndo({ |
| 39 | workspaceId, |
| 40 | tableId, |
| 41 | onColumnOrderChange, |
| 42 | onColumnRename, |
| 43 | onColumnWidthsChange, |
| 44 | onPinnedColumnsChange, |
| 45 | getPinnedColumns, |
| 46 | getColumnWidths, |
| 47 | }: UseTableUndoProps) { |
| 48 | const push = useTableUndoStore((s) => s.push) |
| 49 | const popUndo = useTableUndoStore((s) => s.popUndo) |
| 50 | const popRedo = useTableUndoStore((s) => s.popRedo) |
| 51 | const patchRedoRowId = useTableUndoStore((s) => s.patchRedoRowId) |
| 52 | const patchUndoRowId = useTableUndoStore((s) => s.patchUndoRowId) |
| 53 | const clear = useTableUndoStore((s) => s.clear) |
| 54 | const canUndo = useTableUndoStore((s) => (s.stacks[tableId]?.undo.length ?? 0) > 0) |
| 55 | const canRedo = useTableUndoStore((s) => (s.stacks[tableId]?.redo.length ?? 0) > 0) |
| 56 | |
| 57 | const updateRowMutation = useUpdateTableRow({ workspaceId, tableId }) |
| 58 | const batchCreateRowsMutation = useBatchCreateTableRows({ workspaceId, tableId }) |
| 59 | const batchUpdateRowsMutation = useBatchUpdateTableRows({ workspaceId, tableId }) |
| 60 | const deleteRowMutation = useDeleteTableRow({ workspaceId, tableId }) |
| 61 | const deleteRowsMutation = useDeleteTableRows({ workspaceId, tableId }) |
| 62 | const addColumnMutation = useAddTableColumn({ workspaceId, tableId }) |
| 63 | const updateColumnMutation = useUpdateColumn({ workspaceId, tableId }) |
| 64 | const deleteColumnMutation = useDeleteColumn({ workspaceId, tableId }) |
| 65 | const renameTableMutation = useRenameTable(workspaceId) |
| 66 | const updateMetadataMutation = useUpdateTableMetadata({ workspaceId, tableId }) |
| 67 | |
| 68 | const onColumnOrderChangeRef = useRef(onColumnOrderChange) |
| 69 | onColumnOrderChangeRef.current = onColumnOrderChange |
| 70 | const onColumnRenameRef = useRef(onColumnRename) |
| 71 | onColumnRenameRef.current = onColumnRename |
| 72 | const onColumnWidthsChangeRef = useRef(onColumnWidthsChange) |
| 73 | onColumnWidthsChangeRef.current = onColumnWidthsChange |
| 74 | const onPinnedColumnsChangeRef = useRef(onPinnedColumnsChange) |
| 75 | onPinnedColumnsChangeRef.current = onPinnedColumnsChange |
| 76 | const getPinnedColumnsRef = useRef(getPinnedColumns) |
| 77 | getPinnedColumnsRef.current = getPinnedColumns |
| 78 | const getColumnWidthsRef = useRef(getColumnWidths) |
| 79 | getColumnWidthsRef.current = getColumnWidths |
| 80 | |
| 81 | useEffect(() => { |
| 82 | return () => clear(tableId) |
| 83 | }, [clear, tableId]) |
| 84 | |
| 85 | const pushUndo = useCallback( |
| 86 | (action: TableUndoAction) => { |
| 87 | push(tableId, action) |
| 88 | }, |
| 89 | [push, tableId] |
| 90 | ) |
| 91 | |
| 92 | const executeAction = useCallback( |
| 93 | async (action: TableUndoAction, direction: 'undo' | 'redo') => { |
| 94 | try { |
| 95 | switch (action.type) { |