feat(app): sheet draft editor route, modes, recovery offer and conflict dialog

This commit is contained in:
bchanot committed 2026-11-04 12:35:33 +01:00
1 parent b14509f221
commit 6aba9711f0
44 files changed
+3143 -13

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
import type { Locale } from '@/i18n/types'
const ZONE = 'Europe/Paris'
/** « 4 novembre 2026 à 14:32 », in Paris time (conflict dialog). */
export function formatParisDateTime(iso: string, locale: Locale): string {
return new Date(iso).toLocaleString(locale, {
day: 'numeric',
month: 'long',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
timeZone: ZONE,
})
}
/** « 14:32 », in Paris time (recovery copy). */
export function formatParisTime(iso: string, locale: Locale): string {
return new Date(iso).toLocaleTimeString(locale, {
hour: '2-digit',
minute: '2-digit',
timeZone: ZONE,
})
}
+45
View File
@@ -0,0 +1,45 @@
import type { SheetFailureKind } from '@faunosteo/supabase'
import { failureMessage } from '@/admin/labels'
import type { CatalogueFailure } from '@/catalogue/failure'
import { coreFailureMessage } from '@/core/failure'
type Translate = (key: string) => string
/** Refusals whose message promises that the unsaved work is kept (plan, header status). */
const PROMISES_COPY: ReadonlySet<SheetFailureKind> = new Set([
'session_expired',
'rights.mfa',
'rights.capability',
'draft.frozen',
'sheet.inactive',
])
/**
* The header notice of a refused save. A message that promises kept work reads the real
* write flag of the recovery copy: the copy is temporary and limited to this tab, and
* when its last write failed the changes cannot be kept.
*/
export function saveFailureMessage(
t: Translate,
kind: SheetFailureKind,
state: { unsaved: boolean; copyWritten: boolean },
): string {
if (kind === 'draft_conflict') return t('editor.draft.conflict.unknown')
const base = failureMessage(t, kind)
if (!state.unsaved || !PROMISES_COPY.has(kind)) return base
const tail = state.copyWritten
? 'editor.draft.copy_kept'
: 'editor.draft.copy_not_kept'
return `${base} ${t(tail)}`
}
/** A failed reload of the server draft, phrased from the catalogue's closed kinds. */
export function loadFailureMessage(
t: Translate,
failure: CatalogueFailure,
): string {
if (failure === 'denied') return t('access_denied.body')
if (failure === 'core_unavailable')
return coreFailureMessage(t, 'sheet.core_unavailable')
return t(`catalogue.failure.${failure}`)
}
+92
View File
@@ -0,0 +1,92 @@
import type {
OfficialSheetAdmin,
PlatformRole,
SheetAdminRound,
SheetFailureKind,
} from '@faunosteo/supabase'
/**
* Why the draft is edited or only read (plan « Modes, permissions, layout »). A display
* rule: the server refuses every write it does not allow, this only decides what to show.
*/
export type EditorMode =
| 'edit'
| 'narrow'
| 'rights'
| 'frozen'
| 'historical'
| 'inactive'
/** Editing needs a viewport at least this wide (L15). */
export const MIN_EDIT_WIDTH = 1024
type ModeSheet = Pick<
OfficialSheetAdmin,
'status' | 'historical' | 'rounds' | 'draft_version'
>
/** Round states that freeze the draft on the server (`draft.frozen`). */
const FREEZING_ROUNDS: ReadonlySet<SheetAdminRound['status']> = new Set([
'verifying',
'open',
'approved',
])
/** The round that freezes the draft being edited, if any. */
export function freezingRound(sheet: ModeSheet): SheetAdminRound | null {
const draftId = sheet.draft_version?.version_id
const found = sheet.rounds.find(
(round) =>
round.version_id === draftId && FREEZING_ROUNDS.has(round.status),
)
return found ?? null
}
/** `grant_active` needs the editor capability AND the admin or owner role. */
export function hasEditRight(
editorGrant: boolean,
roles: readonly PlatformRole[],
): boolean {
return editorGrant && (roles.includes('admin') || roles.includes('owner'))
}
/**
* The reason the draft cannot be edited, strongest first: the account's own rights, then
* the sheet (archived, historical), the freezing round, last the screen width.
*/
export function editorMode(input: {
hasRight: boolean
sheet: ModeSheet
wide: boolean
}): EditorMode {
const { sheet } = input
if (!input.hasRight) return 'rights'
if (sheet.status !== 'active') return 'inactive'
if (sheet.historical) return 'historical'
if (freezingRound(sheet)) return 'frozen'
return input.wide ? 'edit' : 'narrow'
}
/**
* A refused save names the state the page missed: the matching read-only mode replaces
* the edit mode (the page never retries or sends again by itself).
*/
export function modeAfterSave(
mode: EditorMode,
failure: SheetFailureKind | null,
): EditorMode {
if (mode !== 'edit' || failure === null) return mode
if (failure === 'draft.frozen') return 'frozen'
if (failure === 'sheet.inactive') return 'inactive'
return failure.startsWith('rights.') ? 'rights' : mode
}
/** Whether the sheet's draft can be edited at a wide enough screen: « Ouvrir » or « Consulter ». */
export function draftEditable(hasRight: boolean, sheet: ModeSheet): boolean {
return editorMode({ hasRight, sheet, wide: true }) === 'edit'
}
/** The store may use a recovery copy where the account could save (also at a narrow screen). */
export function storeCanEdit(mode: EditorMode): boolean {
return mode === 'edit' || mode === 'narrow'
}
+10
View File
@@ -0,0 +1,10 @@
import { readCopy } from './state/recovery'
/**
* The sheet a stored copy of another draft belongs to, for « Ouvrir ce brouillon »; null
* when the copy cannot be read (it can still be abandoned).
*/
export function copySheetId(userId: string, versionId: string): string | null {
const stored = readCopy(userId, versionId)
return stored.kind === 'copy' ? stored.copy.sheet_id : null
}
+37
View File
@@ -0,0 +1,37 @@
import type { TabTier } from './useViewport'
export const EDITOR_TABS = [
'general',
'views',
'annotations',
'findings',
'references',
'vertebrae',
'verification',
] as const
export type EditorTabId = (typeof EDITOR_TABS)[number]
/** Board 05: the tabs shown beside « Plus » at each width; the rest sit under « Plus ». */
const SHOWN: Record<TabTier, readonly EditorTabId[]> = {
wide: EDITOR_TABS,
medium: ['general', 'annotations', 'findings', 'verification'],
small: ['annotations', 'verification'],
}
export function tabsFor(tier: TabTier): {
shown: readonly EditorTabId[]
more: readonly EditorTabId[]
} {
const shown = SHOWN[tier]
return { shown, more: EDITOR_TABS.filter((tab) => !shown.includes(tab)) }
}
/** Props of every tab body (Lots F-J fill them): read-only flag, tab jump, sheet reload. */
export interface EditorTabProps {
/** Not editable now: a mode that cannot save, or a recovery offer not answered yet. */
readOnly: boolean
/** « Aller à … » and any link between tabs. */
onSelectTab: (tab: EditorTabId) => void
/** Reads the sheet's overview again (after a submission: the round freezes the draft). */
reloadSheet: () => void
}
+24
View File
@@ -0,0 +1,24 @@
import { useEffect, useState } from 'react'
import { MIN_EDIT_WIDTH } from './mode'
/** The tab lists of board 05: all, the four of 640 px and over, the two below. */
export type TabTier = 'wide' | 'medium' | 'small'
const MEDIUM_WIDTH = 640
function tierOf(width: number): TabTier {
if (width >= MIN_EDIT_WIDTH) return 'wide'
return width >= MEDIUM_WIDTH ? 'medium' : 'small'
}
/** The viewport's tier, kept in step with the window (resize and rotation). */
export function useTabTier(): TabTier {
const [tier, setTier] = useState<TabTier>(() => tierOf(window.innerWidth))
useEffect(() => {
const update = () => setTier(tierOf(window.innerWidth))
update()
window.addEventListener('resize', update)
return () => window.removeEventListener('resize', update)
}, [])
return tier
}