feat(editor): browser history for tabs, sections and Aller a

EditorView held by the navigation reducer (restore_view), a history
adapter hook mounted in EditorWorkspace (push, replace, back, mount
rule, ownership, write budget, scroll map, refused POP), a guard stack
by layer, an announcer queue joined after open dialogs.
This commit is contained in:
bchanot committed 2026-11-13 17:57:52 +01:00
1 parent 69e225ca7a
commit 792394d3b2
11 files changed
+1268 -40

No files matched your search

@@ -0,0 +1,144 @@
/** R2F-1/2: guard stack, announcer queue, a Back refused while the citations panel is open. */
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
act,
cleanup,
render,
renderHook,
screen,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { ElementKey } from '@faunosteo/core-wasm/types'
import {
MemoryRouter,
useLocation,
useNavigate,
type Location,
type NavigateFunction,
} from 'react-router-dom'
import { flushAfterModals } from '@/components/ui/afterModalsClosed'
import { useBeforeUnload } from '@/app/useLeaveGuard'
import EditorTabs from '@/components/editor/EditorTabs'
import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry'
import useEditorHistory from '@/editor/history/useEditorHistory'
import { defaultView } from '@/editor/navigation'
import { useEditorNavigation } from '@/editor/useEditorNavigation'
import { useLocaleStore } from '@/state/localeStore'
afterEach(cleanup)
const owner = { sheetId: 'S', userId: 'U' }
const finding: ElementKey = {
kind: 'finding',
zone_id: 'zone',
path: ['family', 'node'],
}
let navigate: NavigateFunction
let location: Location
const entry = () => (location.state as Record<string, HistoryEntry>)[ENTRY_KEY]
function Page({ guard }: { guard?: () => { allowed: boolean } }) {
const nav = useEditorNavigation('general')
navigate = useNavigate()
location = useLocation()
useEditorHistory(nav, owner, defaultView('general'))
return (
<>
<EditorTabs active={nav.state.tab} tier="wide" onSelect={nav.selectTab} />
<button onClick={() => nav.openCitations(finding)}>cite</button>
<button onClick={() => nav.registerCanLeave(guard ?? null)}>guard</button>
<button onClick={() => nav.announce('one')}>say one</button>
<button onClick={() => nav.announce('two')}>say two</button>
<p data-testid="live">{nav.live}</p>
</>
)
}
const mount = (guard?: () => { allowed: boolean }) =>
render(
<MemoryRouter initialEntries={['/x']}>
<Page guard={guard} />
</MemoryRouter>,
)
describe('a Back while the citations panel is open', () => {
it('keeps the page where it is, asks the dirty layer, and pushes the view again', async () => {
useLocaleStore.setState({ locale: 'fr' })
const user = userEvent.setup()
const guard = vi.fn(() => ({ allowed: false }))
mount(guard)
await user.click(screen.getByRole('button', { name: 'guard' }))
await user.click(screen.getByRole('tab', { name: 'Constats' }))
await user.click(screen.getByRole('button', { name: 'cite' }))
const before = entry().id
act(() => navigate(-1))
expect(guard).toHaveBeenCalledWith(false)
expect(screen.getByRole('tab', { selected: true })).toHaveTextContent(
'Constats',
)
expect(entry().id).not.toBe(before)
expect(entry().view.tab).toBe('findings')
expect(entry().below?.tab).toBe('general')
})
})
describe('the guard stack', () => {
it('asks each layer on its own, and all of them top down', () => {
const { result } = renderHook(() => useEditorNavigation('general'))
const { guards } = result.current
const order: string[] = []
guards.register('citations', () => {
order.push('citations')
return { allowed: true, note: 'a' }
})
guards.register('finding', () => {
order.push('finding')
return { allowed: false, note: 'b' }
})
expect(guards.ask('reference', false)).toEqual({ allowed: true })
expect(guards.ask('finding', true).allowed).toBe(false)
order.length = 0
expect(guards.askAll(false)).toEqual({ allowed: false, note: 'b. a' })
expect(order).toEqual(['finding', 'citations'])
guards.register('finding', null)
expect(guards.askAll(false).allowed).toBe(true)
})
})
describe('the announcer', () => {
it('says what is announced at once while no dialog is open', async () => {
mount()
await userEvent.click(screen.getByRole('button', { name: 'say one' }))
expect(screen.getByTestId('live')).toHaveTextContent('one')
})
it('joins what is announced under an open dialog into one announcement', async () => {
const user = userEvent.setup()
mount()
const dialog = document.createElement('dialog')
dialog.setAttribute('open', '')
document.body.append(dialog)
await user.click(screen.getByRole('button', { name: 'say one' }))
await user.click(screen.getByRole('button', { name: 'say two' }))
expect(screen.getByTestId('live')).toHaveTextContent('')
dialog.remove()
act(() => flushAfterModals())
expect(screen.getByTestId('live')).toHaveTextContent('one. two')
})
})
describe('useBeforeUnload', () => {
it('asks while dirty and stops when clean', () => {
const { rerender } = renderHook(({ dirty }) => useBeforeUnload(dirty), {
initialProps: { dirty: true },
})
const first = new Event('beforeunload', { cancelable: true })
window.dispatchEvent(first)
expect(first.defaultPrevented).toBe(true)
rerender({ dirty: false })
const second = new Event('beforeunload', { cancelable: true })
window.dispatchEvent(second)
expect(second.defaultPrevented).toBe(false)
})
})