@@ -2,7 +2,12 @@ import { useCallback, useEffect, useRef } from 'react'
22import { toast } from '@sim/emcn'
33import { createLogger } from '@sim/logger'
44import { TABLE_LIMITS } from '@/lib/table/constants'
5- import { TABLE_LOCK_FLAGS , type TableLockKind , type TableLocks } from '@/lib/table/types'
5+ import {
6+ TABLE_LOCK_FLAGS ,
7+ type TableLockKind ,
8+ type TableLocks ,
9+ type TableMetadata ,
10+ } from '@/lib/table/types'
611import {
712 useAddTableColumn ,
813 useBatchCreateTableRows ,
@@ -98,6 +103,13 @@ interface UseTableUndoProps {
98103 onPinnedColumnsChange ?: ( pinned : string [ ] ) => void
99104 getPinnedColumns ?: ( ) => string [ ]
100105 getColumnWidths ?: ( ) => Record < string , number >
106+ /**
107+ * Sink for column-layout writes (order, widths, pinning). Defaults to the
108+ * table's shared metadata; with a saved view active the caller passes the
109+ * view's sink instead, so undoing a reorder unwinds it where the original
110+ * reorder was stored rather than silently rewriting the "All" layout.
111+ */
112+ onPersistLayout ?: ( patch : TableMetadata ) => void
101113}
102114
103115export function useTableUndo ( {
@@ -110,6 +122,7 @@ export function useTableUndo({
110122 onPinnedColumnsChange,
111123 getPinnedColumns,
112124 getColumnWidths,
125+ onPersistLayout,
113126} : UseTableUndoProps ) {
114127 const push = useTableUndoStore ( ( s ) => s . push )
115128 const popUndo = useTableUndoStore ( ( s ) => s . popUndo )
@@ -131,6 +144,9 @@ export function useTableUndo({
131144 const renameTableMutation = useRenameTable ( workspaceId )
132145 const updateMetadataMutation = useUpdateTableMetadata ( { workspaceId, tableId } )
133146
147+ const persistLayoutRef = useRef ( onPersistLayout ?? updateMetadataMutation . mutate )
148+ persistLayoutRef . current = onPersistLayout ?? updateMetadataMutation . mutate
149+
134150 const onColumnOrderChangeRef = useRef ( onColumnOrderChange )
135151 onColumnOrderChangeRef . current = onColumnOrderChange
136152 const onColumnRenameRef = useRef ( onColumnRename )
@@ -293,7 +309,7 @@ export function useTableUndo({
293309 if ( direction === 'undo' ) {
294310 deleteColumnMutation . mutate ( colKey , {
295311 onSuccess : ( ) => {
296- const metadata : Record < string , unknown > = { }
312+ const metadata : TableMetadata = { }
297313 const currentWidths = getColumnWidthsRef . current ?.( ) ?? { }
298314 if ( colKey in currentWidths ) {
299315 const { [ colKey ] : _ , ...rest } = currentWidths
@@ -307,7 +323,7 @@ export function useTableUndo({
307323 metadata . pinnedColumns = newPinned
308324 }
309325 if ( Object . keys ( metadata ) . length > 0 ) {
310- updateMetadataMutation . mutate ( metadata )
326+ persistLayoutRef . current ( metadata )
311327 }
312328 } ,
313329 } )
@@ -368,7 +384,7 @@ export function useTableUndo({
368384 }
369385 } ) ( )
370386 }
371- const metadata : Record < string , unknown > = { }
387+ const metadata : TableMetadata = { }
372388 if ( action . previousOrder ) {
373389 onColumnOrderChangeRef . current ?.( action . previousOrder )
374390 metadata . columnOrder = action . previousOrder
@@ -399,15 +415,15 @@ export function useTableUndo({
399415 }
400416 }
401417 if ( Object . keys ( metadata ) . length > 0 ) {
402- updateMetadataMutation . mutate ( metadata )
418+ persistLayoutRef . current ( metadata )
403419 }
404420 } ,
405421 }
406422 )
407423 } else {
408424 deleteColumnMutation . mutate ( colKey , {
409425 onSuccess : ( ) => {
410- const metadata : Record < string , unknown > = { }
426+ const metadata : TableMetadata = { }
411427 if ( action . previousOrder ) {
412428 const newOrder = action . previousOrder . filter ( ( n ) => n !== colKey )
413429 onColumnOrderChangeRef . current ?.( newOrder )
@@ -428,7 +444,7 @@ export function useTableUndo({
428444 }
429445 }
430446 if ( Object . keys ( metadata ) . length > 0 ) {
431- updateMetadataMutation . mutate ( metadata )
447+ persistLayoutRef . current ( metadata )
432448 }
433449 } ,
434450 } )
@@ -491,7 +507,7 @@ export function useTableUndo({
491507 ]
492508 }
493509 onColumnOrderChangeRef . current ?.( order )
494- updateMetadataMutation . mutate ( { columnOrder : order } )
510+ persistLayoutRef . current ( { columnOrder : order } )
495511 break
496512 }
497513 }
0 commit comments