feat(app): editor Vérification tab and review submission

This commit is contained in:
bchanot committed 2026-11-04 13:59:16 +01:00
1 parent 877bad0021
commit 447d0a4272
12 files changed
+987 -4

No files matched your search

@@ -1,4 +1,5 @@
import { useState, type KeyboardEvent } from 'react'
import { useEditorStore } from '@/editor/state/editorStore'
import { tabsFor, type EditorTabId } from '@/editor/tabs'
import type { TabTier } from '@/editor/useViewport'
import { useT } from '@/i18n/useT'
@@ -13,6 +14,24 @@ const ACTIVE =
const IDLE =
'border-transparent text-[var(--color-ink-muted)] hover:text-[var(--color-ink)]'
/** The blocking-error count on the Vérification tab; absent at zero. */
function ErrorCount({ tab }: { tab: EditorTabId }) {
const count = useEditorStore(
(s) =>
s.previewState?.report.issues.filter((i) => i.severity === 'error')
.length ?? 0,
)
if (tab !== 'verification' || count === 0) return null
return (
<span
aria-hidden="true"
className="ml-2 rounded-md bg-red-50 px-1.5 text-xs text-red-700"
>
{count}
</span>
)
}
interface EditorTabsProps {
active: EditorTabId
tier: TabTier
@@ -70,6 +89,7 @@ export default function EditorTabs({
className={`-mb-px ${BASE} ${tab === active ? ACTIVE : IDLE}`}
>
{t(`editor.tab.${tab}`)}
<ErrorCount tab={tab} />
</button>
))}
</div>
@@ -100,6 +120,7 @@ export default function EditorTabs({
className={`rounded-md px-3 ${BASE} border-b-0 ${tab === active ? ACTIVE : IDLE}`}
>
{t(`editor.tab.${tab}`)}
<ErrorCount tab={tab} />
</button>
</li>
))}
@@ -1,14 +1,89 @@
import type {
AnatomicalSheetDocument,
SheetReport,
} from '@faunosteo/core-wasm/types'
import IssueList from '@/components/editor/verification/IssueList'
import {
Completeness,
GroupStatuses,
SizeAndCaps,
} from '@/components/editor/verification/ReviewFigures'
import SubmitPanel from '@/components/editor/verification/SubmitPanel'
import VerificationSummary from '@/components/editor/verification/VerificationSummary'
import Notice from '@/components/ui/Notice'
import { useEditorStore } from '@/editor/state/editorStore'
import { useSheetDocument } from '@/editor/sheetDocument'
import type { EditorTabProps } from '@/editor/tabs'
import { splitIssues } from '@/editor/verification'
import { useT } from '@/i18n/useT'
/** Placeholder: Lot J replaces the body and keeps the props contract (`EditorTabProps`). */
export default function VerificationTab(_props: EditorTabProps) {
interface PanelProps extends EditorTabProps {
report: SheetReport
doc: AnatomicalSheetDocument
}
function Panel({
report,
doc,
readOnly,
onSelectTab,
reloadSheet,
}: PanelProps) {
const t = useT()
const { errors, warnings } = splitIssues(report)
const { to_confirm } = report.completeness.statuses
return (
<>
<VerificationSummary
errors={errors.length}
warnings={warnings.length}
toConfirm={to_confirm}
/>
{errors.length + warnings.length === 0 && (
<Notice tone="success">{t('editor.verification.none')}</Notice>
)}
<IssueList
issues={errors}
tone="error"
doc={doc}
onSelectTab={onSelectTab}
/>
{warnings.length > 0 && (
<h3 className="text-sm font-medium">
{t('editor.verification.warnings_title')}
</h3>
)}
<IssueList
issues={warnings}
tone="info"
doc={doc}
onSelectTab={onSelectTab}
/>
{to_confirm > 0 && (
<Notice tone="info">{t('editor.verification.to_confirm_info')}</Notice>
)}
<Completeness report={report} />
<GroupStatuses report={report} doc={doc} />
<SizeAndCaps report={report} />
{!readOnly && (
<SubmitPanel errorCount={errors.length} reloadSheet={reloadSheet} />
)}
</>
)
}
/** Vérification: the in-page check of the draft (not the server's verdict) and its submission. */
export default function VerificationTab(props: EditorTabProps) {
const t = useT()
const report = useEditorStore((s) => s.previewState?.report)
const doc = useSheetDocument()
return (
<section
data-testid="editor-tab-verification"
aria-label={t('editor.tab.verification')}
className="min-h-24"
/>
className="min-h-24 space-y-4"
>
{report && doc && <Panel {...props} report={report} doc={doc} />}
</section>
)
}
@@ -0,0 +1,61 @@
import type {
AnatomicalSheetDocument,
SheetIssue,
} from '@faunosteo/core-wasm/types'
import Button from '@/components/ui/Button'
import Notice from '@/components/ui/Notice'
import { issueMessageKey, issueTarget } from '@/editor/verification'
import type { EditorTabId } from '@/editor/tabs'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
interface IssueListProps {
issues: readonly SheetIssue[]
tone: 'error' | 'info'
doc: AnatomicalSheetDocument
onSelectTab: (tab: EditorTabId) => void
}
/** One notice per issue: the page's own message, the element's name, « Aller à … ». */
export default function IssueList({
issues,
tone,
doc,
onSelectTab,
}: IssueListProps) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
if (issues.length === 0) return null
return (
<ul className="space-y-2">
{issues.map((issue, index) => {
const target = issueTarget(doc, issue.path, locale)
return (
<li key={`${issue.code}:${issue.path}:${index}`}>
<Notice tone={tone}>
<p>
{target?.name && <strong>{target.name}</strong>}
{target?.name ? ' : ' : ''}
{t(issueMessageKey(issue.code))}
</p>
{target && (
<Button
type="button"
size="sm"
variant="secondary"
className="mt-2"
onClick={() => onSelectTab(target.tab)}
>
{t('editor.verification.goto').replace(
'{tab}',
t(`editor.tab.${target.tab}`),
)}
</Button>
)}
</Notice>
</li>
)
})}
</ul>
)
}
@@ -0,0 +1,123 @@
import type {
AnatomicalSheetDocument,
ReviewGroup,
SheetReport,
StatusCounts,
} from '@faunosteo/core-wasm/types'
import { elementLabel } from '@/editor/sheetDocument'
import { DOCUMENT_BYTES_CAP, groupThousands } from '@/editor/verification'
import { useT } from '@/i18n/useT'
import type { Locale } from '@/i18n/types'
import { useLocaleStore } from '@/state/localeStore'
type Translate = ReturnType<typeof useT>
function statusLine(t: Translate, counts: StatusCounts): string {
return (['to_confirm', 'reviewed', 'inherited'] as const)
.map(
(status) =>
`${t(`editor.verification.status.${status}`)} ${counts[status]}`,
)
.join(' · ')
}
/** The group key the core builds (`general`, `view:<id>`, `zone:<id>`) as a readable name. */
function groupName(
t: Translate,
doc: AnatomicalSheetDocument,
key: string,
locale: Locale,
): string {
if (key === 'general') return t('editor.tab.general')
const [kind, id] = key.split(/:(.*)/s)
if (kind === 'view') return elementLabel(doc, { kind: 'view', id }, locale)
if (kind === 'zone') return elementLabel(doc, { kind: 'zone', id }, locale)
return key
}
function GroupRow({ group, label }: { group: ReviewGroup; label: string }) {
const t = useT()
return (
<li className="flex flex-wrap justify-between gap-x-4">
<span>
{label}
{group.removed && ` (${t('editor.verification.group_removed')})`}
</span>
<span className="text-[var(--color-ink-muted)]">
{statusLine(t, group.statuses)}
</span>
</li>
)
}
export function Completeness({ report }: { report: SheetReport }) {
const t = useT()
const { statuses, vet_referrals, uncited } = report.completeness
const count = (key: string, n: number) => t(key).replace('{count}', String(n))
return (
<section
aria-label={t('editor.verification.completeness')}
className="space-y-1 text-sm"
>
<h3 className="font-medium">{t('editor.verification.completeness')}</h3>
<p>{statusLine(t, statuses)}</p>
<p>{count('editor.verification.vet_referrals', vet_referrals)}</p>
<p>{count('editor.verification.uncited', uncited)}</p>
</section>
)
}
export function GroupStatuses({
report,
doc,
}: {
report: SheetReport
doc: AnatomicalSheetDocument
}) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
return (
<section
aria-label={t('editor.verification.groups')}
className="space-y-1 text-sm"
>
<h3 className="font-medium">{t('editor.verification.groups')}</h3>
<ul className="space-y-1">
{report.groups.map((group) => (
<GroupRow
key={group.key}
group={group}
label={groupName(t, doc, group.key, locale)}
/>
))}
</ul>
</section>
)
}
export function SizeAndCaps({ report }: { report: SheetReport }) {
const t = useT()
const { exceeded } = report.caps
const size = t('editor.verification.size')
.replace('{bytes}', groupThousands(report.document_bytes))
.replace('{cap}', groupThousands(DOCUMENT_BYTES_CAP))
return (
<section
aria-label={t('editor.verification.size_title')}
className="space-y-1 text-sm"
>
<h3 className="font-medium">{t('editor.verification.size_title')}</h3>
<p>{size}</p>
{exceeded.length > 0 && (
<p role="alert" className="text-[var(--color-danger)]">
{t('editor.verification.caps_exceeded').replace(
'{caps}',
exceeded
.map((cap) => t(`editor.verification.cap.${cap}`))
.join(', '),
)}
</p>
)}
</section>
)
}
@@ -0,0 +1,80 @@
import Button from '@/components/ui/Button'
import FormField from '@/components/ui/FormField'
import Textarea from '@/components/ui/Textarea'
import { useEditorStore } from '@/editor/state/editorStore'
import { NOTE_MAX_LENGTH } from '@/editor/verification'
import { useT } from '@/i18n/useT'
import { useSubmission } from './useSubmission'
interface SubmitPanelProps {
errorCount: number
reloadSheet: () => void
}
/** The hint that explains why the button is off, strongest reason first. */
function useBlockingHint(
errorCount: number,
noteBlank: boolean,
): string | null {
const t = useT()
const unsaved = useEditorStore((s) => s.unsaved || s.staleBase)
if (errorCount > 0) {
return t('editor.verification.hint.errors').replace(
'{count}',
String(errorCount),
)
}
if (unsaved) return t('editor.verification.hint.unsaved')
return noteBlank ? t('editor.verification.hint.note') : null
}
/** « Note de soumission (obligatoire) » and « Soumettre à la revue » (board 06 A). */
export default function SubmitPanel({
errorCount,
reloadSheet,
}: SubmitPanelProps) {
const t = useT()
const submitting = useEditorStore((s) => s.submitting)
const { note, setNote, submit, message } = useSubmission(reloadSheet)
const noteBlank = note.trim() === ''
const hint = useBlockingHint(errorCount, noteBlank)
return (
<section
aria-label={t('editor.verification.submit_title')}
className="space-y-3"
>
<FormField
id="verification-note"
label={t('editor.verification.note')}
hint={t('editor.verification.note_hint').replace(
'{max}',
String(NOTE_MAX_LENGTH),
)}
error={message ?? undefined}
>
<Textarea
id="verification-note"
value={note}
maxLength={NOTE_MAX_LENGTH}
onChange={(event) => setNote(event.target.value)}
/>
</FormField>
<Button
type="button"
disabled={hint !== null || submitting}
aria-describedby={hint ? 'verification-submit-hint' : undefined}
onClick={() => void submit()}
>
{t('editor.verification.submit')}
</Button>
{hint && (
<p
id="verification-submit-hint"
className="text-sm text-[var(--color-ink-muted)]"
>
{hint}
</p>
)}
</section>
)
}
@@ -0,0 +1,36 @@
import Badge from '@/components/ui/Badge'
import { useT } from '@/i18n/useT'
interface VerificationSummaryProps {
errors: number
warnings: number
toConfirm: number
}
/** Board 06 A: the three counts under the panel title (L27, L28). */
export default function VerificationSummary({
errors,
warnings,
toConfirm,
}: VerificationSummaryProps) {
const t = useT()
const count = (key: string, n: number) => t(key).replace('{count}', String(n))
return (
<div className="space-y-2">
<h2 className="text-base font-medium">
{t('editor.verification.title')}
</h2>
<p className="flex flex-wrap gap-2">
<Badge tone={errors > 0 ? 'danger' : 'neutral'}>
{count('editor.verification.errors', errors)}
</Badge>
<Badge tone={warnings > 0 ? 'warn' : 'neutral'}>
{count('editor.verification.warnings', warnings)}
</Badge>
<Badge tone="accent">
{count('editor.verification.to_confirm', toConfirm)}
</Badge>
</p>
</div>
)
}
@@ -0,0 +1,49 @@
import { useEffect, useRef, useState } from 'react'
import { failureMessage } from '@/admin/labels'
import { useEditorStore } from '@/editor/state/editorStore'
import type { SubmitOutcome } from '@/editor/state/editorTypes'
import { useT } from '@/i18n/useT'
type Translate = ReturnType<typeof useT>
/** What a refused submission tells; the conflict dialog and silent kinds tell nothing. */
function refusalMessage(t: Translate, outcome: SubmitOutcome): string | null {
if (outcome.ok) return null
if (outcome.reason === 'server') {
return outcome.kind === 'draft_conflict'
? null
: failureMessage(t, outcome.kind)
}
if (outcome.reason !== 'blocked') return null
if (outcome.kind === 'unsaved' || outcome.kind === 'stale_base') {
return t('editor.verification.hint.unsaved')
}
return null
}
/** The note and the submission: success reloads the sheet (the round freezes the draft). */
export function useSubmission(reloadSheet: () => void) {
const t = useT()
const [note, setNote] = useState('')
const [message, setMessage] = useState<string | null>(null)
const mounted = useRef(true)
useEffect(() => {
mounted.current = true
return () => {
mounted.current = false
}
}, [])
const submit = async () => {
setMessage(null)
const outcome = await useEditorStore.getState().submit(note.trim())
if (!mounted.current) return
if (outcome.ok) {
setNote('')
reloadSheet()
} else {
setMessage(refusalMessage(t, outcome))
}
}
return { note, setNote, submit, message }
}