From 85cf87695c952350dad1b83baccec81404c8529c Mon Sep 17 00:00:00 2001 From: bchanot Date: Sat, 21 Nov 2026 20:15:17 +0100 Subject: [PATCH] fix(editor): the view row reveals the active chip when shown again --- .../components/editor/annotations/viewRow.ts | 20 +++++++++++-- apps/app/tests/unit/editor-view-row.test.tsx | 28 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/apps/app/src/components/editor/annotations/viewRow.ts b/apps/app/src/components/editor/annotations/viewRow.ts index 70b83e1e..9f27a11e 100644 --- a/apps/app/src/components/editor/annotations/viewRow.ts +++ b/apps/app/src/components/editor/annotations/viewRow.ts @@ -110,10 +110,19 @@ export function useViewRow( const row = rowRef.current if (!row) return const chip = chipOf(row, current) - setHints(hintState(row, chip && spanOf(row, chip))) + const next = hintState(row, chip && spanOf(row, chip)) + setHints((prev) => + prev.start === next.start && + prev.end === next.end && + prev.dot === next.dot + ? prev + : next, + ) }, [rowRef, current]) const refreshRef = useRef(refresh) refreshRef.current = refresh + const currentRef = useRef(current) + currentRef.current = current const reveal = useCallback( (id: string) => { @@ -138,6 +147,9 @@ export function useViewRow( [rowRef, reduced], ) + const revealRef = useRef(reveal) + revealRef.current = reveal + useLayoutEffect(() => { openWindow() return () => clearTimeout(openingTimer.current) @@ -160,7 +172,11 @@ export function useViewRow( return () => row.removeEventListener('scroll', onScroll) let width = row.clientWidth const observer = new ResizeObserver(() => { - if (width === 0 && row.clientWidth > 0) openWindow() + if (width === 0 && row.clientWidth > 0) { + openWindow() + // A view that changed while the row was hidden had nothing to reveal. + revealRef.current(currentRef.current) + } width = row.clientWidth refreshRef.current() }) diff --git a/apps/app/tests/unit/editor-view-row.test.tsx b/apps/app/tests/unit/editor-view-row.test.tsx index cefee7b2..d538ddea 100644 --- a/apps/app/tests/unit/editor-view-row.test.tsx +++ b/apps/app/tests/unit/editor-view-row.test.tsx @@ -218,6 +218,34 @@ describe('the reveal of the active chip', () => { expect(scrollTo).toHaveBeenCalledWith({ left: 272, behavior: 'auto' }) }) + it('reveals the active chip when the row is shown again', () => { + const original = globalThis.ResizeObserver + const observers: Array<() => void> = [] + vi.stubGlobal( + 'ResizeObserver', + class { + constructor(callback: () => void) { + observers.push(callback) + } + observe() {} + disconnect() {} + }, + ) + vi.useFakeTimers() + const { again } = renderRow(vi.fn(), viewsOf(), 'v1') + act(() => void vi.advanceTimersByTime(OPENING_MS + 1)) + // Hidden: width 0, the change reveals nothing. + stubGeometry(0, 0) + again({ current: 'v5' }) + expect(scrollTo).not.toHaveBeenCalled() + // Shown again: the chip (400 to 500) is past the end hint of a 300 px row. + stubGeometry() + act(() => observers.forEach((callback) => callback())) + expect(scrollTo).toHaveBeenCalledTimes(1) + expect(scrollTo).toHaveBeenCalledWith({ left: 272, behavior: 'auto' }) + globalThis.ResizeObserver = original + }) + it('shows the end hint again when a name change widens the content', () => { const { container } = render( ,