fix(editor): a sign-out with a dirty dialog is said at the next sign-in

This commit is contained in:
bchanot committed 2026-11-14 18:50:42 +01:00
1 parent 7ecaf1998e
commit 28e298ac56
4 files changed
+178 -53

No files matched your search

+124 -27
View File
@@ -1,5 +1,6 @@
import { useEffect, useRef, useState } from 'react'
import type { HistoryOwner } from './history/entry'
import { fingerprintOf, randomHex } from './history/fingerprint'
import type { NavigationState } from './navigation'
import type { EditorNavigation, Guards, LayerName } from './useEditorNavigation'
@@ -7,10 +8,18 @@ import type { EditorNavigation, Guards, LayerName } from './useEditorNavigation'
* A dirty dialog lost when the editor goes (a Back of several entries the browser does
* not let the page cancel, BDR-078; a draft is never stored). One flag in this tab's
* sessionStorage says so to the next mount of the same sheet under the same session tag
* (criteria 17, 22).
* (criteria 17, 22). A sign-out, or a session ending, rotates the tag first: the flag is
* then keyed by the account, salted (see `ACCOUNT_PREFIX`).
*/
const PREFIX = 'faunosteo:editor:left-draft:'
/**
* Flags left as the tag rotated, keyed by sheet and by a fingerprint of the account
* salted with a random value stored in the record: the same account's next sign-in in
* this tab recomputes the key, another account never matches, and no raw id is stored.
*/
const ACCOUNT_PREFIX = 'faunosteo:editor:left-draft-account:'
export const leftDraftKey = ({ sheetId, tag }: HistoryOwner) =>
`${PREFIX}${sheetId}:${tag}`
@@ -51,19 +60,22 @@ function elementOf(state: NavigationState, layer: LayerName): unknown {
return 'key' in finding ? finding.key : finding.create.parent
}
/**
* Called as the editor goes: a layer whose form holds changes leaves the flag, unless
* the leave is one that leaves none (`confirmed`: see `useLeftDraft`).
*/
function flagIfDirty(
owner: HistoryOwner,
nav: EditorNavigation,
confirmed: boolean,
): void {
if (!owner.sheetId || !owner.tag || confirmed) return
/** What a flag holds: the layer and the element it showed, never any text. */
interface Flag {
kind: LayerName
key: unknown
}
/** The flag of the top dirty layer, or null when no form holds changes. */
function dirtyFlag(nav: EditorNavigation): Flag | null {
const layer = topDirtyLayer(nav.guards)
if (!layer) return
const flag = { kind: layer, key: elementOf(nav.state, layer) }
return layer && { kind: layer, key: elementOf(nav.state, layer) }
}
/** Called as the editor goes under its tag: a dirty layer leaves the tag's flag. */
function flagIfDirty(owner: HistoryOwner, nav: EditorNavigation): void {
const flag = owner.sheetId && owner.tag ? dirtyFlag(nav) : null
if (!flag) return
try {
storage()?.setItem(leftDraftKey(owner), JSON.stringify(flag))
} catch {
@@ -71,6 +83,70 @@ function flagIfDirty(
}
}
const accountKey = (sheetId: string, account: string, salt: string) =>
`${ACCOUNT_PREFIX}${sheetId}:${fingerprintOf(`${salt}:${account}`)}`
/** The salt a stored account flag holds, or null when the record is no such flag. */
function saltOf(raw: string | null): string | null {
try {
const record: unknown = JSON.parse(raw ?? 'null')
const salt = (record as { salt?: unknown } | null)?.salt
return typeof salt === 'string' ? salt : null
} catch {
return null
}
}
/** The keys of the account flags of `sheetId` that belong to `account`. */
function accountFlags(store: Storage, sheetId: string, account: string) {
const keys = Array.from({ length: store.length }, (_, i) => store.key(i))
return keys.filter((key): key is string => {
if (!key?.startsWith(`${ACCOUNT_PREFIX}${sheetId}:`)) return false
const salt = saltOf(store.getItem(key))
return salt !== null && key === accountKey(sheetId, account, salt)
})
}
/** Removes the account's flags of `sheetId`; true when there was one. */
function takeAccountFlags(sheetId: string, account: string): boolean {
try {
const store = storage()
const mine = store ? accountFlags(store, sheetId, account) : []
for (const key of mine) store?.removeItem(key)
return mine.length > 0
} catch {
return false
}
}
/** Called as the editor goes after its tag rotated: one flag, keyed by the account. */
function flagForAccount(
sheetId: string,
account: string,
nav: EditorNavigation,
) {
const flag = sheetId && account ? dirtyFlag(nav) : null
if (!flag) return
takeAccountFlags(sheetId, account)
const salt = randomHex(16)
try {
const record = JSON.stringify({ salt, ...flag })
storage()?.setItem(accountKey(sheetId, account, salt), record)
} catch {
// A full or blocked storage: the leave goes unsaid.
}
}
/** True when an account flag of `sheetId` is there; read once per mount. */
function hasAccountFlag(sheetId: string, account: string): boolean {
try {
const store = storage()
return !!store && accountFlags(store, sheetId, account).length > 0
} catch {
return false
}
}
/** True when the flag is there; read once per mount. */
function readFlag(owner: HistoryOwner): boolean {
try {
@@ -121,27 +197,48 @@ export function sweepAccountFlags(): void {
}
}
/** How the editor leaves, as its cleanup reads it. */
export interface LeaveFacts {
/** A leave the user confirmed (« Abandonner » before a history jump): no flag. */
confirmed: () => boolean
/** The tag was rotated (a sign-out, a session ending, another account). */
rotated: () => boolean
/** The account the tag was resolved for: kept in memory, stored salted only. */
account: string
}
/** The flag the editor leaves as it goes, by how it leaves (`LeaveFacts`). */
function flagOnLeave(
owner: HistoryOwner,
nav: EditorNavigation,
leave: LeaveFacts,
) {
if (leave.confirmed()) return
if (leave.rotated()) flagForAccount(owner.sheetId, leave.account, nav)
else flagIfDirty(owner, nav)
}
/**
* 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
* tree is cleaned up from its root), so their guards still answer when it flags.
* `confirmed` tells a leave that leaves no flag: one the user confirmed (« Abandonner »
* before a history jump), or one under a session tag already rotated.
* Whether the last editor of this sheet under this session tag, or of this account
* before its tag rotated, left with a dirty dialog: shown once, then cleared. The
* editor's own cleanup runs before its dialogs' (a deleted tree is cleaned up from its
* root), so their guards still answer when it flags.
*/
export function useLeftDraft(
nav: EditorNavigation,
owner: HistoryOwner,
confirmed: () => boolean,
leave: LeaveFacts,
): boolean {
const latest = useRef({ nav, confirmed })
latest.current = { nav, confirmed }
const [left] = useState(() => readFlag(owner))
const latest = useRef({ nav, leave })
latest.current = { nav, leave }
const { account } = leave
const [left] = useState(
() => readFlag(owner) || hasAccountFlag(owner.sheetId, account),
)
useEffect(() => {
clearFlag(owner)
return () => {
const { nav: last, confirmed: asked } = latest.current
flagIfDirty(owner, last, asked())
}
}, [owner])
takeAccountFlags(owner.sheetId, account)
return () => flagOnLeave(owner, latest.current.nav, latest.current.leave)
}, [owner, account])
return left
}