fix(app): unreadable permissions show a retry, never the form (V1-2.5 L1)

Owner checkpoint 2026-11-02: creation page checks loading, server refusal, failed read, then the editor rule; Administration home shows the same notice. reloadAccess re-reads roles and grants without wiping held state; refreshAccount (no caller) removed. Zone panel live region always mounted. Unit and e2e for reviewer-only and editor.
This commit is contained in:
bchanot committed 2026-11-02 21:21:59 +01:00
1 parent 14ff890761
commit 4111efa1d1
15 files changed
+856 -56

No files matched your search

@@ -1,3 +1,4 @@
import { useRef } from 'react'
import { Link } from 'react-router-dom'
import {
listOfficialSheetsAdmin,
@@ -5,6 +6,7 @@ import {
} from '@faunosteo/supabase'
import SheetBadges from '@/components/admin/SheetBadges'
import WorkingCopyAction from '@/components/admin/WorkingCopyAction'
import PermissionsUnavailable from '@/components/shell/PermissionsUnavailable'
import Button from '@/components/ui/Button'
import ButtonLink from '@/components/ui/ButtonLink'
import Card from '@/components/ui/Card'
@@ -13,6 +15,7 @@ import { failureMessage, localized } from '@/admin/labels'
import { useAdminPermissions, type AdminPermissions } from '@/admin/permissions'
import { isRightsRefusal, useAdminQuery } from '@/admin/useAdminQuery'
import { usePageHeading } from '@/app/usePageHeading'
import { useAuthStore } from '@/state/authStore'
import { useLocaleStore } from '@/state/localeStore'
import { useT } from '@/i18n/useT'
@@ -164,6 +167,14 @@ export default function AdminHomeRoute() {
const t = useT()
const heading = usePageHeading()
const permissions = useAdminPermissions()
const grantsStatus = useAuthStore((s) => s.catalogGrantsStatus)
const reloadAccess = useAuthStore((s) => s.reloadAccess)
const access = useRef<HTMLDivElement>(null)
// Focus stays on the wrapper when the Retry button unmounts with the notice.
const retry = async () => {
await reloadAccess()
access.current?.focus()
}
return (
<div className="space-y-5">
<header className="space-y-1">
@@ -174,9 +185,21 @@ export default function AdminHomeRoute() {
>
{t('shell.nav.admin')}
</h1>
<p className="text-sm text-[var(--color-ink-muted)]">
{permissionsLine(t, permissions)}
</p>
<div ref={access} tabIndex={-1} className="outline-none">
{grantsStatus === 'loading' && (
<p role="status" className="text-sm text-[var(--color-ink-muted)]">
{t('shell.loading_session')}
</p>
)}
{grantsStatus === 'failed' && (
<PermissionsUnavailable asPage={false} onRetry={() => void retry()} />
)}
{grantsStatus === 'ready' && (
<p className="text-sm text-[var(--color-ink-muted)]">
{permissionsLine(t, permissions)}
</p>
)}
</div>
</header>
<div className="grid gap-4 md:grid-cols-2">
<SheetsCard editor={permissions.editor} />
@@ -11,6 +11,7 @@ import SpeciesDialog, {
type SpeciesDialogMode,
} from '@/components/admin/SpeciesDialog'
import AccessDenied from '@/components/shell/AccessDenied'
import PermissionsUnavailable from '@/components/shell/PermissionsUnavailable'
import Button, { buttonClassName } from '@/components/ui/Button'
import Card from '@/components/ui/Card'
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
@@ -97,6 +98,7 @@ export default function AdminSheetCreateRoute() {
const locale = useLocaleStore((s) => s.locale)
const { editor } = useAdminPermissions()
const grantsStatus = useAuthStore((s) => s.catalogGrantsStatus)
const reloadAccess = useAuthStore((s) => s.reloadAccess)
const species = useAdminQuery('species', listCatalogSpecies)
const [values, setValues] = useState<SheetFormValues>(EMPTY)
const [slugEdited, setSlugEdited] = useState(false)
@@ -112,11 +114,13 @@ export default function AdminSheetCreateRoute() {
if (first) document.getElementById(fieldId(first))?.focus()
}, [errorRound, errors])
// A display rule like the « Nouvelle fiche » button: once the caller's own
// grants are read, a reviewer or a publisher reaching the address is not
// offered the form. While they load nothing is decided; if the read failed
// the form shows and the server, which refuses creation to anyone without
// the editor capability, decides.
// Three states (owner 2026-11-02), checked in this order: own grants still
// loading (wait); the server refused (its `rights.*` answer wins, BDR-094);
// own grants unreadable (retry, never the form and never a refusal deduced
// from a technical error); then, grants read, the display rule: no editor
// capability, no form. The server decides creation either way. Every hook
// stays above these returns so the form values survive a state switch: do
// not move the form state into a child.
if (grantsStatus === 'loading')
return (
<p role="status" className="text-sm text-[var(--color-ink-muted)]">
@@ -124,11 +128,18 @@ export default function AdminSheetCreateRoute() {
</p>
)
if (
(grantsStatus === 'ready' && !editor) ||
(species.state.status === 'error' &&
isRightsRefusal(species.state.failure))
species.state.status === 'error' &&
isRightsRefusal(species.state.failure)
)
return <AccessDenied />
if (grantsStatus === 'failed') {
const retry = () => {
void reloadAccess()
if (species.state.status === 'error') species.refresh()
}
return <PermissionsUnavailable asPage onRetry={retry} />
}
if (!editor) return <AccessDenied />
const set = (key: keyof SheetFormValues) => (value: string) =>
setValues((v) => {
+31 -29
View File
@@ -96,36 +96,38 @@ export default function ZonePanel({ state, viewId, selectedId, onSelect }: ZoneP
))}
</select>
</label>
{annotation ? (
<div aria-live="polite" className="space-y-2">
<h2 id="catalogue-zone-title" className="font-serif text-lg text-[var(--color-ink)]">
{t('catalogue.zone.title').replace('{name}', nameOf(annotation.id))}
</h2>
<p className="text-sm text-[var(--color-ink-muted)]">
{zoneLine(state, annotation, locale, t)}
<div aria-live="polite" className="space-y-2">
{annotation ? (
<>
<h2 id="catalogue-zone-title" className="font-serif text-lg text-[var(--color-ink)]">
{t('catalogue.zone.title').replace('{name}', nameOf(annotation.id))}
</h2>
<p className="text-sm text-[var(--color-ink-muted)]">
{zoneLine(state, annotation, locale, t)}
</p>
{findingZone && findingZone.categories.length > 0 ? (
findingZone.categories.map((category) => (
<section key={category.id} className="space-y-1 pt-1">
<h3 className="text-xs font-semibold uppercase tracking-wide text-[var(--color-ink-muted)]">
{pickLabel(category.name, locale)}
</h3>
<ul className="divide-y divide-[var(--color-border)]">
{category.children.map((node) => (
<FindingItem key={node.id} node={node} locale={locale} />
))}
</ul>
</section>
))
) : (
<p className="text-sm text-[var(--color-ink-muted)]">{t('catalogue.zone.no_findings')}</p>
)}
</>
) : (
<p id="catalogue-zone-title" className="text-sm text-[var(--color-ink-muted)]">
{t('catalogue.zone.prompt')}
</p>
{findingZone && findingZone.categories.length > 0 ? (
findingZone.categories.map((category) => (
<section key={category.id} className="space-y-1 pt-1">
<h3 className="text-xs font-semibold uppercase tracking-wide text-[var(--color-ink-muted)]">
{pickLabel(category.name, locale)}
</h3>
<ul className="divide-y divide-[var(--color-border)]">
{category.children.map((node) => (
<FindingItem key={node.id} node={node} locale={locale} />
))}
</ul>
</section>
))
) : (
<p className="text-sm text-[var(--color-ink-muted)]">{t('catalogue.zone.no_findings')}</p>
)}
</div>
) : (
<p id="catalogue-zone-title" className="text-sm text-[var(--color-ink-muted)]">
{t('catalogue.zone.prompt')}
</p>
)}
)}
</div>
</Card>
)
}
@@ -7,9 +7,10 @@ import { useT } from '@/i18n/useT'
* « Accès non autorisé » (contract §5.3, BDR-094): what a catalogue or
* administration page shows when the server refuses (`rights.*`, and
* `not_found` under /app/anatomy), the same whether the sheet exists or not;
* also the sheet creation page for an account whose own grants hold no editor
* capability (a display rule, the server still decides). Nothing from the
* address is echoed.
* also the sheet creation page once the own grants are read and hold no
* editor capability (a display rule, the server still decides). Never shown
* for a failed read (PermissionsUnavailable). Nothing from the address is
* echoed.
*/
export default function AccessDenied() {
const t = useT()
@@ -0,0 +1,50 @@
import Button from '@/components/ui/Button'
import Card from '@/components/ui/Card'
import { usePageHeading } from '@/app/usePageHeading'
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
interface Props {
/** The whole page (h1, focused on open) or a section inside one (h2). */
asPage: boolean
onRetry?: () => void
}
/**
* « Impossible de vérifier vos autorisations » (owner 2026-11-02): the own
* grants could not be read. A technical error never reads as an absence of
* right, so no form and no refusal; Retry reads them again. The server still
* decides every action.
*/
export default function PermissionsUnavailable({ asPage, onRetry }: Props) {
const t = useT()
const heading = usePageHeading()
const retry = onRetry ?? (() => void useAuthStore.getState().reloadAccess())
const body = (
<>
<p className="text-sm text-[var(--color-ink-muted)]">
{t('permissions_unavailable.body')}
</p>
<Button type="button" variant="secondary" onClick={retry}>
{t('common.retry')}
</Button>
</>
)
if (asPage)
return (
<Card as="section" className="space-y-3 p-5 sm:p-6">
<h1 ref={heading} tabIndex={-1} className="outline-none font-serif text-2xl text-[var(--color-ink)]">
{t('permissions_unavailable.title')}
</h1>
{body}
</Card>
)
return (
<Card as="section" aria-labelledby="permissions-unavailable-title" className="space-y-3 p-5 sm:p-6">
<h2 id="permissions-unavailable-title" className="font-serif text-lg text-[var(--color-ink)]">
{t('permissions_unavailable.title')}
</h2>
{body}
</Card>
)
}
+2
View File
@@ -863,6 +863,8 @@ export const en: Dict = {
'access_denied.title': 'Access denied',
'access_denied.body': "You don't have permission to access this page.",
'access_denied.back': 'Back to dashboard',
'permissions_unavailable.title': 'Unable to verify your permissions',
'permissions_unavailable.body': 'The check did not complete, probably because of a connection problem. This error does not change your permissions.',
'admin.sheets.title': 'Official sheets',
'admin.sheets.new': 'New sheet',
'admin.species.title': 'Species',
+2
View File
@@ -894,6 +894,8 @@ export const fr: Dict = {
'access_denied.title': 'Accès non autorisé',
'access_denied.body': 'Vous ne disposez pas des autorisations nécessaires pour accéder à cette page.',
'access_denied.back': 'Retour au tableau de bord',
'permissions_unavailable.title': 'Impossible de vérifier vos autorisations',
'permissions_unavailable.body': "La vérification n'a pas abouti, probablement à cause d'un problème de connexion. Cette erreur ne modifie pas vos droits.",
'admin.sheets.title': 'Fiches officielles',
'admin.sheets.new': 'Nouvelle fiche',
'admin.species.title': 'Espèces',
+22 -4
View File
@@ -68,11 +68,16 @@ export interface AuthState {
* Empty until loaded and when the read fails.
*/
catalogGrants: OwnCatalogGrant[]
/** Whether `catalogGrants` is read yet: a display rule waits for `ready`, and a `failed` read leaves the decision to the server. */
/** Whether `catalogGrants` is read yet: a display rule waits for `ready`; `failed` = the read failed, pages show the unavailable state and never deduce an absence of right from it. */
catalogGrantsStatus: 'loading' | 'ready' | 'failed'
/** Subscribes to the client; returns the unsubscribe. Safe to call twice. */
start: () => () => void
refreshAccount: () => Promise<void>
/**
* Reads the platform roles and own grants again after a failed permission
* read (owner 2026-11-02): pages wait, then show what the answer gives; a
* technical error never wipes what the account already holds. Never rejects.
*/
reloadAccess: () => Promise<void>
/** Reads the personal workspace again (after a failure). */
refreshWorkspace: () => Promise<void>
updateProfile: (patch: OwnProfilePatch) => Promise<Profile>
@@ -260,8 +265,21 @@ export const useAuthStore = create<AuthState>((set, get) => ({
return () => data.subscription.unsubscribe()
},
refreshAccount: async () => {
await loadAccount(requireClient(), requireUser(get().user), set, get)
reloadAccess: async () => {
const user = get().user
const client = getSupabaseClient()
if (!user || !client) return set({ catalogGrants: [], catalogGrantsStatus: 'failed' })
const setOwn = setForUser(user, set, get)
setOwn({ catalogGrantsStatus: 'loading' })
try {
const [roles, catalogGrants] = await Promise.all([
fetchOwnPlatformRoles(client, user.id),
listOwnCatalogGrants(client, user.id),
])
setOwn({ roles, catalogGrants, catalogGrantsStatus: 'ready' })
} catch {
setOwn({ catalogGrants: [], catalogGrantsStatus: 'failed' })
}
},
refreshWorkspace: async () => {