Files
faunosteo/apps/app/src/components/editor/verification/SubmitPanel.tsx
T

81 lines
2.3 KiB
TypeScript

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>
)
}