Skip to content

Commit 62a2297

Browse files
committed
fix(tables): clamp the find step base when a refetch shrinks the match set
1 parent 7599a53 commit 62a2297

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

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

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,15 +1340,28 @@ export function TableGrid({
13401340
* silently step over the very match the user pressed Enter to reach, and it
13411341
* would only come back around after wrapping the whole list.
13421342
*/
1343+
/**
1344+
* The index the next step counts from, clamped into the CURRENT match set.
1345+
*
1346+
* A row write or SSE update can shrink or reorder the matches for a term the
1347+
* user is still navigating; the term latch deliberately leaves the cursor
1348+
* alone in that case, so the stored index can now point past the end. Stepping
1349+
* from it would wrap off a stale base and land somewhere unrelated to the
1350+
* match on screen. Clamping here rather than in the two callers keeps the
1351+
* stepping base and the displayed index in agreement.
1352+
*/
1353+
const stepBaseIndex = () =>
1354+
Math.min(currentMatchIndexRef.current, Math.max(0, findMatchesRef.current.length - 1))
1355+
13431356
const handleFindNext = useCallback(() => {
13441357
if (!findResultsAreCurrentRef.current) return
1345-
const index = currentMatchIndexRef.current
1358+
const index = stepBaseIndex()
13461359
goToMatch(cursorIsOnMatchRef.current ? index + 1 : index)
13471360
}, [goToMatch])
13481361

13491362
const handleFindPrev = useCallback(() => {
13501363
if (!findResultsAreCurrentRef.current) return
1351-
const index = currentMatchIndexRef.current
1364+
const index = stepBaseIndex()
13521365
goToMatch(cursorIsOnMatchRef.current ? index - 1 : index)
13531366
}, [goToMatch])
13541367

0 commit comments

Comments
 (0)