Skip to content

Commit 5c03acc

Browse files
waleedlatif1claude
andcommitted
fix(tables): merge column widths on delete-column undo, try/finally for auto-resize
- Delete-column undo now reads current column widths via getColumnWidths callback and merges the restored column's width into the full map, preventing other columns' widths from being wiped - Auto-resize measurement span is now wrapped in try/finally to ensure DOM cleanup if an exception occurs during measurement Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4005a15 commit 5c03acc

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table/table.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,14 @@ export function Table({
270270
})
271271
}, [])
272272

273+
const getColumnWidths = useCallback(() => columnWidthsRef.current, [])
274+
273275
const { pushUndo, undo, redo } = useTableUndo({
274276
workspaceId,
275277
tableId,
276278
onColumnOrderChange: handleColumnOrderChange,
277279
onColumnRename: handleColumnRename,
280+
getColumnWidths,
278281
})
279282
const undoRef = useRef(undo)
280283
undoRef.current = undo
@@ -757,20 +760,22 @@ export function Table({
757760
measure.style.cssText = 'position:absolute;visibility:hidden;white-space:nowrap;font:inherit'
758761
document.body.appendChild(measure)
759762

760-
measure.className = 'font-medium text-small'
761-
measure.textContent = columnName
762-
maxWidth = Math.max(maxWidth, measure.offsetWidth + 36)
763-
764-
measure.className = 'text-small'
765-
for (const row of currentRows) {
766-
const val = row.data[columnName]
767-
if (val == null) continue
768-
measure.textContent = String(val)
769-
maxWidth = Math.max(maxWidth, measure.offsetWidth + 17)
763+
try {
764+
measure.className = 'font-medium text-small'
765+
measure.textContent = columnName
766+
maxWidth = Math.max(maxWidth, measure.offsetWidth + 36)
767+
768+
measure.className = 'text-small'
769+
for (const row of currentRows) {
770+
const val = row.data[columnName]
771+
if (val == null) continue
772+
measure.textContent = String(val)
773+
maxWidth = Math.max(maxWidth, measure.offsetWidth + 17)
774+
}
775+
} finally {
776+
document.body.removeChild(measure)
770777
}
771778

772-
document.body.removeChild(measure)
773-
774779
const newWidth = Math.min(Math.ceil(maxWidth), 600)
775780
setColumnWidths((prev) => ({ ...prev, [columnName]: newWidth }))
776781
const updated = { ...columnWidthsRef.current, [columnName]: newWidth }

apps/sim/hooks/use-table-undo.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ interface UseTableUndoProps {
3636
tableId: string
3737
onColumnOrderChange?: (order: string[]) => void
3838
onColumnRename?: (oldName: string, newName: string) => void
39+
getColumnWidths?: () => Record<string, number>
3940
}
4041

4142
export function useTableUndo({
4243
workspaceId,
4344
tableId,
4445
onColumnOrderChange,
4546
onColumnRename,
47+
getColumnWidths,
4648
}: UseTableUndoProps) {
4749
const push = useTableUndoStore((s) => s.push)
4850
const popUndo = useTableUndoStore((s) => s.popUndo)
@@ -69,6 +71,8 @@ export function useTableUndo({
6971
onColumnOrderChangeRef.current = onColumnOrderChange
7072
const onColumnRenameRef = useRef(onColumnRename)
7173
onColumnRenameRef.current = onColumnRename
74+
const getColumnWidthsRef = useRef(getColumnWidths)
75+
getColumnWidthsRef.current = getColumnWidths
7276

7377
useEffect(() => {
7478
return () => clear(tableId)
@@ -230,7 +234,7 @@ export function useTableUndo({
230234
...(action.previousWidth !== null
231235
? {
232236
columnWidths: {
233-
...({} as Record<string, number>),
237+
...(getColumnWidthsRef.current?.() ?? {}),
234238
[action.columnName]: action.previousWidth,
235239
},
236240
}

0 commit comments

Comments
 (0)