fix(editor): sign-out and shell links ask the drawing guard first

With a started move, sign-out and an in-app link now ask « Abandonner
le déplacement ? » (R-12) through the page's own question, registered by
the draft route in leaveGuardStore (`ask`; the shell imports no editor
module). The generic leave and sign-out dialogs cover only sheet-level
unsaved work; with both pending the drawing asks first, then the sheet
question, as useLeaveConfirm orders them. A move with nothing started
arms nothing (it ends); a started one still guards a reload.

Tests: sign-out and link with a started move (alone and with sheet
changes), the history jump with only a move and after dirty layers, a
section entry and « Tout afficher ».
This commit is contained in:
bchanot committed 2026-11-17 19:25:04 +01:00
1 parent 44de465c6d
commit e01d8519ea
8 files changed
+311 -26

No files matched your search

@@ -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) {
+46 -13
View File
@@ -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<string | null>(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 (
<div className="flex min-h-dvh bg-[var(--color-bg)]" onClickCapture={askBeforeLeaving}>
<div
className="flex min-h-dvh bg-[var(--color-bg)]"
onClickCapture={askBeforeLeaving}
>
<Sidebar shell={shell} />
<div className="flex min-w-0 flex-1 flex-col">
<TopBar shell={shell} menuOpen={drawerOpen} onOpenMenu={() => setDrawerOpen(true)} />
<TopBar
shell={shell}
menuOpen={drawerOpen}
onOpenMenu={() => setDrawerOpen(true)}
/>
<main className="flex-1 px-4 py-6 sm:px-8 sm:py-8">
<div className={`mx-auto w-full ${fullWidth ? '' : 'max-w-4xl'}`}>
<Outlet />
@@ -78,11 +103,19 @@ export default function AppLayout() {
open={leaveTo !== null}
onClose={() => setLeaveTo(null)}
title={t('leave.title')}
description={<p className="text-sm text-[var(--color-ink-muted)]">{t('leave.body')}</p>}
description={
<p className="text-sm text-[var(--color-ink-muted)]">
{t('leave.body')}
</p>
}
size="sm"
>
<div className="mt-4 flex flex-wrap justify-end gap-2">
<Button type="button" variant="ghost" onClick={() => setLeaveTo(null)}>
<Button
type="button"
variant="ghost"
onClick={() => setLeaveTo(null)}
>
{t('leave.stay')}
</Button>
<Button type="button" variant="danger" onClick={leave}>
@@ -5,8 +5,8 @@ import Icon from '@/components/ui/Icon'
import SignOutLossDialog from './SignOutLossDialog'
import { hasUserCopies, removeUserCopies } from '@/editor/state/recovery'
import { leaveGuardArmed, useEditorStore } from '@/editor/state/editorStore'
import { guardWork, useAnnotationWork } from '@/editor/annotations/workStore'
import { useAuthStore } from '@/state/authStore'
import { afterPageAsk } from '@/state/leaveGuardStore'
import { useT } from '@/i18n/useT'
export default function SignOutButton({
@@ -18,18 +18,19 @@ export default function SignOutButton({
const navigate = useNavigate()
const signOut = useAuthStore((s) => s.signOut)
const userId = useAuthStore((s) => s.user?.id)
const sheetWork = useEditorStore(leaveGuardArmed)
// A started move on the drawing is unsaved work too (R-12).
const drawingWork = useAnnotationWork((work) => guardWork(work) !== null)
const unsavedWork = sheetWork || drawingWork
const unsavedWork = useEditorStore(leaveGuardArmed)
const [busy, setBusy] = useState(false)
const [confirming, setConfirming] = useState(false)
// The page's own question first (a started move on the drawing, R-12), then the
// sheet-level one.
function onClick() {
const atStake =
unsavedWork || (userId !== undefined && hasUserCopies(userId))
if (atStake) setConfirming(true)
else void leave()
afterPageAsk(() => {
const atStake =
unsavedWork || (userId !== undefined && hasUserCopies(userId))
if (atStake) setConfirming(true)
else void leave()
})
}
function onConfirm() {
+22 -1
View File
@@ -1,16 +1,37 @@
import { create } from 'zustand'
/**
* A page's own question before the shell leaves it (the sheet editor's drawing guard,
* R-12): true when nothing needs asking (the caller goes on now); false when it asked,
* and `go` runs once the user answered « Abandonner ».
*/
export type LeaveAsk = (go: () => void) => boolean
/**
* Whether the page holds unsaved changes the user would lose by following a
* link of the shell. Armed by useLeaveGuard; read by AppLayout, which asks
* before an in-app link click while it is armed.
* before an in-app link click while it is armed. `ask`, registered by a page,
* comes first: AppLayout and the sign-out run it before their own question.
*/
interface LeaveGuardState {
armed: boolean
arm: (armed: boolean) => void
ask: LeaveAsk | null
setAsk: (ask: LeaveAsk | null) => void
}
export const useLeaveGuardStore = create<LeaveGuardState>((set) => ({
armed: false,
arm: (armed) => set({ armed }),
ask: null,
setAsk: (ask) => set({ ask }),
}))
/**
* The page's question first, then `go`: at once when it asks nothing, after its
* « Abandonner » otherwise.
*/
export function afterPageAsk(go: () => void): void {
const { ask } = useLeaveGuardStore.getState()
if (!ask || ask(go)) go()
}