feat(app): sheet draft editor route, modes, recovery offer and conflict dialog
This commit is contained in:
1 parent
b14509f221
commit
6aba9711f0
44 files changed
+3143
-13
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import Button from '@/components/ui/Button'
|
||||
import Dialog from '@/components/ui/Dialog'
|
||||
|
||||
export interface ConfirmDialogProps {
|
||||
open: boolean
|
||||
title: string
|
||||
body: string
|
||||
confirmLabel: string
|
||||
cancelLabel: string
|
||||
onConfirm: () => void
|
||||
onCancel: () => void
|
||||
pending?: boolean
|
||||
}
|
||||
|
||||
/** A question with its two answers; the cancel answer changes nothing. */
|
||||
export default function ConfirmDialog({
|
||||
open,
|
||||
title,
|
||||
body,
|
||||
confirmLabel,
|
||||
cancelLabel,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
pending = false,
|
||||
}: ConfirmDialogProps) {
|
||||
return (
|
||||
<Dialog
|
||||
open={open}
|
||||
onClose={onCancel}
|
||||
title={title}
|
||||
description={
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">{body}</p>
|
||||
}
|
||||
size="sm"
|
||||
>
|
||||
<div className="mt-4 flex flex-wrap justify-end gap-2">
|
||||
<Button type="button" variant="ghost" onClick={onCancel}>
|
||||
{cancelLabel}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="danger"
|
||||
aria-disabled={pending}
|
||||
onClick={pending ? undefined : onConfirm}
|
||||
>
|
||||
{confirmLabel}
|
||||
</Button>
|
||||
</div>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
import { useState } from 'react'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Dialog from '@/components/ui/Dialog'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { personName } from '@/admin/labels'
|
||||
import { formatParisDateTime } from '@/editor/format'
|
||||
import { loadFailureMessage, saveFailureMessage } from '@/editor/messages'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import type {
|
||||
ConflictLastSave,
|
||||
ConflictState,
|
||||
} from '@/editor/state/editorTypes'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
type Translate = ReturnType<typeof useT>
|
||||
type Step = 'choose' | 'reload' | 'keep'
|
||||
type Known = Extract<ConflictState, { status: 'known' }>
|
||||
|
||||
/** Who and when, only when the server tells it (never invented). */
|
||||
function whoAndWhen(
|
||||
t: Translate,
|
||||
locale: Locale,
|
||||
last: ConflictLastSave | null,
|
||||
) {
|
||||
if (!last) return null
|
||||
const date = formatParisDateTime(last.savedAt, locale)
|
||||
if (last.byCaller) {
|
||||
return {
|
||||
line: t('editor.draft.conflict.by_you').replace('{date}', date),
|
||||
who: null,
|
||||
date,
|
||||
}
|
||||
}
|
||||
const who = personName(t, last.savedBy)
|
||||
const line = t('editor.draft.conflict.by_other')
|
||||
.replace('{name}', who)
|
||||
.replace('{date}', date)
|
||||
return { line, who, date }
|
||||
}
|
||||
|
||||
function conflictTitle(t: Translate, conflict: ConflictState): string {
|
||||
if (conflict.status === 'known') {
|
||||
return t('editor.draft.conflict.known').replace(
|
||||
'{revision}',
|
||||
String(conflict.revision),
|
||||
)
|
||||
}
|
||||
return t(
|
||||
conflict.status === 'reading'
|
||||
? 'editor.draft.conflict.reading_title'
|
||||
: 'editor.draft.conflict.unknown',
|
||||
)
|
||||
}
|
||||
|
||||
function reloadQuestion(t: Translate, conflict: ConflictState): string {
|
||||
if (conflict.status !== 'known')
|
||||
return t('editor.draft.conflict.reload_unknown_ask')
|
||||
return t('editor.draft.conflict.reload_ask').replace(
|
||||
'{revision}',
|
||||
String(conflict.revision),
|
||||
)
|
||||
}
|
||||
|
||||
function keepQuestion(t: Translate, locale: Locale, conflict: Known): string {
|
||||
const info = whoAndWhen(t, locale, conflict.lastSave)
|
||||
const revision = String(conflict.revision)
|
||||
if (!info)
|
||||
return t('editor.draft.conflict.keep_ask').replace('{revision}', revision)
|
||||
const who = info.who ?? t('editor.draft.conflict.by_you_short')
|
||||
return t('editor.draft.conflict.keep_ask_by')
|
||||
.replace('{revision}', revision)
|
||||
.replace('{who}', who)
|
||||
.replace('{date}', info.date)
|
||||
}
|
||||
|
||||
/**
|
||||
* The conflict dialog (C29): the page read the server draft after `draft_conflict`. The
|
||||
* known variant names the revision N, and its author and time only when the server says;
|
||||
* the unknown variant (failed read) offers « Recharger » and « Réessayer » only. Both
|
||||
* overwrites ask first; the server's atomic revision check decides at write time.
|
||||
*/
|
||||
export default function ConflictDialog() {
|
||||
const t = useT()
|
||||
const conflict = useEditorStore((s) => s.conflict)
|
||||
const dismiss = () => useEditorStore.getState().dismissConflict()
|
||||
return (
|
||||
<Dialog
|
||||
open={conflict !== null}
|
||||
onClose={dismiss}
|
||||
title={conflict ? conflictTitle(t, conflict) : ''}
|
||||
size="md"
|
||||
>
|
||||
{conflict && (
|
||||
<ConflictBody
|
||||
key={`${conflict.status}:${conflict.status === 'known' ? conflict.revision : ''}`}
|
||||
conflict={conflict}
|
||||
/>
|
||||
)}
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
||||
function ConflictBody({ conflict }: { conflict: ConflictState }) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const [step, setStep] = useState<Step>('choose')
|
||||
const [pending, setPending] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const store = useEditorStore
|
||||
const known = conflict.status === 'known'
|
||||
|
||||
const run = async (action: () => Promise<string | null>) => {
|
||||
setPending(true)
|
||||
setError(null)
|
||||
setError(await action())
|
||||
setPending(false)
|
||||
}
|
||||
const reload = () =>
|
||||
run(async () => {
|
||||
const result = await store
|
||||
.getState()
|
||||
.reloadServerDraft({ confirmed: true })
|
||||
return !result.ok && result.reason === 'load'
|
||||
? loadFailureMessage(t, result.failure)
|
||||
: null
|
||||
})
|
||||
const keep = () =>
|
||||
run(async () => {
|
||||
const result = await store.getState().keepMine({ confirmed: true })
|
||||
if (
|
||||
result.ok ||
|
||||
result.reason !== 'server' ||
|
||||
result.kind === 'draft_conflict'
|
||||
)
|
||||
return null
|
||||
const { unsaved, copyWritten } = store.getState()
|
||||
return saveFailureMessage(t, result.kind, { unsaved, copyWritten })
|
||||
})
|
||||
|
||||
if (conflict.status === 'reading') {
|
||||
return (
|
||||
<p role="status" className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.draft.conflict.reading')}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
if (step !== 'choose') {
|
||||
const ask =
|
||||
step === 'reload'
|
||||
? reloadQuestion(t, conflict)
|
||||
: keepQuestion(t, locale, conflict as Known)
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<p className="text-sm text-[var(--color-ink)]">{ask}</p>
|
||||
{error && <Notice tone="error">{error}</Notice>}
|
||||
<div className="flex flex-wrap justify-end gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
disabled={pending}
|
||||
onClick={() => setStep('choose')}
|
||||
>
|
||||
{t('common.cancel')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="danger"
|
||||
disabled={pending}
|
||||
onClick={() => void (step === 'reload' ? reload() : keep())}
|
||||
>
|
||||
{t(
|
||||
step === 'reload'
|
||||
? 'editor.draft.conflict.reload'
|
||||
: 'editor.draft.conflict.keep',
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const info = known ? whoAndWhen(t, locale, conflict.lastSave) : null
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{info && <p className="text-sm text-[var(--color-ink)]">{info.line}</p>}
|
||||
{error && <Notice tone="error">{error}</Notice>}
|
||||
<div className="flex flex-wrap justify-end gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
onClick={() => store.getState().dismissConflict()}
|
||||
>
|
||||
{t('common.cancel')}
|
||||
</Button>
|
||||
{!known && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
onClick={() => void store.getState().retryConflictRead()}
|
||||
>
|
||||
{t('editor.draft.conflict.retry')}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
onClick={() => setStep('reload')}
|
||||
>
|
||||
{t('editor.draft.conflict.reload')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
disabled={!known}
|
||||
aria-describedby={known ? undefined : 'conflict-keep-hint'}
|
||||
onClick={() => setStep('keep')}
|
||||
>
|
||||
{t('editor.draft.conflict.keep')}
|
||||
</Button>
|
||||
</div>
|
||||
{!known && (
|
||||
<p
|
||||
id="conflict-keep-hint"
|
||||
className="text-right text-sm text-[var(--color-ink-muted)]"
|
||||
>
|
||||
{t('editor.draft.conflict.keep_hint')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import { useState } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { formatParisTime } from '@/editor/format'
|
||||
import { copySheetId } from '@/editor/otherCopies'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import type { StoredCopyInfo } from '@/editor/state/recovery'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import ConfirmDialog from './ConfirmDialog'
|
||||
|
||||
type Translate = ReturnType<typeof useT>
|
||||
|
||||
/** « <sheet_name> · révision R · <heure> », each part only when the copy tells it. */
|
||||
function copyLine(t: Translate, locale: Locale, copy: StoredCopyInfo): string {
|
||||
return [
|
||||
copy.sheetName ?? t('editor.draft.copy.unreadable'),
|
||||
copy.revision === null
|
||||
? null
|
||||
: t('editor.draft.copy.revision').replace(
|
||||
'{revision}',
|
||||
String(copy.revision),
|
||||
),
|
||||
copy.writtenAt === null ? null : formatParisTime(copy.writtenAt, locale),
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' · ')
|
||||
}
|
||||
|
||||
/**
|
||||
* The write failed (C28: cap of two other copies, or a full storage): the account's other
|
||||
* copies are listed, each openable and abandonable (confirmed); abandoning writes the
|
||||
* current copy again at once. Nothing is evicted by itself.
|
||||
*/
|
||||
export default function CopyIssueNotice() {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const issue = useEditorStore((s) => s.copyIssue)
|
||||
const copies = useEditorStore((s) => s.otherCopies)
|
||||
const userId = useEditorStore((s) => s.userId)
|
||||
const [asking, setAsking] = useState<StoredCopyInfo | null>(null)
|
||||
if (issue === null) return null
|
||||
|
||||
const abandon = () => {
|
||||
if (asking)
|
||||
useEditorStore
|
||||
.getState()
|
||||
.abandonOtherCopy(asking.versionId, { confirmed: true })
|
||||
setAsking(null)
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<Notice tone="error">
|
||||
<p>{t(`editor.draft.copy_issue.${issue}`)}</p>
|
||||
<ul className="mt-2 space-y-2">
|
||||
{copies.map((copy) => {
|
||||
const sheetId = userId ? copySheetId(userId, copy.versionId) : null
|
||||
return (
|
||||
<li
|
||||
key={copy.versionId}
|
||||
className="flex flex-wrap items-center gap-2"
|
||||
>
|
||||
<span>{copyLine(t, locale, copy)}</span>
|
||||
{sheetId && (
|
||||
<Link
|
||||
to={`/app/admin/sheets/${sheetId}/draft`}
|
||||
className="text-sm underline"
|
||||
>
|
||||
{t('editor.draft.copy.open')}
|
||||
</Link>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
onClick={() => setAsking(copy)}
|
||||
>
|
||||
{t('editor.draft.copy.abandon')}
|
||||
</Button>
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
{issue === 'quota' && (
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
className="mt-2"
|
||||
onClick={() => useEditorStore.getState().retryCopyWrite()}
|
||||
>
|
||||
{t('common.retry')}
|
||||
</Button>
|
||||
)}
|
||||
</Notice>
|
||||
<ConfirmDialog
|
||||
open={asking !== null}
|
||||
title={t('editor.draft.copy.abandon')}
|
||||
body={
|
||||
asking?.revision != null && asking.writtenAt !== null
|
||||
? t('editor.draft.recovery.ignore_body')
|
||||
.replace('{revision}', String(asking.revision))
|
||||
.replace('{time}', formatParisTime(asking.writtenAt, locale))
|
||||
: t('editor.draft.recovery.ignore_unreadable_body')
|
||||
}
|
||||
confirmLabel={t('editor.draft.recovery.ignore_confirm')}
|
||||
cancelLabel={t('common.cancel')}
|
||||
onConfirm={abandon}
|
||||
onCancel={() => setAsking(null)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
import { useState } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import type { OfficialSheetAdmin } from '@faunosteo/supabase'
|
||||
import Badge from '@/components/ui/Badge'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Icon from '@/components/ui/Icon'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { localized } from '@/admin/labels'
|
||||
import { loadFailureMessage } from '@/editor/messages'
|
||||
import type { EditorMode } from '@/editor/mode'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import { usePageHeading } from '@/app/usePageHeading'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import ConfirmDialog from './ConfirmDialog'
|
||||
|
||||
type Translate = ReturnType<typeof useT>
|
||||
|
||||
interface Status {
|
||||
copyIssue: 'cap' | 'quota' | null
|
||||
saving: boolean
|
||||
unsaved: boolean
|
||||
revision: number
|
||||
}
|
||||
|
||||
/** Board 04's line; only the copy failures replace it (the copy is temporary, this tab's). */
|
||||
function statusText(t: Translate, status: Status): string {
|
||||
if (status.copyIssue !== null)
|
||||
return t(`editor.draft.status.copy_${status.copyIssue}`)
|
||||
if (status.saving) return t('editor.draft.status.saving')
|
||||
const revision = t('editor.draft.status.revision').replace(
|
||||
'{revision}',
|
||||
String(status.revision),
|
||||
)
|
||||
return status.unsaved
|
||||
? `${revision} · ${t('editor.draft.status.unsaved')}`
|
||||
: revision
|
||||
}
|
||||
|
||||
/** Breadcrumb of board 04: Administration › Fiches officielles › the sheet › the draft. */
|
||||
function Breadcrumb({
|
||||
sheet,
|
||||
title,
|
||||
}: {
|
||||
sheet: OfficialSheetAdmin
|
||||
title: string
|
||||
}) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const link = 'underline-offset-2 hover:underline'
|
||||
return (
|
||||
<nav
|
||||
aria-label={t('catalogue.breadcrumb')}
|
||||
className="text-sm text-[var(--color-ink-muted)]"
|
||||
>
|
||||
<Link to="/app/admin" className={link}>
|
||||
{t('shell.nav.admin')}
|
||||
</Link>
|
||||
<span aria-hidden="true"> › </span>
|
||||
<Link to="/app/admin/sheets" className={link}>
|
||||
{t('admin.sheets.title')}
|
||||
</Link>
|
||||
<span aria-hidden="true"> › </span>
|
||||
<Link to={`/app/admin/sheets/${sheet.sheet_id}`} className={link}>
|
||||
{localized(sheet.name, locale)}
|
||||
</Link>
|
||||
<span aria-hidden="true"> › </span>
|
||||
<span aria-current="page">{title}</span>
|
||||
</nav>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The draft's header: title and badges, the revision line with the unsaved state, and in
|
||||
* edit mode « Annuler les modifications » (confirmed) and « Enregistrer ». « Annuler les
|
||||
* modifications » is the store's `discardChanges`; with an older base it reloads.
|
||||
*/
|
||||
export default function DraftHeader({
|
||||
sheet,
|
||||
mode,
|
||||
}: {
|
||||
sheet: OfficialSheetAdmin
|
||||
mode: EditorMode
|
||||
}) {
|
||||
const t = useT()
|
||||
const heading = usePageHeading()
|
||||
const unsaved = useEditorStore((s) => s.unsaved)
|
||||
const saving = useEditorStore((s) => s.saving)
|
||||
const blocked = useEditorStore((s) => s.recovery !== 'none')
|
||||
const revision = useEditorStore((s) => s.savedRevision)
|
||||
const copyIssue = useEditorStore((s) => s.copyIssue)
|
||||
const [asking, setAsking] = useState(false)
|
||||
const [failure, setFailure] = useState<string | null>(null)
|
||||
const title = t('editor.draft.title').replace(
|
||||
'{number}',
|
||||
String(sheet.draft_version?.version_number ?? ''),
|
||||
)
|
||||
|
||||
const discard = async () => {
|
||||
setAsking(false)
|
||||
const result = await useEditorStore
|
||||
.getState()
|
||||
.discardChanges({ confirmed: true })
|
||||
setFailure(
|
||||
!result.ok && result.reason === 'load'
|
||||
? loadFailureMessage(t, result.failure)
|
||||
: null,
|
||||
)
|
||||
}
|
||||
return (
|
||||
<header className="space-y-3">
|
||||
<Breadcrumb sheet={sheet} title={title} />
|
||||
<div className="flex flex-wrap items-center gap-x-3 gap-y-2">
|
||||
<h1
|
||||
ref={heading}
|
||||
tabIndex={-1}
|
||||
className="outline-none font-serif text-2xl text-[var(--color-ink)] sm:text-3xl"
|
||||
>
|
||||
{title}
|
||||
</h1>
|
||||
<Badge>{t('admin.version_status.draft')}</Badge>
|
||||
{sheet.working_copy && <Badge>{t('admin.badge.working_copy')}</Badge>}
|
||||
{mode !== 'edit' && (
|
||||
<Badge>
|
||||
<Icon name="lock" size={14} className="mr-1" />
|
||||
{t('editor.draft.read_only')}
|
||||
</Badge>
|
||||
)}
|
||||
<p
|
||||
role="status"
|
||||
className="ml-auto text-sm text-[var(--color-ink-muted)]"
|
||||
>
|
||||
{statusText(t, { copyIssue, saving, unsaved, revision })}
|
||||
</p>
|
||||
{mode === 'edit' && (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
disabled={!unsaved || saving || blocked}
|
||||
onClick={() => setAsking(true)}
|
||||
>
|
||||
{t('editor.draft.discard')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
disabled={!unsaved || saving || blocked}
|
||||
onClick={() => void useEditorStore.getState().save()}
|
||||
>
|
||||
{t('common.save')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{failure && <Notice tone="error">{failure}</Notice>}
|
||||
<ConfirmDialog
|
||||
open={asking}
|
||||
title={t('editor.draft.discard')}
|
||||
body={t('editor.draft.discard_ask')}
|
||||
confirmLabel={t('editor.draft.discard_confirm')}
|
||||
cancelLabel={t('common.cancel')}
|
||||
onConfirm={() => void discard()}
|
||||
onCancel={() => setAsking(false)}
|
||||
/>
|
||||
</header>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { ComponentType } from 'react'
|
||||
import type { EditorTabId, EditorTabProps } from '@/editor/tabs'
|
||||
import AnnotationsTab from './tabs/AnnotationsTab'
|
||||
import FindingsTab from './tabs/FindingsTab'
|
||||
import GeneralTab from './tabs/GeneralTab'
|
||||
import ReferencesTab from './tabs/ReferencesTab'
|
||||
import VerificationTab from './tabs/VerificationTab'
|
||||
import VertebraeTab from './tabs/VertebraeTab'
|
||||
import ViewsTab from './tabs/ViewsTab'
|
||||
|
||||
/** One component per tab (Lots F-J fill them); only the active one is mounted. */
|
||||
const BODIES: Record<EditorTabId, ComponentType<EditorTabProps>> = {
|
||||
general: GeneralTab,
|
||||
views: ViewsTab,
|
||||
annotations: AnnotationsTab,
|
||||
findings: FindingsTab,
|
||||
references: ReferencesTab,
|
||||
vertebrae: VertebraeTab,
|
||||
verification: VerificationTab,
|
||||
}
|
||||
|
||||
export default function EditorTabBody({
|
||||
tab,
|
||||
...props
|
||||
}: EditorTabProps & { tab: EditorTabId }) {
|
||||
const Body = BODIES[tab]
|
||||
return <Body {...props} />
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import { useState, type KeyboardEvent } from 'react'
|
||||
import { tabsFor, type EditorTabId } from '@/editor/tabs'
|
||||
import type { TabTier } from '@/editor/useViewport'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
export const PANEL_ID = 'editor-panel'
|
||||
export const tabDomId = (tab: EditorTabId) => `editor-tab-${tab}`
|
||||
|
||||
const BASE =
|
||||
'min-h-11 whitespace-nowrap border-b-2 px-3 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]'
|
||||
const ACTIVE =
|
||||
'border-[var(--color-accent)] font-medium text-[var(--color-accent-strong)]'
|
||||
const IDLE =
|
||||
'border-transparent text-[var(--color-ink-muted)] hover:text-[var(--color-ink)]'
|
||||
|
||||
interface EditorTabsProps {
|
||||
active: EditorTabId
|
||||
tier: TabTier
|
||||
onSelect: (tab: EditorTabId) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* The draft's tabs (boards 04 and 05): all seven wide, the static lists of 640 px and over
|
||||
* and below it narrow, with « Plus » opening a disclosure of the others (no measurement).
|
||||
*/
|
||||
export default function EditorTabs({
|
||||
active,
|
||||
tier,
|
||||
onSelect,
|
||||
}: EditorTabsProps) {
|
||||
const t = useT()
|
||||
const { shown, more } = tabsFor(tier)
|
||||
const [open, setOpen] = useState(false)
|
||||
const moreActive = more.includes(active)
|
||||
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
const at = shown.indexOf(active)
|
||||
const step =
|
||||
event.key === 'ArrowRight' ? 1 : event.key === 'ArrowLeft' ? -1 : 0
|
||||
if (step === 0 || at < 0) return
|
||||
event.preventDefault()
|
||||
const next = shown[(at + step + shown.length) % shown.length]
|
||||
onSelect(next)
|
||||
document.getElementById(tabDomId(next))?.focus()
|
||||
}
|
||||
const pick = (tab: EditorTabId) => {
|
||||
onSelect(tab)
|
||||
setOpen(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="flex items-end gap-1 overflow-x-auto border-b border-[var(--color-border)]">
|
||||
<div
|
||||
role="tablist"
|
||||
aria-label={t('editor.tab.list')}
|
||||
onKeyDown={onKeyDown}
|
||||
className="flex gap-1"
|
||||
>
|
||||
{shown.map((tab) => (
|
||||
<button
|
||||
key={tab}
|
||||
id={tabDomId(tab)}
|
||||
type="button"
|
||||
role="tab"
|
||||
aria-selected={tab === active}
|
||||
aria-controls={PANEL_ID}
|
||||
tabIndex={tab === active ? 0 : -1}
|
||||
onClick={() => onSelect(tab)}
|
||||
className={`-mb-px ${BASE} ${tab === active ? ACTIVE : IDLE}`}
|
||||
>
|
||||
{t(`editor.tab.${tab}`)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{more.length > 0 && (
|
||||
<button
|
||||
type="button"
|
||||
aria-expanded={open}
|
||||
aria-controls="editor-tabs-more"
|
||||
onClick={() => setOpen((value) => !value)}
|
||||
className={`-mb-px ${BASE} ${moreActive ? ACTIVE : IDLE}`}
|
||||
>
|
||||
{t('editor.tab.more')}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{open && (
|
||||
<ul
|
||||
id="editor-tabs-more"
|
||||
className="flex flex-wrap gap-1 border-b border-[var(--color-border)] py-1"
|
||||
>
|
||||
{more.map((tab) => (
|
||||
<li key={tab}>
|
||||
<button
|
||||
id={tabDomId(tab)}
|
||||
type="button"
|
||||
aria-pressed={tab === active}
|
||||
onClick={() => pick(tab)}
|
||||
className={`rounded-md px-3 ${BASE} border-b-0 ${tab === active ? ACTIVE : IDLE}`}
|
||||
>
|
||||
{t(`editor.tab.${tab}`)}
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import { Link } from 'react-router-dom'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { roundLabel } from '@/admin/labels'
|
||||
import { freezingRound, type EditorMode } from '@/editor/mode'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import type { OfficialSheetAdmin } from '@faunosteo/supabase'
|
||||
|
||||
type Translate = ReturnType<typeof useT>
|
||||
|
||||
/** « Revue n° N · vérification en cours », else the round's status label. */
|
||||
function frozenText(t: Translate, sheet: OfficialSheetAdmin): string {
|
||||
const round = freezingRound(sheet)
|
||||
if (!round) return t('editor.draft.mode.frozen_generic')
|
||||
const number = t('admin.overview.round').replace(
|
||||
'{round}',
|
||||
String(round.round),
|
||||
)
|
||||
if (round.status === 'verifying') {
|
||||
return t('editor.draft.mode.frozen_verifying').replace('{round}', number)
|
||||
}
|
||||
return `${number} · ${roundLabel(t, round).title}`
|
||||
}
|
||||
|
||||
function modeText(
|
||||
t: Translate,
|
||||
mode: EditorMode,
|
||||
sheet: OfficialSheetAdmin,
|
||||
): string {
|
||||
return mode === 'frozen'
|
||||
? frozenText(t, sheet)
|
||||
: t(`editor.draft.mode.${mode}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Why the draft is read-only (boards 05): the reason alone, nothing repeated (L15). The
|
||||
* frozen state links to the overview; a recovery copy that this state cannot use is named.
|
||||
*/
|
||||
export default function ModeNotice({
|
||||
mode,
|
||||
sheet,
|
||||
}: {
|
||||
mode: EditorMode
|
||||
sheet: OfficialSheetAdmin
|
||||
}) {
|
||||
const t = useT()
|
||||
const unusableCopy = useEditorStore((s) => s.unusableCopy)
|
||||
if (mode === 'edit') return null
|
||||
return (
|
||||
<Notice tone="info">
|
||||
<p className="font-medium">{modeText(t, mode, sheet)}</p>
|
||||
{unusableCopy && (
|
||||
<p className="mt-1">{t('editor.draft.mode.unusable_copy')}</p>
|
||||
)}
|
||||
{mode === 'frozen' && (
|
||||
<Link
|
||||
to={`/app/admin/sheets/${sheet.sheet_id}`}
|
||||
className="mt-1 inline-block underline"
|
||||
>
|
||||
{t('editor.draft.overview_link')}
|
||||
</Link>
|
||||
)}
|
||||
</Notice>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
import { useState } from 'react'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { formatParisTime } from '@/editor/format'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import type { RecoveryOffer } from '@/editor/state/editorTypes'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import ConfirmDialog from './ConfirmDialog'
|
||||
|
||||
type Translate = ReturnType<typeof useT>
|
||||
|
||||
function named(text: string, offer: RecoveryOffer, locale: Locale): string {
|
||||
return text
|
||||
.replace('{revision}', String(offer.revision))
|
||||
.replace('{time}', formatParisTime(offer.writtenAt, locale))
|
||||
}
|
||||
|
||||
/** The notice text: unreadable (the loss named), refused by the core, or the offer. */
|
||||
function noticeText(
|
||||
t: Translate,
|
||||
locale: Locale,
|
||||
offer: RecoveryOffer | null,
|
||||
refused: string | null,
|
||||
): string {
|
||||
if (!offer || refused === 'unreadable')
|
||||
return t('editor.draft.recovery.unreadable')
|
||||
if (refused !== null)
|
||||
return t('editor.draft.recovery.refused').replace('{kind}', refused)
|
||||
const base = named(t('editor.draft.recovery.offer'), offer, locale)
|
||||
if (offer.revision >= offer.serverRevision) return base
|
||||
const older = t('editor.draft.recovery.older')
|
||||
.replace('{revision}', String(offer.revision))
|
||||
.replace('{server}', String(offer.serverRevision))
|
||||
return `${base} ${older}`
|
||||
}
|
||||
|
||||
/**
|
||||
* The blocking offer under the header (C13, C27, C28): a temporary copy of this tab, not a
|
||||
* server save. The store refuses every change until « Ignorer la copie » or a restore; the
|
||||
* page only phrases it. An unreadable copy offers « Ignorer la copie » alone, a copy the
|
||||
* core refused shows « Restaurer la copie » disabled.
|
||||
*/
|
||||
export default function RecoveryNotice() {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const recovery = useEditorStore((s) => s.recovery)
|
||||
const offer = useEditorStore((s) => s.offer)
|
||||
const refused = useEditorStore((s) => s.refusedKind)
|
||||
const [asking, setAsking] = useState(false)
|
||||
if (recovery === 'none') return null
|
||||
|
||||
const unreadable = !offer || refused === 'unreadable'
|
||||
const busy = recovery === 'restoring'
|
||||
const question = unreadable
|
||||
? t('editor.draft.recovery.ignore_unreadable_body')
|
||||
: named(t('editor.draft.recovery.ignore_body'), offer, locale)
|
||||
const ignore = () => {
|
||||
useEditorStore.getState().ignoreCopy({ confirmed: true })
|
||||
setAsking(false)
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<Notice tone={refused === null ? 'info' : 'error'}>
|
||||
<p>{noticeText(t, locale, offer, refused)}</p>
|
||||
<div className="mt-2 flex flex-wrap gap-2">
|
||||
{!unreadable && (
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
disabled={busy || refused !== null}
|
||||
onClick={() => void useEditorStore.getState().restoreCopy()}
|
||||
>
|
||||
{t('editor.draft.recovery.restore')}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
disabled={busy}
|
||||
onClick={() => setAsking(true)}
|
||||
>
|
||||
{t('editor.draft.recovery.ignore')}
|
||||
</Button>
|
||||
</div>
|
||||
</Notice>
|
||||
<ConfirmDialog
|
||||
open={asking}
|
||||
title={t('editor.draft.recovery.ignore')}
|
||||
body={question}
|
||||
confirmLabel={t('editor.draft.recovery.ignore_confirm')}
|
||||
cancelLabel={t('common.cancel')}
|
||||
onConfirm={ignore}
|
||||
onCancel={() => setAsking(false)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import Button, { type ButtonProps } from '@/components/ui/Button'
|
||||
|
||||
/** An action of a panel: absent from the page while the panel is read-only. */
|
||||
export default function ActionButton({
|
||||
readOnly,
|
||||
...props
|
||||
}: ButtonProps & { readOnly: boolean }) {
|
||||
return readOnly ? null : <Button type="button" {...props} />
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/** A label and its value as text: what a field shows when it cannot be edited. */
|
||||
export default function ReadOnlyValue({
|
||||
id,
|
||||
label,
|
||||
value,
|
||||
multiline = false,
|
||||
}: {
|
||||
id: string
|
||||
label: string
|
||||
value: string
|
||||
multiline?: boolean
|
||||
}) {
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
<p
|
||||
id={`${id}-label`}
|
||||
className="text-sm font-medium text-[var(--color-ink)]"
|
||||
>
|
||||
{label}
|
||||
</p>
|
||||
<p
|
||||
id={id}
|
||||
aria-labelledby={`${id}-label`}
|
||||
className={`text-sm text-[var(--color-ink-muted)] ${multiline ? 'whitespace-pre-wrap' : ''}`}
|
||||
>
|
||||
{value === '' ? '—' : value}
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Select from '@/components/ui/Select'
|
||||
import ReadOnlyValue from './ReadOnlyValue'
|
||||
|
||||
export interface SelectOption {
|
||||
value: string
|
||||
label: string
|
||||
}
|
||||
|
||||
export interface SelectFieldProps {
|
||||
id: string
|
||||
label: string
|
||||
value: string
|
||||
options: readonly SelectOption[]
|
||||
/** A choice is a confirmation: it commits at once. */
|
||||
onCommit: (value: string) => void
|
||||
readOnly?: boolean
|
||||
hint?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
/** A choice among fixed options; read-only, the chosen option's label as text. */
|
||||
export default function SelectField({
|
||||
id,
|
||||
label,
|
||||
value,
|
||||
options,
|
||||
onCommit,
|
||||
readOnly,
|
||||
hint,
|
||||
error,
|
||||
}: SelectFieldProps) {
|
||||
if (readOnly) {
|
||||
const chosen = options.find((option) => option.value === value)
|
||||
return <ReadOnlyValue id={id} label={label} value={chosen?.label ?? ''} />
|
||||
}
|
||||
return (
|
||||
<FormField id={id} label={label} hint={hint} error={error}>
|
||||
<Select
|
||||
id={id}
|
||||
value={value}
|
||||
onChange={(e) => onCommit(e.target.value)}
|
||||
aria-invalid={Boolean(error)}
|
||||
aria-describedby={fieldDescribedBy(id, hint, error)}
|
||||
>
|
||||
{options.map((option) => (
|
||||
<option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</FormField>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import { useEffect, useRef, useState, type KeyboardEvent } from 'react'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Input from '@/components/ui/Input'
|
||||
import Textarea from '@/components/ui/Textarea'
|
||||
import ReadOnlyValue from './ReadOnlyValue'
|
||||
|
||||
export interface TextFieldProps {
|
||||
id: string
|
||||
label: string
|
||||
/** The committed value (the document's). */
|
||||
value: string
|
||||
/** One commit per change: blur with a changed value, Enter (single line), unmount. */
|
||||
onCommit: (value: string) => void
|
||||
/** Shows the value as text, no control (every mode that cannot edit). */
|
||||
readOnly?: boolean
|
||||
multiline?: boolean
|
||||
maxLength?: number
|
||||
hint?: string
|
||||
/** The refusal phrased by the caller, shown next to the control. */
|
||||
error?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* A text field that commits once per change, never per keystroke (BDR-050): typing stays
|
||||
* local, blur or Enter commits a changed value, and a value still pending when the field
|
||||
* unmounts (a mode switch) is committed by the cleanup. Read-only, it is plain text.
|
||||
*/
|
||||
export default function TextField(props: TextFieldProps) {
|
||||
if (props.readOnly) {
|
||||
return (
|
||||
<ReadOnlyValue
|
||||
id={props.id}
|
||||
label={props.label}
|
||||
value={props.value}
|
||||
multiline={props.multiline}
|
||||
/>
|
||||
)
|
||||
}
|
||||
return <EditableText {...props} />
|
||||
}
|
||||
|
||||
function EditableText({
|
||||
id,
|
||||
label,
|
||||
value,
|
||||
onCommit,
|
||||
multiline,
|
||||
maxLength,
|
||||
hint,
|
||||
error,
|
||||
}: TextFieldProps) {
|
||||
const [draft, setDraft] = useState(value)
|
||||
const [seen, setSeen] = useState(value)
|
||||
// A new committed value (undo, reload, restore) replaces what was typed.
|
||||
if (seen !== value) {
|
||||
setSeen(value)
|
||||
setDraft(value)
|
||||
}
|
||||
const latest = useRef({ draft, value, onCommit })
|
||||
useEffect(() => {
|
||||
latest.current = { draft, value, onCommit }
|
||||
})
|
||||
useEffect(
|
||||
() => () => {
|
||||
const pending = latest.current
|
||||
if (pending.draft !== pending.value) pending.onCommit(pending.draft)
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
const commit = () => {
|
||||
if (draft !== value) onCommit(draft)
|
||||
}
|
||||
const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
||||
if (event.key === 'Enter' && !event.nativeEvent.isComposing) commit()
|
||||
}
|
||||
const shared = {
|
||||
id,
|
||||
value: draft,
|
||||
maxLength,
|
||||
onChange: (e: { target: { value: string } }) => setDraft(e.target.value),
|
||||
onBlur: commit,
|
||||
'aria-invalid': Boolean(error),
|
||||
'aria-describedby': fieldDescribedBy(id, hint, error),
|
||||
}
|
||||
return (
|
||||
<FormField id={id} label={label} hint={hint} error={error}>
|
||||
{multiline ? (
|
||||
<Textarea {...shared} />
|
||||
) : (
|
||||
<Input {...shared} onKeyDown={onKeyDown} />
|
||||
)}
|
||||
</FormField>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Placeholder: Lot H replaces the body and keeps the props contract (`EditorTabProps`). */
|
||||
export default function AnnotationsTab(_props: EditorTabProps) {
|
||||
const t = useT()
|
||||
return (
|
||||
<section
|
||||
data-testid="editor-tab-annotations"
|
||||
aria-label={t('editor.tab.annotations')}
|
||||
className="min-h-24"
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Placeholder: Lot I replaces the body and keeps the props contract (`EditorTabProps`). */
|
||||
export default function FindingsTab(_props: EditorTabProps) {
|
||||
const t = useT()
|
||||
return (
|
||||
<section
|
||||
data-testid="editor-tab-findings"
|
||||
aria-label={t('editor.tab.findings')}
|
||||
className="min-h-24"
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Placeholder: Lot F replaces the body and keeps the props contract (`EditorTabProps`). */
|
||||
export default function GeneralTab(_props: EditorTabProps) {
|
||||
const t = useT()
|
||||
return (
|
||||
<section
|
||||
data-testid="editor-tab-general"
|
||||
aria-label={t('editor.tab.general')}
|
||||
className="min-h-24"
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Placeholder: Lot F replaces the body and keeps the props contract (`EditorTabProps`). */
|
||||
export default function ReferencesTab(_props: EditorTabProps) {
|
||||
const t = useT()
|
||||
return (
|
||||
<section
|
||||
data-testid="editor-tab-references"
|
||||
aria-label={t('editor.tab.references')}
|
||||
className="min-h-24"
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Placeholder: Lot J replaces the body and keeps the props contract (`EditorTabProps`). */
|
||||
export default function VerificationTab(_props: EditorTabProps) {
|
||||
const t = useT()
|
||||
return (
|
||||
<section
|
||||
data-testid="editor-tab-verification"
|
||||
aria-label={t('editor.tab.verification')}
|
||||
className="min-h-24"
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Placeholder: Lot F replaces the body and keeps the props contract (`EditorTabProps`). */
|
||||
export default function VertebraeTab(_props: EditorTabProps) {
|
||||
const t = useT()
|
||||
return (
|
||||
<section
|
||||
data-testid="editor-tab-vertebrae"
|
||||
aria-label={t('editor.tab.vertebrae')}
|
||||
className="min-h-24"
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** Placeholder: Lot G replaces the body and keeps the props contract (`EditorTabProps`). */
|
||||
export default function ViewsTab(_props: EditorTabProps) {
|
||||
const t = useT()
|
||||
return (
|
||||
<section
|
||||
data-testid="editor-tab-views"
|
||||
aria-label={t('editor.tab.views')}
|
||||
className="min-h-24"
|
||||
/>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user