From bb35e86dbbb1466bcec635e646838b28146beebb Mon Sep 17 00:00:00 2001 From: bchanot Date: Sat, 14 Nov 2026 18:52:41 +0100 Subject: [PATCH] fix(editor): five locals in stepHistory and MoreTabs, sync tag in tests --- apps/app/src/components/editor/EditorTabs.tsx | 22 +++++----- apps/app/src/editor/state/editorEdit.ts | 13 +++++- .../tests/unit/editor-finding-dialog.test.tsx | 4 +- apps/app/tests/unit/editor-history.test.tsx | 4 +- .../app/tests/unit/editor-session-tag.test.ts | 40 +++++++++---------- 5 files changed, 48 insertions(+), 35 deletions(-) diff --git a/apps/app/src/components/editor/EditorTabs.tsx b/apps/app/src/components/editor/EditorTabs.tsx index 797968fb..85c60993 100644 --- a/apps/app/src/components/editor/EditorTabs.tsx +++ b/apps/app/src/components/editor/EditorTabs.tsx @@ -148,28 +148,32 @@ function MoreList({ more, active, onPick }: MoreListProps) { } /** - * The panel of a tab picked in « Plus » takes the focus once that tab is the shown one: the - * entry that held it is gone with the list, so the focus would fall to the page. + * The choice of a tab in « Plus »: the menu closes, and the panel of the tab picked takes + * the focus once that tab is the shown one (the entry that held it is gone with the list, + * so the focus would fall to the page). */ -function usePanelFocus(active: EditorTabId) { +function usePanelPick( + active: EditorTabId, + onSelect: MoreTabsProps['onSelect'], + close: () => void, +) { const [wanted, setWanted] = useState(null) useEffect(() => { if (wanted === null || wanted !== active) return document.getElementById(panelDomId(active))?.focus() setWanted(null) }, [wanted, active]) - return setWanted + return (tab: EditorTabId) => { + if (onSelect(tab) !== false) setWanted(tab) + close() + } } /** Below 640 px: « Plus » and its dropdown of the tabs that do not fit. */ function MoreTabs({ more, active, onSelect }: MoreTabsProps) { const t = useT() const { open, close, toggle } = useMoreMenu(active) - const wantPanel = usePanelFocus(active) - const pick = (tab: EditorTabId) => { - if (onSelect(tab) !== false) wantPanel(tab) - close() - } + const pick = usePanelPick(active, onSelect, close) useMoreDismiss(open, close) return ( <> diff --git a/apps/app/src/editor/state/editorEdit.ts b/apps/app/src/editor/state/editorEdit.ts index 419d14ba..4f04b398 100644 --- a/apps/app/src/editor/state/editorEdit.ts +++ b/apps/app/src/editor/state/editorEdit.ts @@ -69,6 +69,16 @@ export function commit( return commitBatch(api, [command], accept) } +/** A step's patch with the finding it brought back, revealed (null when none). */ +function withReveal( + patch: Partial, + before: string, + after: string, +): Partial { + const key = appearedFinding(before, after) + return { ...patch, revealed: key ? { key } : null } +} + export function stepHistory( api: StoreApi, direction: 'undo' | 'redo', @@ -82,8 +92,7 @@ export function stepHistory( const replaced = preview.replaceDocument(step.text) if (!replaced.ok) return replaced const patch = replacedPatch(state, step.text, replaced.state, step.history) - const key = appearedFinding(state.documentText, step.text) - api.set({ ...patch, revealed: key ? { key } : null }) + api.set(withReveal(patch, state.documentText, step.text)) return { ok: true } } diff --git a/apps/app/tests/unit/editor-finding-dialog.test.tsx b/apps/app/tests/unit/editor-finding-dialog.test.tsx index 656e6100..3b9a4604 100644 --- a/apps/app/tests/unit/editor-finding-dialog.test.tsx +++ b/apps/app/tests/unit/editor-finding-dialog.test.tsx @@ -58,9 +58,9 @@ const back = () => act(async () => navigate(-1)) /** The session tag of USER in this tab: entries and flags carry it (criterion 22). */ let TAG: string -beforeEach(async () => { +beforeEach(() => { useLocaleStore.setState({ locale: 'fr' }) - TAG = await resolveSessionTag(USER) + TAG = resolveSessionTag(USER) }) afterEach(() => { cleanup() diff --git a/apps/app/tests/unit/editor-history.test.tsx b/apps/app/tests/unit/editor-history.test.tsx index 964b1cd4..709de793 100644 --- a/apps/app/tests/unit/editor-history.test.tsx +++ b/apps/app/tests/unit/editor-history.test.tsx @@ -56,8 +56,8 @@ const foreign = (patch: Partial) => ({ /** The session tag of USER in this tab (criterion 22). */ let TAG: string -beforeEach(async () => { - TAG = await resolveSessionTag(USER) +beforeEach(() => { + TAG = resolveSessionTag(USER) useLocaleStore.setState({ locale: 'fr' }) serverHas(emptyText(), 3, false) }) diff --git a/apps/app/tests/unit/editor-session-tag.test.ts b/apps/app/tests/unit/editor-session-tag.test.ts index 75b6d148..1ca0048a 100644 --- a/apps/app/tests/unit/editor-session-tag.test.ts +++ b/apps/app/tests/unit/editor-session-tag.test.ts @@ -28,8 +28,8 @@ afterEach(() => { }) describe('resolveSessionTag', () => { - it('draws a 128-bit hex tag, stored with the account fingerprint, never the id', async () => { - const tag = await resolveSessionTag('account-a') + it('draws a 128-bit hex tag, stored with the account fingerprint, never the id', () => { + const tag = resolveSessionTag('account-a') expect(tag).toMatch(/^[0-9a-f]{32}$/) const record = stored() expect(record?.tag).toBe(tag) @@ -37,37 +37,37 @@ describe('resolveSessionTag', () => { expect(sessionStorage.getItem(STORAGE_KEY)).not.toContain('account-a') }) - it('reuses the tag for the same account, after a reload too', async () => { - const tag = await resolveSessionTag('account-a') - expect(await resolveSessionTag('account-a')).toBe(tag) + it('reuses the tag for the same account, after a reload too', () => { + const tag = resolveSessionTag('account-a') + expect(resolveSessionTag('account-a')).toBe(tag) reload() - expect(await resolveSessionTag('account-a')).toBe(tag) + expect(resolveSessionTag('account-a')).toBe(tag) }) - it('rotates on another account and clears the flags of the old tag', async () => { - const old = await resolveSessionTag('account-a') + it('rotates on another account and clears the flags of the old tag', () => { + const old = resolveSessionTag('account-a') const flag = leftDraftKey({ sheetId: 'S', tag: old }) sessionStorage.setItem(flag, '{}') reload() - const tag = await resolveSessionTag('account-b') + const tag = resolveSessionTag('account-b') expect(tag).not.toBe(old) expect(sessionStorage.getItem(flag)).toBeNull() reload() - expect(await resolveSessionTag('account-a')).not.toBe(old) + expect(resolveSessionTag('account-a')).not.toBe(old) }) - it('keeps the tag in memory when the storage refuses: a reload restores nothing', async () => { + it('keeps the tag in memory when the storage refuses: a reload restores nothing', () => { vi.spyOn(sessionStorage, 'getItem').mockImplementation(() => { throw new Error('blocked') }) vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => { throw new Error('blocked') }) - const tag = await resolveSessionTag('account-a') + const tag = resolveSessionTag('account-a') expect(tag).toMatch(/^[0-9a-f]{32}$/) - expect(await resolveSessionTag('account-a')).toBe(tag) + expect(resolveSessionTag('account-a')).toBe(tag) reload() - expect(await resolveSessionTag('account-a')).not.toBe(tag) + expect(resolveSessionTag('account-a')).not.toBe(tag) }) it('works without crypto.subtle (http): kept on reload, rotated per account', () => { @@ -93,9 +93,9 @@ describe('resolveSessionTag', () => { expect(typeof resolveSessionTag('account-a')).toBe('string') }) - it('reads a tampered record as none', async () => { + it('reads a tampered record as none', () => { sessionStorage.setItem(STORAGE_KEY, '{"tag":"x","account":"y"}') - const tag = await resolveSessionTag('account-a') + const tag = resolveSessionTag('account-a') expect(tag).toMatch(/^[0-9a-f]{32}$/) expect(stored()?.tag).toBe(tag) }) @@ -137,14 +137,14 @@ describe('the sweep of flags keyed by an account id (older shape)', () => { }) describe('forgetSessionTag and the sign-out', () => { - it('rotates the tag of the same account and clears its flags', async () => { - const old = await resolveSessionTag('account-a') + it('rotates the tag of the same account and clears its flags', () => { + const old = resolveSessionTag('account-a') const flag = leftDraftKey({ sheetId: 'S', tag: old }) sessionStorage.setItem(flag, '{}') forgetSessionTag() expect(stored()).toBeNull() expect(sessionStorage.getItem(flag)).toBeNull() - expect(await resolveSessionTag('account-a')).not.toBe(old) + expect(resolveSessionTag('account-a')).not.toBe(old) }) it('follows the account: a sign-out forgets, a sign-in resolves', async () => { @@ -158,6 +158,6 @@ describe('forgetSessionTag and the sign-out', () => { expect(useSessionTagStore.getState().held).toBeNull() expect(stored()).toBeNull() stop() - expect(await resolveSessionTag('account-a')).not.toBe(old) + expect(resolveSessionTag('account-a')).not.toBe(old) }) })