import { useState } from 'react' import { classifyRecordError, setAnimalArchived, type RecordFailureKind, } from '@faunosteo/supabase' import Button from '@/components/ui/Button' import Dialog from '@/components/ui/Dialog' import Notice from '@/components/ui/Notice' import { getSupabaseClient } from '@/lib/supabase' import { useT } from '@/i18n/useT' export interface ArchiveAnimalDialogProps { open: boolean animalId: string onClose: () => void /** Called once the server archived the animal; the page then closes the dialog. */ onArchived: () => void } /** * Confirmation before archiving an animal (contract ยง7.1): its relations are * kept and frozen, no client is archived. Closing it is never a * confirmation, and while the request runs it refuses to close. */ export default function ArchiveAnimalDialog({ open, animalId, onClose, onArchived, }: ArchiveAnimalDialogProps) { const t = useT() const [pending, setPending] = useState(false) const [failure, setFailure] = useState(null) const close = () => { if (pending) return setFailure(null) onClose() } async function confirm() { const client = getSupabaseClient() if (pending) return if (!client) return setFailure('unavailable') setPending(true) setFailure(null) try { await setAnimalArchived(client, animalId, true) onArchived() } catch (error) { setFailure(classifyRecordError(error)) } finally { setPending(false) } } return ( {t('animals.archive.body')}

} size="sm" > {failure && ( {t(`animals.failure.${failure}`)} )}
{/* Not disabled while pending: a button that turns disabled drops the focus out of the dialog. */}
) }