fix(editor): Task 8 round 2, cascade closes the sheet
This commit is contained in:
1 parent
579d8afff4
commit
791fc03a0d
7 files changed
+205
-10
No files matched your search
@@ -132,5 +132,9 @@ export function startSessionTagSync(): () => void {
|
||||
*/
|
||||
export function useSessionTag(): string | null {
|
||||
const user = useAuthStore((s) => s.user?.id ?? null)
|
||||
// shortcut: resolved during the render (it may set the tag store, write sessionStorage
|
||||
// and clear old flags). Safe while it stays idempotent per account and nothing
|
||||
// subscribes to `useSessionTagStore` (no update-while-rendering). Upgrade path: resolve
|
||||
// at sign-in in `startSessionTagSync` only, and read the held tag here.
|
||||
return useMemo(() => (user ? resolveSessionTag(user) : null), [user])
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
import { useCallback, useEffect, useRef } from 'react'
|
||||
import { useLocation } from 'react-router-dom'
|
||||
import { topDirtyLayer } from '../leftDraft'
|
||||
import type { Guards, LayerExit, LayerName } from '../useEditorNavigation'
|
||||
import type {
|
||||
EditorNavigation,
|
||||
Guards,
|
||||
LayerExit,
|
||||
LayerName,
|
||||
} from '../useEditorNavigation'
|
||||
|
||||
/** The part of a Navigation API `navigate` event this guard reads (not in the DOM types). */
|
||||
interface NavigateEventLike extends Event {
|
||||
@@ -35,6 +40,8 @@ function leavesEditor(event: NavigateEventLike, path: string): boolean {
|
||||
|
||||
interface Watch {
|
||||
guards: Guards
|
||||
/** Closes the citations sheet, which may lie between two asking dialogs. */
|
||||
closeSheet: () => void
|
||||
path: string
|
||||
/** The key of the jump the user confirmed: it passes, and leaves no flag. */
|
||||
passing: { current: string | null }
|
||||
@@ -63,18 +70,21 @@ function jumpTo({ api, key, watch }: Jump, keep: () => void): void {
|
||||
|
||||
/**
|
||||
* `layer` asks « Abandonner les modifications ? » and holds the jump. Its « Abandonner »
|
||||
* closes it and the next dirty layer under it asks in turn; the last one lets the jump go.
|
||||
* Any « Continuer la saisie » drops the jump.
|
||||
* closes it, with the citations sheet it may have been opened from (so the next question
|
||||
* is not hidden under the sheet), and the next dirty layer under it asks in turn; the
|
||||
* last one lets the jump go. Any « Continuer la saisie » drops the jump.
|
||||
*/
|
||||
function askLeave(jump: Jump, layer: LayerName): void {
|
||||
const { guards } = jump.watch
|
||||
const { guards, closeSheet } = jump.watch
|
||||
guards.ask(layer, false)
|
||||
guards.holdLeave({
|
||||
layer,
|
||||
go: (exit: LayerExit) => {
|
||||
const next = topDirtyLayer(guards, layer)
|
||||
if (!next) return jumpTo(jump, exit.keep)
|
||||
// One commit closes both: the history writes one replace, never a Back racing it.
|
||||
exit.close()
|
||||
closeSheet()
|
||||
askLeave(jump, next)
|
||||
},
|
||||
})
|
||||
@@ -101,18 +111,22 @@ function onNavigate(api: NavigationLike, event: NavigateEventLike, w: Watch) {
|
||||
* cancel) the left-draft notice says it afterwards. Traversals inside the editor keep the
|
||||
* history adapter's refusal path. Returns whether the user confirmed a leave.
|
||||
*/
|
||||
export default function useLeaveConfirm(guards: Guards): () => boolean {
|
||||
export default function useLeaveConfirm(
|
||||
nav: Pick<EditorNavigation, 'guards' | 'closeCitations'>,
|
||||
): () => boolean {
|
||||
const { pathname } = useLocation()
|
||||
const passing = useRef<string | null>(null)
|
||||
const path = useRef(pathname).current
|
||||
const { guards, closeCitations } = nav
|
||||
useEffect(() => {
|
||||
const api = navigationApi()
|
||||
if (!api) return
|
||||
const watch: Watch = { guards, path, passing }
|
||||
const closeSheet = () => void closeCitations()
|
||||
const watch: Watch = { guards, closeSheet, path, passing }
|
||||
const listener = (event: Event) =>
|
||||
onNavigate(api, event as NavigateEventLike, watch)
|
||||
api.addEventListener('navigate', listener)
|
||||
return () => api.removeEventListener('navigate', listener)
|
||||
}, [guards, path])
|
||||
}, [guards, closeCitations, path])
|
||||
return useCallback(() => passing.current !== null, [])
|
||||
}
|
||||
Reference in new issue
Block a user