feat(editor): session tag in history, confirm before leaving

This commit is contained in:
bchanot committed 2026-11-14 14:47:43 +01:00
1 parent 4018344ffc
commit 07bebbc855
19 files changed
+844 -76

No files matched your search

@@ -1,4 +1,10 @@
import { useEffect, useMemo, useState, type ReactNode } from 'react'
import {
useCallback,
useEffect,
useMemo,
useState,
type ReactNode,
} from 'react'
import { useLocation } from 'react-router-dom'
import type { OfficialSheetAdmin } from '@faunosteo/supabase'
import Notice from '@/components/ui/Notice'
@@ -13,6 +19,8 @@ import {
type HistoryOwner,
} from '@/editor/history/entry'
import useEditorHistory from '@/editor/history/useEditorHistory'
import useLeaveConfirm from '@/editor/history/useLeaveConfirm'
import { useSessionTag, useSessionTagStore } from '@/editor/history/sessionTag'
import { useLeftDraft } from '@/editor/leftDraft'
import { citationsOfView, defaultView } from '@/editor/navigation'
import { findingLayerOf, referenceLayerOf } from '@/editor/findingLayer'
@@ -23,7 +31,6 @@ import {
} from '@/editor/useEditorNavigation'
import type { TabTier } from '@/editor/useViewport'
import { useT } from '@/i18n/useT'
import { useAuthStore } from '@/state/authStore'
import ConflictDialog from './ConflictDialog'
import CopyIssueNotice from './CopyIssueNotice'
import DraftHeader from './DraftHeader'
@@ -124,10 +131,21 @@ function Places({ nav, readOnly, reloadSheet }: PlacesProps) {
)
}
/** The sheet and the account the history entries of this editor belong to. */
function useOwner(sheetId: string): HistoryOwner {
const userId = useAuthStore((s) => s.user?.id ?? '')
return useMemo(() => ({ sheetId, userId }), [sheetId, userId])
/** The sheet and the session tag the history entries of this editor belong to. */
function useOwner(sheetId: string, tag: string): HistoryOwner {
return useMemo(() => ({ sheetId, tag }), [sheetId, tag])
}
/**
* A leave that leaves no left-draft flag: one the user confirmed before a history jump,
* or one under a tag already rotated (a sign-out): nobody would read that flag.
*/
function useUnflagged(nav: EditorNavigation, tag: string) {
const confirmed = useLeaveConfirm(nav.guards)
return useCallback(
() => confirmed() || useSessionTagStore.getState().held?.tag !== tag,
[confirmed, tag],
)
}
/** A reload or a reopened entry shows the view the history kept, never a layer. */
@@ -154,18 +172,19 @@ function useShowRestored(nav: EditorNavigation) {
}
/**
* The navigation of this sheet for this account: the first tab known at mount
* (`initialTab`), the view a kept entry holds, the browser history in step with it, and
* whether the last editor left a dirty dialog behind.
* The navigation of this sheet under this session tag: the first tab known at mount
* (`initialTab`), the view a kept entry holds, the browser history in step with it, the
* confirmation before a history jump leaves with a dirty dialog, and whether the last
* editor left a dirty dialog behind.
*/
function useWorkspaceNavigation(sheetId: string, tier: TabTier) {
function useWorkspaceNavigation(sheetId: string, tier: TabTier, tag: string) {
const previewState = useEditorStore((s) => s.previewState)
const tab = previewState ? initialTab(previewState, tier) : 'annotations'
const owner = useOwner(sheetId)
const owner = useOwner(sheetId, tag)
const nav = useEditorNavigation(tab, useRestoredView(owner))
useEditorHistory(nav, owner, defaultView(tab))
useShowRestored(nav)
return { nav, left: useLeftDraft(nav, owner) }
return { nav, left: useLeftDraft(nav, owner, useUnflagged(nav, tag)) }
}
/** The last editor of this sheet was left with a dirty dialog: said once. */
@@ -180,9 +199,13 @@ function LeftDraftNotice({ shown }: { shown: boolean }) {
* panel and the shown place. Owns the navigation state, so the first tab is known at
* mount and later edits never move the user off their tab.
*/
export default function EditorWorkspace(props: Props) {
function Workspace(props: Props & { tag: string }) {
const { sheet, mode, readOnly } = props
const { nav, left } = useWorkspaceNavigation(sheet.sheet_id, props.tier)
const { nav, left } = useWorkspaceNavigation(
sheet.sheet_id,
props.tier,
props.tag,
)
return (
<EditingState readOnly={readOnly} mode={mode}>
<div className="space-y-4" data-editor-workspace>
@@ -214,3 +237,12 @@ export default function EditorWorkspace(props: Props) {
</EditingState>
)
}
/**
* 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.
*/
export default function EditorWorkspace(props: Props) {
const tag = useSessionTag()
return tag ? <Workspace key={tag} {...props} tag={tag} /> : null
}
@@ -183,7 +183,7 @@ export default function FindingCreateForm(props: FindingCreateFormProps) {
submitLabel={t('editor.findings.create')}
onCancel={() => props.onClose(props.readOnly)}
onKeepTyping={form.guard.keepTyping}
onDiscard={() => props.onClose(true)}
onDiscard={form.guard.discard(() => props.onClose(true))}
/>
</form>
)
@@ -259,7 +259,7 @@ export default function FindingEditForm(props: FindingEditFormProps) {
submitLabel={t('editor.findings.save')}
onCancel={form.cancel}
onKeepTyping={guard.keepTyping}
onDiscard={discard}
onDiscard={guard.discard(discard)}
/>
</form>
)
@@ -13,6 +13,8 @@ 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.
*/
export function useDirtyGuard(
guards: Guards,
@@ -37,7 +39,12 @@ export function useDirtyGuard(
return {
confirming,
askToLeave: () => setConfirming('form'),
keepTyping: () => setConfirming(null),
keepTyping: () => {
guards.holdLeave(null)
setConfirming(null)
},
/** « Abandonner »: the held leave when there is one, else `close`. */
discard: (close: () => void) => () => (guards.takeLeave() ?? close)(),
}
}
@@ -239,7 +239,7 @@ export default function ReferenceForm(props: ReferenceFormProps) {
: props.onClose(props.readOnly)
}
onKeepTyping={guard.keepTyping}
onDiscard={() => props.onClose(true)}
onDiscard={guard.discard(() => props.onClose(true))}
/>
</form>
)