@@ -222,19 +222,22 @@ export function useTableUndo({
222222 }
223223
224224 case 'create-column' : {
225+ // Identity (delete lookup + id-keyed metadata) uses the stable id;
226+ // re-create uses the display name.
227+ const colKey = action . columnId ?? action . columnName
225228 if ( direction === 'undo' ) {
226- deleteColumnMutation . mutate ( action . columnName , {
229+ deleteColumnMutation . mutate ( colKey , {
227230 onSuccess : ( ) => {
228231 const metadata : Record < string , unknown > = { }
229232 const currentWidths = getColumnWidthsRef . current ?.( ) ?? { }
230- if ( action . columnName in currentWidths ) {
231- const { [ action . columnName ] : _ , ...rest } = currentWidths
233+ if ( colKey in currentWidths ) {
234+ const { [ colKey ] : _ , ...rest } = currentWidths
232235 onColumnWidthsChangeRef . current ?.( rest )
233236 metadata . columnWidths = rest
234237 }
235238 const currentPinned = getPinnedColumnsRef . current ?.( ) ?? [ ]
236- if ( currentPinned . includes ( action . columnName ) ) {
237- const newPinned = currentPinned . filter ( ( n ) => n !== action . columnName )
239+ if ( currentPinned . includes ( colKey ) ) {
240+ const newPinned = currentPinned . filter ( ( n ) => n !== colKey )
238241 onPinnedColumnsChangeRef . current ?.( newPinned )
239242 metadata . pinnedColumns = newPinned
240243 }
@@ -245,6 +248,7 @@ export function useTableUndo({
245248 } )
246249 } else {
247250 addColumnMutation . mutate ( {
251+ ...( action . columnId ? { id : action . columnId } : { } ) ,
248252 name : action . columnName ,
249253 type : 'string' ,
250254 position : action . position ,
@@ -254,9 +258,15 @@ export function useTableUndo({
254258 }
255259
256260 case 'delete-column' : {
261+ // Identity (cell-data keys, id-keyed metadata, delete lookup) uses the
262+ // stable id; re-create uses the display name.
263+ const colKey = action . columnId ?? action . columnName
257264 if ( direction === 'undo' ) {
258265 addColumnMutation . mutate (
259266 {
267+ // Reuse the original id so the saved (id-keyed) cell data below
268+ // lands on the restored column.
269+ ...( action . columnId ? { id : action . columnId } : { } ) ,
260270 name : action . columnName ,
261271 type : action . columnType ,
262272 required : action . columnRequired ,
@@ -268,7 +278,7 @@ export function useTableUndo({
268278 if ( action . cellData . length > 0 ) {
269279 const updates = action . cellData . map ( ( c ) => ( {
270280 rowId : c . rowId ,
271- data : { [ action . columnName ] : c . value } ,
281+ data : { [ colKey ] : c . value } ,
272282 } ) )
273283 void ( async ( ) => {
274284 try {
@@ -297,26 +307,22 @@ export function useTableUndo({
297307 if ( action . previousWidth !== null ) {
298308 const merged = {
299309 ...( getColumnWidthsRef . current ?.( ) ?? { } ) ,
300- [ action . columnName ] : action . previousWidth ,
310+ [ colKey ] : action . previousWidth ,
301311 }
302312 metadata . columnWidths = merged
303313 onColumnWidthsChangeRef . current ?.( merged )
304314 }
305315 if ( action . previousPinnedColumns !== null ) {
306- const wasColumnPinned = action . previousPinnedColumns . includes (
307- action . columnName
308- )
316+ const wasColumnPinned = action . previousPinnedColumns . includes ( colKey )
309317 if ( wasColumnPinned ) {
310318 const currentPinned = getPinnedColumnsRef . current ?.( ) ?? [ ]
311- if ( ! currentPinned . includes ( action . columnName ) ) {
312- const insertIndex = action . previousPinnedColumns . indexOf (
313- action . columnName
314- )
319+ if ( ! currentPinned . includes ( colKey ) ) {
320+ const insertIndex = action . previousPinnedColumns . indexOf ( colKey )
315321 const restoredPinned = [ ...currentPinned ]
316322 restoredPinned . splice (
317323 Math . min ( insertIndex , restoredPinned . length ) ,
318324 0 ,
319- action . columnName
325+ colKey
320326 )
321327 onPinnedColumnsChangeRef . current ?.( restoredPinned )
322328 metadata . pinnedColumns = restoredPinned
@@ -330,24 +336,24 @@ export function useTableUndo({
330336 }
331337 )
332338 } else {
333- deleteColumnMutation . mutate ( action . columnName , {
339+ deleteColumnMutation . mutate ( colKey , {
334340 onSuccess : ( ) => {
335341 const metadata : Record < string , unknown > = { }
336342 if ( action . previousOrder ) {
337- const newOrder = action . previousOrder . filter ( ( n ) => n !== action . columnName )
343+ const newOrder = action . previousOrder . filter ( ( n ) => n !== colKey )
338344 onColumnOrderChangeRef . current ?.( newOrder )
339345 metadata . columnOrder = newOrder
340346 }
341347 if ( action . previousWidth !== null ) {
342348 const currentWidths = getColumnWidthsRef . current ?.( ) ?? { }
343- const { [ action . columnName ] : _ , ...rest } = currentWidths
349+ const { [ colKey ] : _ , ...rest } = currentWidths
344350 metadata . columnWidths = rest
345351 onColumnWidthsChangeRef . current ?.( rest )
346352 }
347353 if ( action . previousPinnedColumns !== null ) {
348354 const currentPinned = getPinnedColumnsRef . current ?.( ) ?? [ ]
349- if ( currentPinned . includes ( action . columnName ) ) {
350- const newPinned = currentPinned . filter ( ( n ) => n !== action . columnName )
355+ if ( currentPinned . includes ( colKey ) ) {
356+ const newPinned = currentPinned . filter ( ( n ) => n !== colKey )
351357 onPinnedColumnsChangeRef . current ?.( newPinned )
352358 metadata . pinnedColumns = newPinned
353359 }
@@ -364,11 +370,14 @@ export function useTableUndo({
364370 case 'rename-column' : {
365371 const fromName = direction === 'undo' ? action . newName : action . oldName
366372 const toName = direction === 'undo' ? action . oldName : action . newName
373+ // Look up by the stable id (falls back to the current name) so undo
374+ // never renames the column to its internal id.
375+ const colKey = action . columnId ?? fromName
367376 updateColumnMutation . mutate ( {
368- columnName : fromName ,
377+ columnName : colKey ,
369378 updates : { name : toName } ,
370379 } )
371- onColumnRenameRef . current ?.( fromName , toName )
380+ onColumnRenameRef . current ?.( colKey , toName )
372381 break
373382 }
374383
0 commit comments