feat(editor): session tag in history, confirm before leaving
This commit is contained in:
1 parent
4018344ffc
commit
07bebbc855
19 files changed
+844
-76
No files matched your search
@@ -11,8 +11,9 @@ import { EDITOR_TABS, type EditorTabId } from '../tabs'
|
||||
|
||||
/**
|
||||
* What one browser history entry of the editor holds (R2F-1): the view, the view below
|
||||
* it (the place a Back returns to), and whose editor wrote it. Navigation only: never
|
||||
* typed text, a search term or a draft.
|
||||
* it (the place a Back returns to), and whose editor wrote it: the sheet and the session
|
||||
* tag (criterion 22), a random label bound to the signed-in account, never its id.
|
||||
* Navigation only: never typed text, a search term or a draft.
|
||||
*/
|
||||
export interface HistoryEntry {
|
||||
/** Keys the scroll map: one per entry written, kept by a replace. */
|
||||
@@ -20,13 +21,13 @@ export interface HistoryEntry {
|
||||
view: EditorView
|
||||
below: EditorView | null
|
||||
sheetId: string
|
||||
userId: string
|
||||
tag: string
|
||||
}
|
||||
|
||||
/** The sheet and the account an entry may be restored for. */
|
||||
/** The sheet and the session tag an entry may be restored for. */
|
||||
export interface HistoryOwner {
|
||||
sheetId: string
|
||||
userId: string
|
||||
tag: string
|
||||
}
|
||||
|
||||
/** The key of the entry inside the router's `location.state`. */
|
||||
@@ -129,11 +130,14 @@ export function withoutForms(view: EditorView): EditorView {
|
||||
return sanitizeView(view, false) ?? view
|
||||
}
|
||||
|
||||
/** The entry's id and owner, each a string within the cap, or null. */
|
||||
/**
|
||||
* The entry's id and owner, each a string within the cap, or null. An entry of the older
|
||||
* shape (an account id, no tag) has no owner: it reads as foreign.
|
||||
*/
|
||||
function idsOf(raw: Record<string, unknown>) {
|
||||
const { id, sheetId, userId } = raw
|
||||
const valid = isId(id) && isId(sheetId) && isId(userId)
|
||||
return valid ? { id, sheetId, userId } : null
|
||||
const { id, sheetId, tag } = raw
|
||||
const valid = isId(id) && isId(sheetId) && isId(tag)
|
||||
return valid ? { id, sheetId, tag } : null
|
||||
}
|
||||
|
||||
/** The entry `state` (a `location.state`) carries, or null when it carries none. */
|
||||
@@ -147,15 +151,15 @@ export function readEntry(state: unknown): HistoryEntry | null {
|
||||
return { ...ids, view, below }
|
||||
}
|
||||
|
||||
/** An entry restores only for the sheet and the account that wrote it. */
|
||||
/** An entry restores only for the sheet and the session tag that wrote it. */
|
||||
export function ownedEntry(
|
||||
state: unknown,
|
||||
owner: HistoryOwner,
|
||||
): HistoryEntry | null {
|
||||
const entry = readEntry(state)
|
||||
// An empty id is no owner: nobody signed in, no sheet known.
|
||||
if (!owner.sheetId || !owner.userId) return null
|
||||
const mine = entry?.sheetId === owner.sheetId && entry.userId === owner.userId
|
||||
// An empty id is no owner: no tag known yet (fail closed), no sheet known.
|
||||
if (!owner.sheetId || !owner.tag) return null
|
||||
const mine = entry?.sheetId === owner.sheetId && entry.tag === owner.tag
|
||||
return mine ? entry : null
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user