Files
faunosteo/apps/app/src/components/admin/RevokeGrantDialog.tsx
T
bchanot 0176b3184d feat(app): administration screens and anatomical catalogue (V1-2.5 G, H, I)
Administration page (board 10), species, rights, official sheets list with filters, creation from the core's empty document, overview with the working-copy action; catalogue list (search by sheet name), sheet and version pages through PreviewSheet with zoom and pan. App 650, typecheck, lint, build green.
2026-11-02 17:18:27 +01:00

126 lines
3.7 KiB
TypeScript

import { useEffect, useState, type FormEvent } from 'react'
import {
classifySheetError,
revokeCatalogRight,
type CatalogGrant,
type SheetFailureKind,
} from '@faunosteo/supabase'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
import Notice from '@/components/ui/Notice'
import Textarea from '@/components/ui/Textarea'
import { failureMessage } from '@/admin/labels'
import { GRANT_TEXT_MAX, parseRevocation } from '@/admin/validation'
import { translateFieldError } from '@/account/validation'
import { getSupabaseClient } from '@/lib/supabase'
import { useT } from '@/i18n/useT'
export interface RevokeGrantDialogProps {
grant: CatalogGrant | null
/** The holder and the right, as the list names them. */
subject: string
onClose: () => void
/** Called once the server revoked it; the caller closes the dialog. */
onRevoked: () => void
}
/** Revocation with its required reason (§5.2): it takes effect at the holder's next request. */
export default function RevokeGrantDialog({
grant,
subject,
onClose,
onRevoked,
}: RevokeGrantDialogProps) {
const t = useT()
const [reason, setReason] = useState('')
const [error, setError] = useState<string | undefined>()
const [failure, setFailure] = useState<SheetFailureKind | null>(null)
const [pending, setPending] = useState(false)
useEffect(() => {
setReason('')
setError(undefined)
setFailure(null)
}, [grant])
const close = () => {
if (!pending) onClose()
}
async function submit(event: FormEvent) {
event.preventDefault()
const client = getSupabaseClient()
if (!grant || pending) return
const parsed = parseRevocation(reason)
if (!parsed.ok) return setError(parsed.errors.reason)
if (!client) return setFailure('unavailable')
setError(undefined)
setFailure(null)
setPending(true)
try {
await revokeCatalogRight(client, grant.id, parsed.value)
onRevoked()
} catch (caught) {
setFailure(classifySheetError(caught))
} finally {
setPending(false)
}
}
const message = translateFieldError(t, error)
return (
<Dialog
open={grant !== null}
onClose={close}
title={t('admin.access.revoke.title')}
description={
<p className="text-sm text-[var(--color-ink-muted)]">
{t('admin.access.revoke.body').replace('{subject}', subject)}
</p>
}
>
<form
onSubmit={(e) => void submit(e)}
noValidate
className="mt-3 space-y-4"
>
<FormField
id="revoke-reason"
label={t('admin.access.revoke.reason')}
error={message}
>
<Textarea
id="revoke-reason"
value={reason}
onChange={(e) => setReason(e.target.value)}
maxLength={GRANT_TEXT_MAX}
aria-invalid={Boolean(error)}
aria-describedby={fieldDescribedBy(
'revoke-reason',
undefined,
error,
)}
/>
</FormField>
{failure && <Notice tone="error">{failureMessage(t, failure)}</Notice>}
<div className="flex flex-wrap justify-end gap-2">
<Button
type="button"
variant="ghost"
onClick={close}
aria-disabled={pending}
>
{t('common.cancel')}
</Button>
<Button type="submit" variant="danger" aria-disabled={pending}>
{pending
? t('admin.access.revoke.pending')
: t('admin.access.revoke.confirm')}
</Button>
</div>
</form>
</Dialog>
)
}