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
|
* 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
|
* sign-out ask « Abandonner le déplacement ? » (or « … la création ? ») before the
|
||||||
* move also guards a reload or a tab close; one with nothing started arms nothing.
|
* 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() {
|
function useDrawingLeaveAsk() {
|
||||||
const setAsk = useLeaveGuardStore((s) => s.setAsk)
|
const setAsk = useLeaveGuardStore((s) => s.setAsk)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setAsk(guardDrawing)
|
setAsk(guardDrawing, () => guardWork(useAnnotationWork.getState()) !== null)
|
||||||
return () => setAsk(null)
|
return () => setAsk(null)
|
||||||
}, [setAsk])
|
}, [setAsk])
|
||||||
useBeforeUnload(useAnnotationWork((work) => guardWork(work) !== null))
|
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). */
|
/** The draft editor takes the whole width (board 04's three columns). */
|
||||||
const DRAFT_EDITOR_PATH = '/app/admin/sheets/:sheetId/draft'
|
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 {
|
function followedLink(event: MouseEvent): HTMLAnchorElement | null {
|
||||||
if (event.defaultPrevented || event.button !== 0) return null
|
if (event.defaultPrevented || event.button !== 0) return null
|
||||||
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
|
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
|
||||||
@@ -22,6 +25,7 @@ function followedLink(event: MouseEvent): HTMLAnchorElement | null {
|
|||||||
if (
|
if (
|
||||||
!anchor ||
|
!anchor ||
|
||||||
anchor.target === '_blank' ||
|
anchor.target === '_blank' ||
|
||||||
|
anchor.hasAttribute('download') ||
|
||||||
anchor.origin !== window.location.origin
|
anchor.origin !== window.location.origin
|
||||||
)
|
)
|
||||||
return null
|
return null
|
||||||
@@ -44,11 +48,12 @@ export default function AppLayout() {
|
|||||||
const arm = useLeaveGuardStore((s) => s.arm)
|
const arm = useLeaveGuardStore((s) => s.arm)
|
||||||
const [leaveTo, setLeaveTo] = useState<string | null>(null)
|
const [leaveTo, setLeaveTo] = useState<string | null>(null)
|
||||||
|
|
||||||
// The page's own question first (a started move on the drawing, R-12), then the
|
// The page's own question first (a started move or a creation on the drawing, R-12),
|
||||||
// unsaved-changes one; the link is followed by the router once both let it go.
|
// 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) {
|
function askBeforeLeaving(event: MouseEvent) {
|
||||||
const { ask } = useLeaveGuardStore.getState()
|
const { atStake } = useLeaveGuardStore.getState()
|
||||||
if (!guarded && !ask) return
|
if (!guarded && !atStake?.()) return
|
||||||
const anchor = followedLink(event)
|
const anchor = followedLink(event)
|
||||||
if (!anchor) return
|
if (!anchor) return
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|||||||
@@ -17,14 +17,22 @@ interface LeaveGuardState {
|
|||||||
armed: boolean
|
armed: boolean
|
||||||
arm: (armed: boolean) => void
|
arm: (armed: boolean) => void
|
||||||
ask: LeaveAsk | null
|
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) => ({
|
export const useLeaveGuardStore = create<LeaveGuardState>((set) => ({
|
||||||
armed: false,
|
armed: false,
|
||||||
arm: (armed) => set({ armed }),
|
arm: (armed) => set({ armed }),
|
||||||
ask: null,
|
ask: null,
|
||||||
setAsk: (ask) => set({ ask }),
|
atStake: null,
|
||||||
|
setAsk: (ask, atStake) =>
|
||||||
|
set({ ask, atStake: ask ? (atStake ?? null) : null }),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -8,7 +8,13 @@ import {
|
|||||||
within,
|
within,
|
||||||
} from '@testing-library/react'
|
} from '@testing-library/react'
|
||||||
import userEvent from '@testing-library/user-event'
|
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 {
|
import {
|
||||||
createFakeSupabase,
|
createFakeSupabase,
|
||||||
fakeProfile,
|
fakeProfile,
|
||||||
@@ -31,6 +37,7 @@ import { fr } from '@/i18n/fr'
|
|||||||
import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog'
|
import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog'
|
||||||
import { guardDrawing } from '@/editor/annotations/drawingGuard'
|
import { guardDrawing } from '@/editor/annotations/drawingGuard'
|
||||||
import {
|
import {
|
||||||
|
guardWork,
|
||||||
resetWork,
|
resetWork,
|
||||||
markRedraw,
|
markRedraw,
|
||||||
setModePreview,
|
setModePreview,
|
||||||
@@ -292,12 +299,18 @@ describe('AppLayout and a started move on the drawing (R-12)', () => {
|
|||||||
<MemoryRouter initialEntries={['/app/admin/sheets/s-1/draft']}>
|
<MemoryRouter initialEntries={['/app/admin/sheets/s-1/draft']}>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/app" element={<AppLayout />}>
|
<Route path="/app" element={<AppLayout />}>
|
||||||
<Route path="account" element={<p>account page</p>} />
|
<Route path="account" element={<AccountPage />} />
|
||||||
<Route
|
<Route
|
||||||
path="admin/sheets/:sheetId/draft"
|
path="admin/sheets/:sheetId/draft"
|
||||||
element={
|
element={
|
||||||
<>
|
<>
|
||||||
<p>draft page</p>
|
<p>draft page</p>
|
||||||
|
<Link to="/app/account" replace state={{ from: 'draft' }}>
|
||||||
|
page link
|
||||||
|
</Link>
|
||||||
|
<a href="/export.json" download onClick={onDownload}>
|
||||||
|
download link
|
||||||
|
</a>
|
||||||
<DrawingGuardDialog />
|
<DrawingGuardDialog />
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
@@ -306,7 +319,28 @@ describe('AppLayout and a started move on the drawing (R-12)', () => {
|
|||||||
</Routes>
|
</Routes>
|
||||||
</MemoryRouter>,
|
</MemoryRouter>,
|
||||||
)
|
)
|
||||||
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 <p>account page from {from}</p>
|
||||||
}
|
}
|
||||||
const account = () =>
|
const account = () =>
|
||||||
screen.getAllByRole('link', {
|
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 ?' }),
|
screen.getByRole('dialog', { name: 'Abandonner le déplacement ?' }),
|
||||||
).toBeInTheDocument()
|
).toBeInTheDocument()
|
||||||
expect(screen.queryByText(fr['leave.title'])).toBeNull()
|
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' }))
|
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 () => {
|
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()
|
expect(screen.queryByText(fr['leave.title'])).toBeNull()
|
||||||
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
|
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
|
||||||
expect(await screen.findByText(fr['leave.title'])).toBeInTheDocument()
|
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 () => {
|
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(
|
expect(
|
||||||
screen.getByRole('dialog', { name: 'Abandonner le nouveau tracé ?' }),
|
screen.getByRole('dialog', { name: 'Abandonner le nouveau tracé ?' }),
|
||||||
).toBeInTheDocument()
|
).toBeInTheDocument()
|
||||||
expect(screen.queryByText('account page')).toBeNull()
|
expect(screen.queryByText(/account page/)).toBeNull()
|
||||||
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
|
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()
|
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()
|
const user = userEvent.setup()
|
||||||
mountDraft()
|
mountDraft()
|
||||||
startMove('a', 'doc')
|
startMove('a', 'doc')
|
||||||
await user.click(account())
|
await user.click(account())
|
||||||
expect(await screen.findByText('account page')).toBeInTheDocument()
|
expect(await screen.findByText(/account page/)).toBeInTheDocument()
|
||||||
expect(useAnnotationWork.getState().mode).toBeNull()
|
})
|
||||||
|
|
||||||
|
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()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user