diff --git a/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx b/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx index 3a0bfd4d..6be2c76a 100644 --- a/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx +++ b/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx @@ -15,7 +15,7 @@ import Notice from '@/components/ui/Notice' import { failureMessage } from '@/admin/labels' import { useAdminPermissions } from '@/admin/permissions' import { isRightsRefusal, useAdminQuery } from '@/admin/useAdminQuery' -import { useLeaveGuard } from '@/app/useLeaveGuard' +import { useBeforeUnload, useLeaveGuard } from '@/app/useLeaveGuard' import { editorMode, type EditorMode, @@ -26,6 +26,8 @@ import { import { releaseEditFocus } from '@/editor/focusRequest' import { leaveGuardArmed, useEditorStore } from '@/editor/state/editorStore' import { guardWork, useAnnotationWork } from '@/editor/annotations/workStore' +import { guardDrawing } from '@/editor/annotations/drawingGuard' +import { useLeaveGuardStore } from '@/state/leaveGuardStore' import { useUndoShortcuts } from '@/editor/useUndoShortcuts' import { useTabTier } from '@/editor/useViewport' import { useAuthStore } from '@/state/authStore' @@ -55,6 +57,20 @@ function useReadOnly(mode: EditorMode, canEdit: boolean): boolean { * The draft page once the sheet is read: opens the store session for this version, derives * the mode, and renders what the store holds. Components only render and call actions. */ +/** + * 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. + */ +function useDrawingLeaveAsk() { + const setAsk = useLeaveGuardStore((s) => s.setAsk) + useEffect(() => { + setAsk(guardDrawing) + return () => setAsk(null) + }, [setAsk]) + useBeforeUnload(useAnnotationWork((work) => guardWork(work) !== null)) +} + function DraftEditor({ sheet, versionId, @@ -91,8 +107,8 @@ function DraftEditor({ useLayoutEffect(() => { if (readOnly) releaseEditFocus() }, [readOnly]) - const drawingWork = useAnnotationWork((work) => guardWork(work) !== null) - useLeaveGuard(useEditorStore(leaveGuardArmed) || drawingWork) + useLeaveGuard(useEditorStore(leaveGuardArmed)) + useDrawingLeaveAsk() useUndoShortcuts(!readOnly) if (status === 'failed' && failure !== null) { diff --git a/apps/app/src/components/shell/AppLayout.tsx b/apps/app/src/components/shell/AppLayout.tsx index ca92b61d..0a8ae188 100644 --- a/apps/app/src/components/shell/AppLayout.tsx +++ b/apps/app/src/components/shell/AppLayout.tsx @@ -5,7 +5,7 @@ import Dialog from '@/components/ui/Dialog' import MobileDrawer from './MobileDrawer' import { DEFAULT_SHELL, EDITOR_SHELL, Sidebar, TopBar } from './ShellChrome' import { usePurgeForeignCopies } from '@/app/usePurgeForeignCopies' -import { useLeaveGuardStore } from '@/state/leaveGuardStore' +import { afterPageAsk, useLeaveGuardStore } from '@/state/leaveGuardStore' import { useT } from '@/i18n/useT' /** The draft editor takes the whole width (board 04's three columns). */ @@ -14,9 +14,17 @@ 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. */ function followedLink(event: MouseEvent): HTMLAnchorElement | null { if (event.defaultPrevented || event.button !== 0) return null - if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return null - const anchor = (event.target as Element).closest('a[href]') as HTMLAnchorElement | null - if (!anchor || anchor.target === '_blank' || anchor.origin !== window.location.origin) return null + if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) + return null + const anchor = (event.target as Element).closest( + 'a[href]', + ) as HTMLAnchorElement | null + if ( + !anchor || + anchor.target === '_blank' || + anchor.origin !== window.location.origin + ) + return null return anchor } @@ -36,32 +44,49 @@ 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. function askBeforeLeaving(event: MouseEvent) { - if (!guarded) return + const { ask } = useLeaveGuardStore.getState() + if (!guarded && !ask) return const anchor = followedLink(event) if (!anchor) return event.preventDefault() event.stopPropagation() - setLeaveTo(anchor.pathname + anchor.search + anchor.hash) + afterPageAsk(() => askSheet(anchor.pathname + anchor.search + anchor.hash)) } - function leave() { - const to = leaveTo - if (!to) return + function askSheet(to: string) { + if (useLeaveGuardStore.getState().armed) setLeaveTo(to) + else go(to) + } + + function go(to: string) { arm(false) setLeaveTo(null) setDrawerOpen(false) navigate(to) } + + function leave() { + if (leaveTo) go(leaveTo) + } const fullWidth = matchPath(DRAFT_EDITOR_PATH, location.pathname) !== null const shell = fullWidth ? EDITOR_SHELL : DEFAULT_SHELL return ( -
+
- setDrawerOpen(true)} /> + setDrawerOpen(true)} + />
@@ -78,11 +103,19 @@ export default function AppLayout() { open={leaveTo !== null} onClose={() => setLeaveTo(null)} title={t('leave.title')} - description={

{t('leave.body')}

} + description={ +

+ {t('leave.body')} +

+ } size="sm" >
- + @@ -423,6 +424,22 @@ describe('the drawing guard (R-12): a move mode and the navigations', () => { expect(useAnnotationWork.getState().mode).toBeNull() }) + it('a section entry and « Tout afficher » ask the same way', async () => { + useLocaleStore.setState({ locale: 'fr' }) + const user = userEvent.setup() + mount() + started() + await user.click(screen.getByRole('button', { name: 'section' })) + expect(useAnnotationWork.getState().question?.work).toBe('move') + expect(tabShown()).toHaveTextContent('Général') + await user.click(screen.getByRole('button', { name: 'Continuer' })) + await user.click(screen.getByRole('button', { name: 'show all' })) + expect(useAnnotationWork.getState().question?.work).toBe('move') + expect(tabShown()).toHaveTextContent('Général') + await user.click(screen.getByRole('button', { name: 'Abandonner' })) + expect(tabShown()).toHaveTextContent('Références') + }) + it('« Aller à » asks the same way', async () => { useLocaleStore.setState({ locale: 'fr' }) const user = userEvent.setup() diff --git a/apps/app/tests/unit/editor-leave-cascade.test.tsx b/apps/app/tests/unit/editor-leave-cascade.test.tsx index 82836071..ab0fae30 100644 --- a/apps/app/tests/unit/editor-leave-cascade.test.tsx +++ b/apps/app/tests/unit/editor-leave-cascade.test.tsx @@ -13,6 +13,13 @@ import { type LayerName, } from '@/editor/useEditorNavigation' import { useLocaleStore } from '@/state/localeStore' +import { answerDrawing } from '@/editor/annotations/drawingGuard' +import { + resetWork, + setModePreview, + startMove, + useAnnotationWork, +} from '@/editor/annotations/workStore' const path = '/app/admin/sheets/S/draft' @@ -132,3 +139,54 @@ describe('reference, citations and finding dialogs, all dirty', () => { expect(api.traverseTo).not.toHaveBeenCalled() }) }) + +describe('the drawing guard on a history jump out of the editor (R-12)', () => { + const STORED = { type: 'circle' as const, cx: 1, cy: 2, r: 3 } + afterEach(resetWork) + + it('with only a started move: the jump waits for its question, then goes', async () => { + cleanup() + render( + + + , + ) + startMove('a', 'doc') + setModePreview({ ...STORED, cx: 9 }, STORED) + expect(jump().defaultPrevented).toBe(true) + expect(useAnnotationWork.getState().question?.work).toBe('move') + expect(api.traverseTo).not.toHaveBeenCalled() + act(() => answerDrawing(true)) + expect(api.traverseTo).toHaveBeenCalledWith('list') + }) + + it('with nothing started: the jump is not held and the mode ends', () => { + cleanup() + render( + + + , + ) + startMove('a', 'doc') + expect(jump().defaultPrevented).toBe(false) + }) + + it('dirty layers ask first, then the drawing, then the jump goes', async () => { + const user = userEvent.setup() + startMove('a', 'doc') + setModePreview({ ...STORED, cx: 9 }, STORED) + jump() + await user.click(button('reference', 'Abandonner')) + await user.click(button('citations', 'Abandonner')) + await user.click(button('finding', 'Abandonner')) + expect(api.traverseTo).not.toHaveBeenCalled() + expect(useAnnotationWork.getState().question?.work).toBe('move') + act(() => answerDrawing(true)) + expect(api.traverseTo).toHaveBeenCalledWith('list') + }) +}) + +function DrawingOnly() { + useLeaveConfirm(useEditorNavigation('findings')) + return null +} diff --git a/apps/app/tests/unit/shell.AppLayout.test.tsx b/apps/app/tests/unit/shell.AppLayout.test.tsx index bb71d84d..6324d2b3 100644 --- a/apps/app/tests/unit/shell.AppLayout.test.tsx +++ b/apps/app/tests/unit/shell.AppLayout.test.tsx @@ -28,6 +28,14 @@ import { useAuthStore } from '@/state/authStore' import { useLeaveGuardStore } from '@/state/leaveGuardStore' import { useLocaleStore } from '@/state/localeStore' import { fr } from '@/i18n/fr' +import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog' +import { guardDrawing } from '@/editor/annotations/drawingGuard' +import { + resetWork, + setModePreview, + startMove, + useAnnotationWork, +} from '@/editor/annotations/workStore' let fake: FakeSupabase @@ -271,6 +279,78 @@ describe('AppLayout, editor sidebar from 1 280 px', () => { }) }) +describe('AppLayout and a started move on the drawing (R-12)', () => { + const STORED = { type: 'circle' as const, cx: 1, cy: 2, r: 3 } + afterEach(() => { + useLeaveGuardStore.getState().setAsk(null) + resetWork() + }) + function mountDraft() { + render( + + + }> + account page

} /> + +

draft page

+ + + } + /> +
+
+
, + ) + useLeaveGuardStore.getState().setAsk(guardDrawing) + } + const account = () => + screen.getAllByRole('link', { + name: new RegExp(fr['shell.nav.account']), + })[0] + const started = () => { + startMove('a', 'doc') + setModePreview({ ...STORED, cx: 9 }, STORED) + } + + it('a link asks the drawing question, not the form one; « Abandonner » follows it', async () => { + const user = userEvent.setup() + mountDraft() + started() + await user.click(account()) + expect( + screen.getByRole('dialog', { name: 'Abandonner le déplacement ?' }), + ).toBeInTheDocument() + expect(screen.queryByText(fr['leave.title'])).toBeNull() + expect(screen.queryByText('account page')).toBeNull() + await user.click(screen.getByRole('button', { name: 'Abandonner' })) + expect(await screen.findByText('account page')).toBeInTheDocument() + }) + + it('with sheet changes too: the drawing question, then the leave question', async () => { + const user = userEvent.setup() + mountDraft() + started() + useLeaveGuardStore.getState().arm(true) + await user.click(account()) + 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() + }) + + it('a move with nothing started leaves at once and ends', 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() + }) +}) + describe('monogram', () => { it('takes initials, else two letters, else ?', () => { expect(monogram('Alice Local', undefined)).toBe('AL')