Files
faunosteo/apps/app/src/components/admin/GrantList.tsx
T
bchanot 51225cacf6 fix(app): grant expiry ends the chosen day in Paris; species order 0..9999 (V1-2.5 L2)
Owner checkpoint 2026-11-02: expiry 23:59:59.999 Europe/Paris (picker, validation, display), tests under non-Paris zones and DST days; M7 redefines create_species and update_inactive_species with the range check; pgTAP 015 and 022; « Relecteur ou relectrice »; object_conflict EN; setup key selectable, no Copy.
2026-11-02 21:29:33 +01:00

205 lines
6.4 KiB
TypeScript

import { useState } from 'react'
import type { CatalogGrant } from '@faunosteo/supabase'
import RevokeGrantDialog from '@/components/admin/RevokeGrantDialog'
import Badge from '@/components/ui/Badge'
import Button from '@/components/ui/Button'
import FormField from '@/components/ui/FormField'
import Notice from '@/components/ui/Notice'
import Select from '@/components/ui/Select'
import { formatDay } from '@/admin/labels'
import { PARIS_TIME_ZONE } from '@/lib/dates'
import type { Locale } from '@/i18n/types'
import { useLocaleStore } from '@/state/localeStore'
import { useT } from '@/i18n/useT'
type Translate = ReturnType<typeof useT>
type GrantState = CatalogGrant['state']
/** The four states of the rights screen (§5.2), in reading order. */
const GRANT_STATES: readonly GrantState[] = [
'active',
'expiring',
'expired',
'revoked',
]
const STATE_TONE = {
active: 'accent',
expiring: 'warn',
expired: 'neutral',
revoked: 'danger',
} as const
function holder(t: Translate, grant: CatalogGrant): string {
const name = grant.display_name || t('admin.person.unnamed')
return grant.email ? `${name} (${grant.email})` : name
}
function GrantItem({
grant,
t,
locale,
onRevoke,
}: {
grant: CatalogGrant
t: Translate
locale: Locale
onRevoke: () => void
}) {
const granted = t('admin.access.list.granted')
.replace('{date}', formatDay(grant.granted_at, locale))
.replace('{name}', grant.granted_by_name || t('admin.person.unnamed'))
const expiry = grant.expires_at
? t('admin.access.list.expires').replace(
'{date}',
formatDay(grant.expires_at, locale, PARIS_TIME_ZONE),
)
: t('admin.access.list.no_expiry')
const live = grant.state === 'active' || grant.state === 'expiring'
return (
<li className="space-y-2 px-4 py-3">
<div className="flex flex-wrap items-center gap-2">
<span className="min-w-0 font-medium text-[var(--color-ink)]">
{holder(t, grant)}
</span>
<Badge>{t(`admin.grant_kind.${grant.kind}`)}</Badge>
<Badge tone={STATE_TONE[grant.state]}>
{t(`admin.grant_state.${grant.state}`)}
</Badge>
{live && !grant.effective && (
<Badge tone="warn">{t('admin.access.list.suspended')}</Badge>
)}
</div>
<dl className="grid gap-1 text-sm sm:grid-cols-[auto_1fr] sm:gap-x-3">
<dt className="text-[var(--color-ink-muted)]">
{t('admin.access.form.reason')}
</dt>
<dd className="text-[var(--color-ink)] break-words">{grant.reason}</dd>
{grant.verification_note && (
<>
<dt className="text-[var(--color-ink-muted)]">
{t('admin.access.form.note')}
</dt>
<dd className="text-[var(--color-ink)] break-words">
{grant.verification_note}
</dd>
</>
)}
{grant.revoked_at && (
<>
<dt className="text-[var(--color-ink-muted)]">
{t('admin.access.list.revoked_on').replace(
'{date}',
formatDay(grant.revoked_at, locale),
)}
</dt>
<dd className="text-[var(--color-ink)] break-words">
{grant.revocation_reason}
</dd>
</>
)}
</dl>
<p className="text-sm text-[var(--color-ink-muted)]">{`${granted} · ${expiry}`}</p>
{live && (
<Button type="button" variant="secondary" size="sm" onClick={onRevoke}>
{t('admin.access.revoke.action')}
</Button>
)}
</li>
)
}
/**
* Every grant (§5.2): active, expiring within 30 days, expired and revoked,
* with reason and verification note; a state filter, and revocation of the
* grants still in force. A grant shown « suspendu » is active on paper while
* its holder no longer meets the eligibility rules (`effective` false).
*/
export default function GrantList({
grants,
onChanged,
}: {
grants: CatalogGrant[]
onChanged: () => void
}) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const [state, setState] = useState<GrantState | ''>('')
const [revoking, setRevoking] = useState<CatalogGrant | null>(null)
const [done, setDone] = useState(false)
const shown = state ? grants.filter((grant) => grant.state === state) : grants
const subject = revoking
? `${holder(t, revoking)}, ${t(`admin.grant_kind.${revoking.kind}`)}`
: ''
const revoked = () => {
setRevoking(null)
setDone(true)
onChanged()
}
return (
<section aria-labelledby="grants-title" className="space-y-3">
<h2
id="grants-title"
className="font-serif text-xl text-[var(--color-ink)]"
>
{t('admin.access.list.title')}
</h2>
<div className="max-w-xs">
<FormField id="grant-state" label={t('admin.access.list.filter')}>
<Select
id="grant-state"
value={state}
onChange={(e) => setState(e.target.value as GrantState | '')}
>
<option value="">{t('admin.access.list.all')}</option>
{GRANT_STATES.map((value) => (
<option key={value} value={value}>
{t(`admin.grant_state.${value}`)}
</option>
))}
</Select>
</FormField>
</div>
{done && <Notice tone="success">{t('admin.access.revoke.done')}</Notice>}
{shown.length === 0 ? (
<p
role="status"
className="rounded-2xl border border-dashed border-[var(--color-border)] p-6 text-center text-[var(--color-ink)]"
>
{t(
grants.length === 0
? 'admin.access.list.empty'
: 'admin.access.list.no_match',
)}
</p>
) : (
<ul
aria-label={t('admin.access.list.title')}
className="divide-y divide-[var(--color-border)] rounded-2xl border border-[var(--color-border)] bg-[var(--color-surface)]"
>
{shown.map((grant) => (
<GrantItem
key={grant.id}
grant={grant}
t={t}
locale={locale}
onRevoke={() => {
setDone(false)
setRevoking(grant)
}}
/>
))}
</ul>
)}
<RevokeGrantDialog
grant={revoking}
subject={subject}
onClose={() => setRevoking(null)}
onRevoked={revoked}
/>
</section>
)
}