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:
bchanot committed 2026-11-17 22:56:37 +01:00
1 parent 87528303a1
commit 2d58da7d01
4 files changed
+87 -21

No files matched your search

+10 -5
View File
@@ -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()