fix(app): shell takes a link only with drawing work at stake
The draft route registers an at-stake predicate with its question; AppLayout intercepts and replays a link through navigate(to) only when it holds (or when unsaved changes arm the leave guard), so a Link keeps its own replace and state. A download anchor is never taken. Tests: the unstarted-move test no longer expects the mode ended by the shell (the link is not intercepted; leaving the editor resets the work); new: a Link's state reaches the page, a download link is not prevented.
This commit is contained in:
1 parent
87528303a1
commit
2d58da7d01
4 files changed
+87
-21
No files matched your search
@@ -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))
|
||||
|
||||
@@ -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<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.
|
||||
// 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()
|
||||
|
||||
@@ -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<LeaveGuardState>((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 }),
|
||||
}))
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user