168 lines
5.3 KiB
TypeScript
168 lines
5.3 KiB
TypeScript
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>
|
||
)
|
||
}
|