Files
faunosteo/apps/app/src/components/relations/RelationActionDialog.tsx
T
bchanot 930499cd25 feat(app): animal screens, relation panels, animals on the client page
Routes /app/animals, new, :animalId, edit under one workspace gate; list with
species filter; form (D-R7, U3 birth, no default sex or role, optional first
client); page with current and former relations, end, dates, void; client page
links its animals, adds one pre-associated, links an existing one. Card now
passes aria-labelledby, which it silently dropped.
2026-10-26 17:11:28 +01:00

124 lines
3.5 KiB
TypeScript

import { useState, type ReactNode } from 'react'
import {
classifyRecordError,
type RecordFailureKind,
} from '@faunosteo/supabase'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import Notice from '@/components/ui/Notice'
import { useT } from '@/i18n/useT'
export interface RelationActionHandlers {
onClose: () => void
/** After a success: the page reloads the relations, announces, then closes the dialog. */
onDone: () => Promise<void>
/**
* The relation changed or left elsewhere (`conflict`, `not_found`): the page
* reloads and says so, then closes the dialog; the stale row is never retried.
*/
onStale: (failure: RecordFailureKind) => Promise<void>
}
export interface RelationActionDialogProps extends RelationActionHandlers {
title: string
/** Which relation: « Marie Dupont · Propriétaire ». */
relationLabel: string
body: string
confirmLabel: string
pendingLabel: string
danger?: boolean
children?: ReactNode
/** Checks the fields; false keeps the dialog open with their errors. */
validate?: () => boolean
/** The relation RPC; throws a RecordFailure. */
action: () => Promise<unknown>
}
/**
* Confirmation of one relation write (end, dates, void) on the shared
* ModalDialog: closing is never confirming; while the write and the reload
* that shows it run, the dialog refuses to close, so focus returns once,
* after the change is on the page (to the opener, or to its container when
* the opener left with the row). Other failures stay in the dialog with the
* values typed.
*/
export default function RelationActionDialog({
title,
relationLabel,
body,
confirmLabel,
pendingLabel,
danger = false,
children,
validate,
action,
onClose,
onDone,
onStale,
}: RelationActionDialogProps) {
const t = useT()
const [pending, setPending] = useState(false)
const [failure, setFailure] = useState<RecordFailureKind | null>(null)
const close = () => {
if (!pending) onClose()
}
async function confirm() {
if (pending || (validate && !validate())) return
setPending(true)
setFailure(null)
try {
await action()
await onDone()
} catch (error) {
const kind = classifyRecordError(error)
if (kind === 'conflict' || kind === 'not_found') await onStale(kind)
else setFailure(kind)
} finally {
setPending(false)
}
}
return (
<Dialog
open
onClose={close}
title={title}
description={
<div className="space-y-2 text-sm">
<p className="font-medium text-[var(--color-ink)]">{relationLabel}</p>
<p className="text-[var(--color-ink-muted)]">{body}</p>
</div>
}
size="sm"
>
<div className="mt-3 space-y-3">
{children}
{failure && (
<Notice tone="error">{t(`relations.failure.${failure}`)}</Notice>
)}
</div>
<div className="mt-4 flex flex-wrap justify-end gap-2">
{/* Not disabled while pending: a button that turns disabled drops the focus out of the dialog. */}
<Button
type="button"
variant="ghost"
onClick={close}
aria-disabled={pending}
>
{t('common.cancel')}
</Button>
<Button
type="button"
variant={danger ? 'danger' : 'primary'}
onClick={() => void confirm()}
aria-disabled={pending}
>
{pending ? pendingLabel : confirmLabel}
</Button>
</div>
</Dialog>
)
}