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 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 ( ) } /** * 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(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 (

{title}

{t('admin.version_status.draft')} {sheet.working_copy && {t('admin.badge.working_copy')}} {mode !== 'edit' && ( {t('editor.draft.read_only')} )}

{statusText(t, { copyIssue, saving, unsaved, revision })}

{mode === 'edit' && (
)}
{failure && {failure}} void discard()} onCancel={() => setAsking(false)} />
) }