@@ -27,6 +27,7 @@ import {
2727 useDeleteTableRows ,
2828 useUpdateTableRow ,
2929} from '@/hooks/queries/tables'
30+ import { useTableUndoStore } from '@/stores/table/store'
3031
3132const logger = createLogger ( 'RowModal' )
3233
@@ -92,6 +93,7 @@ export function RowModal({ mode, isOpen, onClose, table, row, rowIds, onSuccess
9293 const updateRowMutation = useUpdateTableRow ( { workspaceId, tableId } )
9394 const deleteRowMutation = useDeleteTableRow ( { workspaceId, tableId } )
9495 const deleteRowsMutation = useDeleteTableRows ( { workspaceId, tableId } )
96+ const pushToUndoStack = useTableUndoStore ( ( s ) => s . push )
9597 const isSubmitting =
9698 createRowMutation . isPending ||
9799 updateRowMutation . isPending ||
@@ -106,9 +108,24 @@ export function RowModal({ mode, isOpen, onClose, table, row, rowIds, onSuccess
106108 const cleanData = cleanRowData ( columns , rowData )
107109
108110 if ( mode === 'add' ) {
109- await createRowMutation . mutateAsync ( { data : cleanData } )
111+ const response = await createRowMutation . mutateAsync ( { data : cleanData } )
112+ const createdRow = ( response as { data ?: { row ?: { id ?: string ; position ?: number } } } )
113+ ?. data ?. row
114+ if ( createdRow ?. id ) {
115+ pushToUndoStack ( tableId , {
116+ type : 'create-row' ,
117+ rowId : createdRow . id ,
118+ position : createdRow . position ?? 0 ,
119+ data : cleanData ,
120+ } )
121+ }
110122 } else if ( mode === 'edit' && row ) {
123+ const oldData = row . data as Record < string , unknown >
111124 await updateRowMutation . mutateAsync ( { rowId : row . id , data : cleanData } )
125+ pushToUndoStack ( tableId , {
126+ type : 'update-cells' ,
127+ cells : [ { rowId : row . id , oldData, newData : cleanData } ] ,
128+ } )
112129 }
113130
114131 onSuccess ( )
@@ -124,8 +141,14 @@ export function RowModal({ mode, isOpen, onClose, table, row, rowIds, onSuccess
124141 const idsToDelete = rowIds ?? ( row ? [ row . id ] : [ ] )
125142
126143 try {
127- if ( idsToDelete . length === 1 ) {
144+ if ( idsToDelete . length === 1 && row ) {
128145 await deleteRowMutation . mutateAsync ( idsToDelete [ 0 ] )
146+ pushToUndoStack ( tableId , {
147+ type : 'delete-rows' ,
148+ rows : [
149+ { rowId : row . id , data : row . data as Record < string , unknown > , position : row . position } ,
150+ ] ,
151+ } )
129152 } else {
130153 await deleteRowsMutation . mutateAsync ( idsToDelete )
131154 }
0 commit comments