Skip to content

Commit cfb1da5

Browse files
fix(tables): make in-grid find id-native (scan/return JSONB keys as column ids)
1 parent 98c883c commit cfb1da5

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,15 @@ export function TableGrid({
762762
const findMatches = useMemo<readonly TableFindMatch[]>(() => {
763763
const raw = findData?.matches
764764
if (!raw || raw.length === 0) return EMPTY_FIND_MATCHES
765-
const colIndexByName = new Map(displayColumns.map((c, i) => [c.name, i]))
765+
// `m.column` is the stable column id (the JSONB storage key); index display
766+
// columns by their id so id-native tables resolve and stale/hidden columns drop.
767+
const colIndexByKey = new Map(displayColumns.map((c, i) => [c.key, i]))
766768
return raw
767-
.filter((m) => colIndexByName.has(m.column))
769+
.filter((m) => colIndexByKey.has(m.column))
768770
.sort(
769771
(a, b) =>
770772
a.ordinal - b.ordinal ||
771-
(colIndexByName.get(a.column) ?? 0) - (colIndexByName.get(b.column) ?? 0)
773+
(colIndexByKey.get(a.column) ?? 0) - (colIndexByKey.get(b.column) ?? 0)
772774
)
773775
}, [findData, displayColumns])
774776

@@ -809,7 +811,7 @@ export function TableGrid({
809811
if (!match) return
810812
const rowIndex = rows.findIndex((r) => r.id === match.rowId)
811813
if (rowIndex === -1) return
812-
const colIndex = displayColumns.findIndex((c) => c.name === match.column)
814+
const colIndex = displayColumns.findIndex((c) => c.key === match.column)
813815
pendingMatchRef.current = null
814816
if (colIndex === -1) return
815817
setEditingCell(null)

apps/sim/lib/api/contracts/tables.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ export const findTableRowsQuerySchema = z.object({
523523
export const tableFindMatchSchema = z.object({
524524
ordinal: z.number().int(),
525525
rowId: z.string(),
526+
/** Stable column id of the matching cell (JSONB storage key), not the display name. */
526527
column: z.string(),
527528
})
528529

apps/sim/lib/table/service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,7 @@ export interface FindRowMatch {
24292429
/** 0-based index of the row in the filtered+sorted view (aligns with the list query). */
24302430
ordinal: number
24312431
rowId: string
2432+
/** Stable column id of the matching cell (the JSONB storage key), not the display name. */
24322433
column: string
24332434
}
24342435

@@ -2454,8 +2455,9 @@ export async function findRowMatches(
24542455
): Promise<{ matches: FindRowMatch[]; truncated: boolean }> {
24552456
const tableName = USER_TABLE_ROWS_SQL_NAME
24562457
const columns = table.schema.columns
2457-
const columnNames = columns.map((c) => c.name)
2458-
if (columnNames.length === 0) return { matches: [], truncated: false }
2458+
// Row data is keyed by stable column id, so scan/return JSONB keys as ids.
2459+
const columnIds = columns.map(getColumnId)
2460+
if (columnIds.length === 0) return { matches: [], truncated: false }
24592461

24602462
const baseConditions = and(
24612463
eq(userTableRows.tableId, table.id),
@@ -2484,7 +2486,7 @@ export async function findRowMatches(
24842486
FROM ordered o
24852487
CROSS JOIN LATERAL jsonb_each_text(o.data) kv
24862488
WHERE kv.value ILIKE ${pattern}
2487-
AND ${inArray(sql`kv.key`, columnNames)}
2489+
AND ${inArray(sql`kv.key`, columnIds)}
24882490
ORDER BY o.ordinal
24892491
LIMIT ${FIND_MATCH_LIMIT + 1}
24902492
`)

0 commit comments

Comments
 (0)