fix(editor): Task 8 round 1, sync tag, cascaded leave ask
This commit is contained in:
1 parent
07bebbc855
commit
579d8afff4
10 files changed
+359
-83
No files matched your search
@@ -239,8 +239,8 @@ function Workspace(props: Props & { tag: string }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The editor of the signed-in account's session tag (criterion 22). Until the tag is
|
||||
* known nothing is restored or written (fail closed); another tag is another editor.
|
||||
* The editor of the signed-in account's session tag (criterion 22), resolved at once;
|
||||
* nobody signed in, no editor. Another tag is another editor.
|
||||
*/
|
||||
export default function EditorWorkspace(props: Props) {
|
||||
const tag = useSessionTag()
|
||||
|
||||
@@ -13,8 +13,9 @@ export type DiscardTarget = 'form' | 'dialog'
|
||||
* forced close (the element vanished) goes ahead and says the draft was dropped
|
||||
* (BDR-115); a form with nothing typed says nothing.
|
||||
* `askToLeave` is the form's own « Annuler ». The browser's unload is guarded too.
|
||||
* A history jump out of the editor that was held for this question (criterion 23) is
|
||||
* what `discard` carries out; « Continuer la saisie » drops it.
|
||||
* A history jump out of the editor held for this question (criterion 23) is what
|
||||
* `discard` carries on (the next dirty layer asks, or the jump goes); « Continuer la
|
||||
* saisie » drops it.
|
||||
*/
|
||||
export function useDirtyGuard(
|
||||
guards: Guards,
|
||||
@@ -43,8 +44,12 @@ export function useDirtyGuard(
|
||||
guards.holdLeave(null)
|
||||
setConfirming(null)
|
||||
},
|
||||
/** « Abandonner »: the held leave when there is one, else `close`. */
|
||||
discard: (close: () => void) => () => (guards.takeLeave() ?? close)(),
|
||||
/** « Abandonner »: the leave held for this layer when there is one, else `close`. */
|
||||
discard: (close: () => void) => () => {
|
||||
const leave = guards.takeLeave(layer)
|
||||
if (leave) leave({ close, keep: () => setConfirming(null) })
|
||||
else close()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
import { useEffect } from 'react'
|
||||
import { useMemo } from 'react'
|
||||
import { create } from 'zustand'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
import { clearLeftDrafts } from '../leftDraft'
|
||||
import { clearLeftDrafts, sweepAccountFlags } from '../leftDraft'
|
||||
|
||||
/**
|
||||
* The session tag (criterion 22, owner 2026-11-14): a random 128-bit label the history
|
||||
* entries and the left-draft flag carry instead of the account id. It is kept in this
|
||||
* tab's sessionStorage with the SHA-256 digest of the account id, reused only for the
|
||||
* account whose digest it holds, and rotated on sign-out and on any other account (the
|
||||
* flags of the old tag cleared). A storage that refuses, or no digest (an insecure
|
||||
* origin), keeps it in memory only: a reload then restores nothing.
|
||||
* tab's sessionStorage with a fingerprint of the account id, reused only for the account
|
||||
* whose fingerprint it holds, and rotated on sign-out and on any other account (the flags
|
||||
* of the old tag cleared). Resolved at once, on any origin (http included). A storage
|
||||
* that refuses keeps it in memory only: a reload then restores nothing.
|
||||
*/
|
||||
const STORAGE_KEY = 'faunosteo:editor:session-tag'
|
||||
const TAG_PATTERN = /^[0-9a-f]{32}$/
|
||||
const DIGEST_PATTERN = /^[0-9a-f]{64}$/
|
||||
const FINGERPRINT_PATTERN = /^[0-9a-f]{16}$/
|
||||
|
||||
interface TagRecord {
|
||||
tag: string
|
||||
/** The SHA-256 digest (hex) of the account id; empty when none could be made. */
|
||||
/** The fingerprint of the account id (`fingerprintOf`). */
|
||||
account: string
|
||||
}
|
||||
|
||||
@@ -36,14 +36,21 @@ const hex = (bytes: Uint8Array) =>
|
||||
|
||||
const randomTag = () => hex(crypto.getRandomValues(new Uint8Array(16)))
|
||||
|
||||
/** The digest of the account id, or null where the browser offers none. */
|
||||
async function digestOf(user: string): Promise<string | null> {
|
||||
try {
|
||||
const data = new TextEncoder().encode(user)
|
||||
return hex(new Uint8Array(await crypto.subtle.digest('SHA-256', data)))
|
||||
} catch {
|
||||
return null
|
||||
const FNV_OFFSET = 0xcbf29ce484222325n
|
||||
const FNV_PRIME = 0x100000001b3n
|
||||
const MASK_64 = 0xffffffffffffffffn
|
||||
|
||||
/**
|
||||
* FNV-1a 64 of the account id, hex. An equality check binding the tag to one account,
|
||||
* not a secret: it keeps the raw id out of storage, and needs no secure context
|
||||
* (`crypto.subtle` is absent on an http origin such as the LAN trial).
|
||||
*/
|
||||
function fingerprintOf(user: string): string {
|
||||
let hash = FNV_OFFSET
|
||||
for (const byte of new TextEncoder().encode(user)) {
|
||||
hash = ((hash ^ BigInt(byte)) * FNV_PRIME) & MASK_64
|
||||
}
|
||||
return hash.toString(16).padStart(16, '0')
|
||||
}
|
||||
|
||||
function isTagRecord(raw: unknown): raw is TagRecord {
|
||||
@@ -53,7 +60,7 @@ function isTagRecord(raw: unknown): raw is TagRecord {
|
||||
typeof tag === 'string' &&
|
||||
TAG_PATTERN.test(tag) &&
|
||||
typeof account === 'string' &&
|
||||
DIGEST_PATTERN.test(account)
|
||||
FINGERPRINT_PATTERN.test(account)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -82,22 +89,19 @@ function retire(...records: (TagRecord | null)[]): void {
|
||||
|
||||
function hold(next: HeldTag): string {
|
||||
useSessionTagStore.setState({ held: next })
|
||||
if (next.account) writeStored({ tag: next.tag, account: next.account })
|
||||
writeStored({ tag: next.tag, account: next.account })
|
||||
return next.tag
|
||||
}
|
||||
|
||||
/** The tag of `user` in this tab: the kept one when its digest matches, else a new one. */
|
||||
export async function resolveSessionTag(user: string): Promise<string> {
|
||||
const known = useSessionTagStore.getState().held
|
||||
if (known?.user === user) return known.tag
|
||||
const account = await digestOf(user)
|
||||
// Another resolve may have finished while the digest was made.
|
||||
/** The tag of `user` in this tab: the kept one when its fingerprint matches, else a new one. */
|
||||
export function resolveSessionTag(user: string): string {
|
||||
const held = useSessionTagStore.getState().held
|
||||
if (held?.user === user) return held.tag
|
||||
const kept = account ? readStored() : null
|
||||
const account = fingerprintOf(user)
|
||||
const kept = readStored()
|
||||
if (kept && kept.account === account) return hold({ ...kept, user })
|
||||
retire(kept, held)
|
||||
return hold({ tag: randomTag(), account: account ?? '', user })
|
||||
return hold({ tag: randomTag(), account, user })
|
||||
}
|
||||
|
||||
/** Sign-out: the tag goes, with its flags; the next sign-in draws a new one. */
|
||||
@@ -107,22 +111,26 @@ export function forgetSessionTag(): void {
|
||||
useSessionTagStore.setState({ held: null })
|
||||
}
|
||||
|
||||
/** Follows the signed-in account for the app's lifetime; returns the unsubscribe. */
|
||||
/**
|
||||
* Follows the signed-in account for the app's lifetime; returns the unsubscribe. On start,
|
||||
* flags of the older shape (keyed by an account id) are swept from the tab.
|
||||
*/
|
||||
export function startSessionTagSync(): () => void {
|
||||
sweepAccountFlags()
|
||||
return useAuthStore.subscribe((state, previous) => {
|
||||
const user = state.user?.id
|
||||
if (user === previous.user?.id) return
|
||||
if (user) void resolveSessionTag(user)
|
||||
if (user) resolveSessionTag(user)
|
||||
else forgetSessionTag()
|
||||
})
|
||||
}
|
||||
|
||||
/** The session tag of the signed-in account, or null until it is known (fail closed). */
|
||||
/**
|
||||
* The session tag of the signed-in account, resolved at once; null when nobody is signed
|
||||
* in. Nothing renders under this hook's store, so resolving during the render updates no
|
||||
* other component.
|
||||
*/
|
||||
export function useSessionTag(): string | null {
|
||||
const user = useAuthStore((s) => s.user?.id ?? null)
|
||||
const held = useSessionTagStore((s) => s.held)
|
||||
useEffect(() => {
|
||||
if (user) void resolveSessionTag(user)
|
||||
}, [user])
|
||||
return user && held?.user === user ? held.tag : null
|
||||
return useMemo(() => (user ? resolveSessionTag(user) : null), [user])
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useCallback, useEffect, useRef } from 'react'
|
||||
import { useLocation } from 'react-router-dom'
|
||||
import { topDirtyLayer } from '../leftDraft'
|
||||
import type { Guards } from '../useEditorNavigation'
|
||||
import type { 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 {
|
||||
@@ -40,21 +40,49 @@ interface Watch {
|
||||
passing: { current: string | null }
|
||||
}
|
||||
|
||||
/** « Abandonner » after a held jump: the same jump, let through this time. */
|
||||
function jumpTo(api: NavigationLike, key: string, watch: Watch): void {
|
||||
interface Jump {
|
||||
api: NavigationLike
|
||||
key: string
|
||||
watch: Watch
|
||||
}
|
||||
|
||||
/**
|
||||
* The last « Abandonner »: the same jump, let through this time. A jump that fails closes
|
||||
* the question and keeps the draft (`keep`).
|
||||
*/
|
||||
function jumpTo({ api, key, watch }: Jump, keep: () => void): void {
|
||||
watch.passing.current = key
|
||||
const failed = () => {
|
||||
watch.passing.current = null
|
||||
keep()
|
||||
}
|
||||
const { committed, finished } = api.traverseTo(key)
|
||||
committed.catch(failed)
|
||||
committed.catch(() => undefined)
|
||||
finished.catch(failed)
|
||||
}
|
||||
|
||||
/**
|
||||
* `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.
|
||||
*/
|
||||
function askLeave(jump: Jump, layer: LayerName): void {
|
||||
const { guards } = jump.watch
|
||||
guards.ask(layer, false)
|
||||
guards.holdLeave({
|
||||
layer,
|
||||
go: (exit: LayerExit) => {
|
||||
const next = topDirtyLayer(guards, layer)
|
||||
if (!next) return jumpTo(jump, exit.keep)
|
||||
exit.close()
|
||||
askLeave(jump, next)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* A jump out of the editor while a dialog holds changes is cancelled when the browser
|
||||
* lets the page cancel it; the top dirty dialog asks « Abandonner les modifications ? »
|
||||
* and holds the jump for its « Abandonner ».
|
||||
* lets the page cancel it; the dirty dialogs ask, top down, before it goes.
|
||||
*/
|
||||
function onNavigate(api: NavigationLike, event: NavigateEventLike, w: Watch) {
|
||||
if (!leavesEditor(event, w.path)) return
|
||||
@@ -63,14 +91,13 @@ function onNavigate(api: NavigationLike, event: NavigateEventLike, w: Watch) {
|
||||
const layer = topDirtyLayer(w.guards)
|
||||
if (!layer) return
|
||||
event.preventDefault()
|
||||
w.guards.ask(layer, false)
|
||||
w.guards.holdLeave(() => jumpTo(api, key, w))
|
||||
askLeave({ api, key, watch: w }, layer)
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirmation before a history jump of several entries leaves the editor with a dirty
|
||||
* dialog (criterion 23): where the Navigation API can cancel it, the jump waits for the
|
||||
* dialog's own question; elsewhere (no API, a jump the browser will not let the page
|
||||
* dialog (criterion 23): where the Navigation API can cancel it, the jump waits for each
|
||||
* dirty dialog's own question; elsewhere (no API, a jump the browser will not let the page
|
||||
* 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.
|
||||
*/
|
||||
|
||||
@@ -17,9 +17,18 @@ export const leftDraftKey = ({ sheetId, tag }: HistoryOwner) =>
|
||||
/** The top layer first: the dialog the user was typing in when they left. */
|
||||
const LAYERS_TOP_DOWN: readonly LayerName[] = ['reference', 'finding']
|
||||
|
||||
/** The top layer whose form holds changes; a forced ask has no side effect. */
|
||||
export const topDirtyLayer = (guards: Guards): LayerName | null =>
|
||||
LAYERS_TOP_DOWN.find((l) => guards.ask(l, true).note) ?? null
|
||||
/**
|
||||
* The top layer whose form holds changes, or the first one under `below`; a forced ask
|
||||
* has no side effect.
|
||||
*/
|
||||
export function topDirtyLayer(
|
||||
guards: Guards,
|
||||
below: LayerName | null = null,
|
||||
): LayerName | null {
|
||||
const from = below ? LAYERS_TOP_DOWN.indexOf(below) + 1 : 0
|
||||
const layers = LAYERS_TOP_DOWN.slice(from)
|
||||
return layers.find((l) => guards.ask(l, true).note) ?? null
|
||||
}
|
||||
|
||||
function storage(): Storage | null {
|
||||
try {
|
||||
@@ -94,6 +103,24 @@ export function clearLeftDrafts(tag: string): void {
|
||||
}
|
||||
}
|
||||
|
||||
const TAG_SUFFIX = /:[0-9a-f]{32}$/
|
||||
|
||||
/**
|
||||
* Removes the flags of the older shape, keyed by an account id (criterion 22): no raw
|
||||
* account id stays in the tab's storage.
|
||||
*/
|
||||
export function sweepAccountFlags(): void {
|
||||
try {
|
||||
const store = storage()
|
||||
if (!store) return
|
||||
const keys = Array.from({ length: store.length }, (_, i) => store.key(i))
|
||||
const old = keys.filter((k) => k?.startsWith(PREFIX) && !TAG_SUFFIX.test(k))
|
||||
for (const key of old) if (key) store.removeItem(key)
|
||||
} catch {
|
||||
// A blocked storage holds no flag to sweep.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the last editor of this sheet under this session tag left with a dirty dialog:
|
||||
* shown once, then cleared. The editor's own cleanup runs before its dialogs' (a deleted
|
||||
|
||||
@@ -65,12 +65,24 @@ export interface Guards {
|
||||
/** The layers asked top down; one refusal refuses, and each one still shows its own. */
|
||||
askAll: (forced: boolean) => LeaveAnswer
|
||||
/**
|
||||
* A leave of the editor held while the top dirty layer asks (criterion 23): its
|
||||
* « Abandonner » runs it. Any later user ask, or a layer going, drops it.
|
||||
* A leave of the editor held while a dirty layer asks (criterion 23): that layer's
|
||||
* « Abandonner » carries it on. Any later user ask, or the asking layer going, drops it.
|
||||
*/
|
||||
holdLeave: (leave: (() => void) | null) => void
|
||||
/** The held leave, taken once; null when none is held. */
|
||||
takeLeave: () => (() => void) | null
|
||||
holdLeave: (leave: HeldLeave | null) => void
|
||||
/** The leave held for `layer`, taken once; null when none is held for it. */
|
||||
takeLeave: (layer: LayerName) => HeldLeave['go'] | null
|
||||
}
|
||||
|
||||
/** What a held leave may do with the layer that asked: close it, or keep it as it is. */
|
||||
export interface LayerExit {
|
||||
close: () => void
|
||||
keep: () => void
|
||||
}
|
||||
|
||||
/** A leave held for the layer asking about it. */
|
||||
export interface HeldLeave {
|
||||
layer: LayerName
|
||||
go: (exit: LayerExit) => void
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,16 +210,20 @@ function useLiveRegion() {
|
||||
|
||||
/** The leave a layer's « Abandonner » would carry out, held while it asks. */
|
||||
function useHeldLeave() {
|
||||
const held = useRef<(() => void) | null>(null)
|
||||
const held = useRef<HeldLeave | null>(null)
|
||||
return useMemo(
|
||||
() => ({
|
||||
holdLeave: (leave: (() => void) | null) => {
|
||||
holdLeave: (leave: HeldLeave | null) => {
|
||||
held.current = leave
|
||||
},
|
||||
takeLeave: () => {
|
||||
const leave = held.current
|
||||
held.current = null
|
||||
return leave
|
||||
takeLeave: (layer: LayerName) => {
|
||||
const leave = held.current?.layer === layer ? held.current : null
|
||||
if (leave) held.current = null
|
||||
return leave?.go ?? null
|
||||
},
|
||||
/** The asking layer went: its leave goes with it. */
|
||||
dropFor: (layer: LayerName) => {
|
||||
if (held.current?.layer === layer) held.current = null
|
||||
},
|
||||
}),
|
||||
[],
|
||||
@@ -225,11 +241,12 @@ function useGuards(): Guards {
|
||||
return guards.get(layer)?.(forced) ?? { allowed: true }
|
||||
}
|
||||
return {
|
||||
...leave,
|
||||
holdLeave: leave.holdLeave,
|
||||
takeLeave: leave.takeLeave,
|
||||
register: (layer, guard) => {
|
||||
if (guard) guards.set(layer, guard)
|
||||
else guards.delete(layer)
|
||||
if (!guard) leave.holdLeave(null)
|
||||
if (!guard) leave.dropFor(layer)
|
||||
},
|
||||
ask,
|
||||
askAll: (forced) => {
|
||||
|
||||
Reference in new issue
Block a user