diff --git a/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx b/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx index 6be2c76a..aeeef8eb 100644 --- a/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx +++ b/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx @@ -59,13 +59,14 @@ function useReadOnly(mode: EditorMode, canEdit: boolean): boolean { */ /** * The drawing guard (R-12) as the shell's own question: a link of the shell and the - * sign-out ask « Abandonner le déplacement ? » before the sheet-level question. A started - * move also guards a reload or a tab close; one with nothing started arms nothing. + * sign-out ask « Abandonner le déplacement ? » (or « … la création ? ») before the + * sheet-level question, and a link is intercepted only while such work is at stake. A + * started move also guards a reload or a tab close; one with nothing started arms nothing. */ function useDrawingLeaveAsk() { const setAsk = useLeaveGuardStore((s) => s.setAsk) useEffect(() => { - setAsk(guardDrawing) + setAsk(guardDrawing, () => guardWork(useAnnotationWork.getState()) !== null) return () => setAsk(null) }, [setAsk]) useBeforeUnload(useAnnotationWork((work) => guardWork(work) !== null)) diff --git a/apps/app/src/components/shell/AppLayout.tsx b/apps/app/src/components/shell/AppLayout.tsx index 0a8ae188..f0208428 100644 --- a/apps/app/src/components/shell/AppLayout.tsx +++ b/apps/app/src/components/shell/AppLayout.tsx @@ -11,7 +11,10 @@ import { useT } from '@/i18n/useT' /** The draft editor takes the whole width (board 04's three columns). */ const DRAFT_EDITOR_PATH = '/app/admin/sheets/:sheetId/draft' -/** An in-app link a plain click would follow: same origin, same tab, no modifier key. */ +/** + * An in-app link a plain click would follow: same origin, same tab, no modifier key, not + * a download (it leaves no page). + */ function followedLink(event: MouseEvent): HTMLAnchorElement | null { if (event.defaultPrevented || event.button !== 0) return null if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) @@ -22,6 +25,7 @@ function followedLink(event: MouseEvent): HTMLAnchorElement | null { if ( !anchor || anchor.target === '_blank' || + anchor.hasAttribute('download') || anchor.origin !== window.location.origin ) return null @@ -44,11 +48,12 @@ export default function AppLayout() { const arm = useLeaveGuardStore((s) => s.arm) const [leaveTo, setLeaveTo] = useState(null) - // The page's own question first (a started move on the drawing, R-12), then the - // unsaved-changes one; the link is followed by the router once both let it go. + // The page's own question first (a started move or a creation on the drawing, R-12), + // then the unsaved-changes one; the link is followed by the router once both let it go. + // With nothing at stake the click is left to the link itself. function askBeforeLeaving(event: MouseEvent) { - const { ask } = useLeaveGuardStore.getState() - if (!guarded && !ask) return + const { atStake } = useLeaveGuardStore.getState() + if (!guarded && !atStake?.()) return const anchor = followedLink(event) if (!anchor) return event.preventDefault() diff --git a/apps/app/src/state/leaveGuardStore.ts b/apps/app/src/state/leaveGuardStore.ts index 03023fc2..f6ffd7dd 100644 --- a/apps/app/src/state/leaveGuardStore.ts +++ b/apps/app/src/state/leaveGuardStore.ts @@ -17,14 +17,22 @@ interface LeaveGuardState { armed: boolean arm: (armed: boolean) => void ask: LeaveAsk | null - setAsk: (ask: LeaveAsk | null) => void + /** + * Whether the page holds work its question would ask about now. AppLayout intercepts a + * link for the page's question only then: a link with nothing at stake is the router's, + * with its own `replace` and `state`. + */ + atStake: (() => boolean) | null + setAsk: (ask: LeaveAsk | null, atStake?: () => boolean) => void } export const useLeaveGuardStore = create((set) => ({ armed: false, arm: (armed) => set({ armed }), ask: null, - setAsk: (ask) => set({ ask }), + atStake: null, + setAsk: (ask, atStake) => + set({ ask, atStake: ask ? (atStake ?? null) : null }), })) /** diff --git a/apps/app/tests/unit/shell.AppLayout.test.tsx b/apps/app/tests/unit/shell.AppLayout.test.tsx index eb580050..2d4cdbb9 100644 --- a/apps/app/tests/unit/shell.AppLayout.test.tsx +++ b/apps/app/tests/unit/shell.AppLayout.test.tsx @@ -8,7 +8,13 @@ import { within, } from '@testing-library/react' import userEvent from '@testing-library/user-event' -import { MemoryRouter, Route, Routes } from 'react-router-dom' +import { + Link, + MemoryRouter, + Route, + Routes, + useLocation, +} from 'react-router-dom' import { createFakeSupabase, fakeProfile, @@ -31,6 +37,7 @@ import { fr } from '@/i18n/fr' import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog' import { guardDrawing } from '@/editor/annotations/drawingGuard' import { + guardWork, resetWork, markRedraw, setModePreview, @@ -292,12 +299,18 @@ describe('AppLayout and a started move on the drawing (R-12)', () => { }> - account page

} /> + } />

draft page

+ + page link + + + download link + } @@ -306,7 +319,28 @@ describe('AppLayout and a started move on the drawing (R-12)', () => {
, ) - useLeaveGuardStore.getState().setAsk(guardDrawing) + // As the draft route registers it: the question, and whether anything is at stake. + useLeaveGuardStore + .getState() + .setAsk( + guardDrawing, + () => guardWork(useAnnotationWork.getState()) !== null, + ) + } + /** The download anchor's own click: whether a handler before it took the click. */ + const downloads: boolean[] = [] + function onDownload(event: { + defaultPrevented: boolean + preventDefault(): void + }) { + downloads.push(event.defaultPrevented) + // happy-dom would try to follow it. + event.preventDefault() + } + function AccountPage() { + const location = useLocation() + const from = (location.state as { from?: string } | null)?.from ?? 'none' + return

account page from {from}

} const account = () => screen.getAllByRole('link', { @@ -326,9 +360,9 @@ describe('AppLayout and a started move on the drawing (R-12)', () => { screen.getByRole('dialog', { name: 'Abandonner le déplacement ?' }), ).toBeInTheDocument() expect(screen.queryByText(fr['leave.title'])).toBeNull() - expect(screen.queryByText('account page')).toBeNull() + expect(screen.queryByText(/account page/)).toBeNull() await user.click(screen.getByRole('button', { name: 'Abandonner' })) - expect(await screen.findByText('account page')).toBeInTheDocument() + expect(await screen.findByText(/account page/)).toBeInTheDocument() }) it('with sheet changes too: the drawing question, then the leave question', async () => { @@ -340,7 +374,7 @@ describe('AppLayout and a started move on the drawing (R-12)', () => { expect(screen.queryByText(fr['leave.title'])).toBeNull() await user.click(screen.getByRole('button', { name: 'Abandonner' })) expect(await screen.findByText(fr['leave.title'])).toBeInTheDocument() - expect(screen.queryByText('account page')).toBeNull() + expect(screen.queryByText(/account page/)).toBeNull() }) it('a started redraw asks « Abandonner le nouveau tracé ? » before a link', async () => { @@ -352,19 +386,37 @@ describe('AppLayout and a started move on the drawing (R-12)', () => { expect( screen.getByRole('dialog', { name: 'Abandonner le nouveau tracé ?' }), ).toBeInTheDocument() - expect(screen.queryByText('account page')).toBeNull() + expect(screen.queryByText(/account page/)).toBeNull() await user.click(screen.getByRole('button', { name: 'Abandonner' })) - expect(await screen.findByText('account page')).toBeInTheDocument() + expect(await screen.findByText(/account page/)).toBeInTheDocument() expect(useAnnotationWork.getState().mode).toBeNull() }) - it('a move with nothing started leaves at once and ends', async () => { + it('a move with nothing started is not at stake: the link goes at once', async () => { const user = userEvent.setup() mountDraft() startMove('a', 'doc') await user.click(account()) - expect(await screen.findByText('account page')).toBeInTheDocument() - expect(useAnnotationWork.getState().mode).toBeNull() + expect(await screen.findByText(/account page/)).toBeInTheDocument() + }) + + it("with nothing at stake a page link is the router's own: its state reaches the page", async () => { + const user = userEvent.setup() + mountDraft() + await user.click(screen.getByRole('link', { name: 'page link' })) + expect( + await screen.findByText('account page from draft'), + ).toBeInTheDocument() + }) + + it('a download link is never taken, even with a started move', async () => { + const user = userEvent.setup() + mountDraft() + started() + downloads.length = 0 + await user.click(screen.getByRole('link', { name: 'download link' })) + expect(downloads).toEqual([false]) + expect(screen.queryByRole('dialog')).toBeNull() }) })