Skip to content

Commit db183b5

Browse files
committed
fix(tables): cap the Add-to-Chat label at the rows a chip can carry
Cursor Bugbot: last round's fix for the label undercounting rows introduced the opposite error. addToChatRowCount passed the raw selection size, but buildTableSelectionContext caps rowIds at MAX_TABLE_SELECTION_ROWS, so a 2,000-row selection advertised 2,000 while the chip referenced 500 — breaking the same invariant the fix claimed to establish. Routes the count through a chipRowCount helper next to the builder that applies the cap, so the label cannot drift from the payload again. utils.test.ts now asserts the invariant directly rather than the formula: across 1, 42, 500, 750 and 50,000 requested rows, chipRowCount equals the rowIds length the context actually carries. Verified to fail if the cap is dropped.
1 parent dac8313 commit db183b5

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
type CellCoord,
7070
canWriteRowsWithChip,
7171
checkboxColLayout,
72+
chipRowCount,
7273
classifyExecStatusMix,
7374
collectRowSnapshots,
7475
computeNormalizedSelection,
@@ -4592,7 +4593,9 @@ export function TableGrid({
45924593
disableDelete={!canDeleteRow}
45934594
onAddToChat={addToChatRowIds.length > 0 ? handleAddSelectionToChat : undefined}
45944595
addToChatCellScoped={Boolean(contextMenuColumnIds)}
4595-
addToChatRowCount={contextMenuIsSelectAll ? selectedRowCount : addToChatRowIds.length}
4596+
addToChatRowCount={chipRowCount(
4597+
contextMenuIsSelectAll ? selectedRowCount : addToChatRowIds.length
4598+
)}
45964599
/>
45974600

45984601
<ExpandedCellPopover

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/utils.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import {
88
} from '@/lib/copilot/chat/selection-context'
99
import { TABLE_LIMITS } from '@/lib/table/constants'
1010
import type { DisplayColumn } from './types'
11-
import { buildTableSelectionContext, canWriteRowsWithChip, selectedColumnIds } from './utils'
11+
import {
12+
buildTableSelectionContext,
13+
canWriteRowsWithChip,
14+
chipRowCount,
15+
selectedColumnIds,
16+
} from './utils'
1217

1318
function columns(count: number): DisplayColumn[] {
1419
return Array.from({ length: count }, (_, i) => ({
@@ -107,3 +112,22 @@ describe('canWriteRowsWithChip', () => {
107112
expect(canWriteRowsWithChip({ ...ok, rowCount: TABLE_LIMITS.MAX_COPY_ROWS + 1 })).toBe(false)
108113
})
109114
})
115+
116+
describe('chipRowCount', () => {
117+
it.each([1, 42, MAX_TABLE_SELECTION_ROWS, MAX_TABLE_SELECTION_ROWS + 250, 50_000])(
118+
'agrees with what a context carries for %i requested rows',
119+
(requested) => {
120+
// The invariant that keeps breaking: a label derived from the raw
121+
// selection size over-promises once the context caps its rowIds, and one
122+
// derived from the loaded page under-promises. Both must equal this.
123+
const context = buildTableSelectionContext({
124+
tableId: 't1',
125+
tableName: 'Sales',
126+
rowIds: rowIds(requested),
127+
})
128+
129+
if (context?.kind !== 'table_selection') throw new Error('expected a table_selection')
130+
expect(chipRowCount(requested)).toBe(context.rowIds.length)
131+
}
132+
)
133+
})

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,15 @@ export function buildTableSelectionContext(opts: {
404404
}
405405
}
406406

407+
/**
408+
* Rows a chip will actually reference for a selection of `requested` rows —
409+
* {@link buildTableSelectionContext} caps its `rowIds`, so any count shown to
410+
* the user must pass through here or the UI promises more than it sends.
411+
*/
412+
export function chipRowCount(requested: number): number {
413+
return Math.min(requested, MAX_TABLE_SELECTION_ROWS)
414+
}
415+
407416
/**
408417
* Whether a copy can be written synchronously on the event — the only way a
409418
* chat-selection chip survives, since the paged path's async Clipboard API write

0 commit comments

Comments
 (0)