Skip to content

Commit 2790197

Browse files
committed
fix(tables): define mergePagePreservingIdentity helper used in polling cache merge
1 parent dbf44d5 commit 2790197

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

apps/sim/hooks/queries/tables.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,26 @@ export function useTableRows({
248248
})
249249
}
250250

251+
/** Merges a freshly-fetched page into the cached page while preserving row
252+
* object identity for unchanged rows (by `updatedAt`). Returns the original
253+
* `prev` reference unchanged when nothing has actually changed — callers can
254+
* use a `===` check to skip a `setQueryData` write entirely. */
255+
function mergePagePreservingIdentity(
256+
prev: TableRowsResponse,
257+
fresh: TableRowsResponse
258+
): TableRowsResponse {
259+
if (prev.totalCount !== fresh.totalCount || prev.rows.length !== fresh.rows.length) return fresh
260+
const prevById = new Map(prev.rows.map((r) => [r.id, r]))
261+
let allSame = true
262+
const nextRows = fresh.rows.map((freshRow) => {
263+
const prevRow = prevById.get(freshRow.id)
264+
if (prevRow && String(prevRow.updatedAt) === String(freshRow.updatedAt)) return prevRow
265+
allSame = false
266+
return freshRow
267+
})
268+
return allSame ? prev : { ...fresh, rows: nextRows }
269+
}
270+
251271
export function tableRowsParamsKey({
252272
pageSize,
253273
filter,

0 commit comments

Comments
 (0)