feat(app): catalogue cards say an integrity or load failure (board 30)

This commit is contained in:
bchanot committed 2026-11-17 00:14:14 +01:00
1 parent f061111e25
commit 6ad8d8347c
8 files changed
+564 -63

No files matched your search

@@ -0,0 +1,71 @@
import type { ReactNode, Ref } from 'react'
import { cardFailure, type CatalogueFailure } from '@/catalogue/failure'
import type { CardState } from '@/catalogue/useCardDetails'
import Button from '@/components/ui/Button'
import Notice from '@/components/ui/Notice'
import { coreFailureMessage } from '@/core/failure'
import { useCoreStopped } from '@/core/runtime'
import { useT } from '@/i18n/useT'
type Translate = ReturnType<typeof useT>
function failureText(t: Translate, failure: CatalogueFailure, integrity: boolean): string {
if (integrity) return t('catalogue.card.integrity_failed')
if (failure === 'core_unavailable') return coreFailureMessage(t, 'sheet.core_unavailable')
if (failure === 'denied') return t('access_denied.title')
return t(`catalogue.failure.${failure}`)
}
export interface CardFailureProps {
failed: Extract<CardState, { status: 'failed' }>
onRetry: () => void
retryRef: Ref<HTMLButtonElement>
/** The card's « Consulter », kept after « Réessayer » in every state. */
children: ReactNode
}
/**
* A catalogue card's failure (board 30, R-1 to R-4): the line (not a live
* region, so several cards failing at load stay quiet), « Réessayer » where a
* retry can work, and a polite region that says the line again only after a
* retry failed (empty at load and while an attempt runs).
*/
export default function CardFailure({ failed, onRetry, retryRef, children }: CardFailureProps) {
const t = useT()
const stopped = useCoreStopped()
const { failure, retrying, attempt } = failed
const { integrity, refused, retry } = cardFailure(failure, stopped)
const text = failureText(t, failure, integrity)
return (
<>
<Notice
tone={refused ? 'error' : 'warning'}
icon={failure === 'network' ? 'wifi-off' : 'alert'}
live={false}
>
{integrity ? <strong className="font-semibold">{text}</strong> : text}
</Notice>
{retry && (
<div aria-live="polite" className="sr-only">
{!retrying && attempt > 0 ? text : ''}
</div>
)}
<div className="mt-auto flex flex-wrap gap-2">
{retry && (
<Button
ref={retryRef}
type="button"
variant="secondary"
className="min-h-11"
aria-disabled={retrying ? 'true' : undefined}
onClick={onRetry}
>
{failure === 'core_unavailable' ? t('sheet.core_retry') : t('common.retry')}
</Button>
)}
{children}
</div>
</>
)
}
@@ -1,11 +1,19 @@
import { useCallback, useEffect, useRef } from 'react'
import type { CatalogEntry } from '@/catalogue/catalogue'
import { useCardDetails } from '@/catalogue/useCardDetails'
import {
useCardDetails,
type CardDetails,
type CardState,
} from '@/catalogue/useCardDetails'
import { versionText } from '@/catalogue/versionText'
import Badge from '@/components/ui/Badge'
import ButtonLink from '@/components/ui/ButtonLink'
import Card from '@/components/ui/Card'
import Icon from '@/components/ui/Icon'
import Notice from '@/components/ui/Notice'
import CardFailure from './CardFailure'
import { pickLabel } from '@/i18n/pickLocaleLabel'
import type { Locale } from '@/i18n/types'
import { useLocaleStore } from '@/state/localeStore'
import { useT } from '@/i18n/useT'
@@ -15,11 +23,112 @@ function counted(count: number, key: string, t: Translate): string {
return t(`${key}_${count === 1 ? 'one' : 'other'}`).replace('{count}', String(count))
}
/** The version line, with the counts once the core read them. */
function versionLine(
entry: CatalogEntry,
details: CardDetails | null,
locale: Locale,
t: Translate,
): string {
return [
versionText(entry.version_number, details?.publishedAt ?? null, locale, t),
...(details
? [
`${counted(details.viewCount, 'catalogue.card.views', t)}, ${counted(details.zoneCount, 'catalogue.card.zones', t)}`,
]
: []),
].join(' · ')
}
function ConsultLink({ sheetId, name }: { sheetId: string; name: string }) {
const t = useT()
return (
<ButtonLink
to={`/app/anatomy/${sheetId}`}
variant="secondary"
className="min-h-11 gap-2"
aria-label={t('catalogue.view_sheet').replace('{name}', name)}
>
<Icon name="eye" size={18} />
{t('catalogue.view')}
</ButtonLink>
)
}
/**
* One published sheet of the catalogue (board 01): species, « Publiée », name,
* version line with the counts the core read, a thumbnail of the main view,
* « Consulter ». Until its details load (or when they cannot), the card keeps
* its name, version and link.
* After a successful retry, the focus left on the removed « Réessayer » (or on
* nothing) moves to the card's « Consulter » (R-3); focus elsewhere is kept.
*/
function useFocusAfterRetry(state: CardState) {
const item = useRef<HTMLLIElement>(null)
const lastRetry = useRef<HTMLButtonElement | null>(null)
const wasRetrying = useRef(false)
useEffect(() => {
if (state.status === 'failed') wasRetrying.current = state.retrying
if (state.status !== 'ready' || !wasRetrying.current) return
wasRetrying.current = false
const active = document.activeElement
if (!active || active === document.body || active === lastRetry.current) {
item.current?.querySelector('a')?.focus()
}
}, [state])
// Kept past unmount: the removed button is how the focus is recognised.
const retryRef = useCallback((button: HTMLButtonElement | null) => {
if (button) lastRetry.current = button
}, [])
return { item, retryRef }
}
interface CardContentProps {
entry: CatalogEntry
name: string
state: CardState
onRetry: () => void
retryRef: (button: HTMLButtonElement | null) => void
}
/** Below the name: version line, then the thumbnail, or the failure in its place. */
function CardContent({ entry, name, state, onRetry, retryRef }: CardContentProps) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const details = state.status === 'ready' ? state.details : null
const consult = <ConsultLink sheetId={entry.sheet_id} name={name} />
return (
<>
<p className="text-sm text-[var(--color-ink-muted)]">
{versionLine(entry, details, locale, t)}
</p>
{details?.thumbnail && (
<img
src={details.thumbnail}
alt=""
className="aspect-[16/9] w-full rounded-xl bg-[var(--color-surface-strong)] object-contain"
/>
)}
{details?.viewRefused && (
<Notice tone="info" live={false}>
{t('catalogue.failure.view')}
</Notice>
)}
{state.status === 'failed' ? (
<CardFailure failed={state} onRetry={onRetry} retryRef={retryRef}>
{consult}
</CardFailure>
) : (
<div className="mt-auto">{consult}</div>
)}
</>
)
}
/**
* One published sheet of the catalogue (boards 01 and 30): species, « Publiée »,
* name, version line with the counts the core read, a thumbnail of the main
* view, « Consulter ». A failed load is said in place of the counts and the
* thumbnail, with « Réessayer » where it can help; « Consulter » stays.
*/
export default function CatalogueCard({
entry,
@@ -30,20 +139,17 @@ export default function CatalogueCard({
}) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const details = useCardDetails(entry)
const { state, retry } = useCardDetails(entry)
const { item, retryRef } = useFocusAfterRetry(state)
const name = pickLabel(entry.name, locale)
const line = [
versionText(entry.version_number, details?.publishedAt ?? null, locale, t),
...(details
? [
`${counted(details.viewCount, 'catalogue.card.views', t)}, ${counted(details.zoneCount, 'catalogue.card.zones', t)}`,
]
: []),
].join(' · ')
return (
<li>
<Card as="article" className="flex h-full flex-col gap-3 p-5">
<li ref={item}>
<Card
as="article"
className="flex h-full flex-col gap-3 p-5"
aria-busy={(state.status === 'failed' && state.retrying) || undefined}
>
<div className="flex items-center justify-between gap-2">
<Badge>{speciesName}</Badge>
<span className="inline-flex items-center gap-1 rounded-md bg-[color-mix(in_srgb,var(--severity-mild)_14%,white)] px-2 py-0.5 text-xs font-medium text-[#3f5e3f]">
@@ -52,25 +158,13 @@ export default function CatalogueCard({
</span>
</div>
<h2 className="font-serif text-xl text-[var(--color-ink)]">{name}</h2>
<p className="text-sm text-[var(--color-ink-muted)]">{line}</p>
{details?.thumbnail && (
<img
src={details.thumbnail}
alt=""
className="aspect-[16/9] w-full rounded-xl bg-[var(--color-surface-strong)] object-contain"
/>
)}
<div className="mt-auto">
<ButtonLink
to={`/app/anatomy/${entry.sheet_id}`}
variant="secondary"
className="min-h-11 gap-2"
aria-label={t('catalogue.view_sheet').replace('{name}', name)}
>
<Icon name="eye" size={18} />
{t('catalogue.view')}
</ButtonLink>
</div>
<CardContent
entry={entry}
name={name}
state={state}
onRetry={retry}
retryRef={retryRef}
/>
</Card>
</li>
)
+4
View File
@@ -6,6 +6,8 @@ export interface CardProps {
as?: 'div' | 'section' | 'article'
/** Names a section card by its heading, so it reads as a named region. */
'aria-labelledby'?: string
/** While the card's content is being loaded again (a retry). */
'aria-busy'?: boolean
}
export default function Card({
@@ -13,10 +15,12 @@ export default function Card({
className = '',
as: Tag = 'div',
'aria-labelledby': labelledBy,
'aria-busy': busy,
}: CardProps) {
return (
<Tag
aria-labelledby={labelledBy}
aria-busy={busy}
className={[
'rounded-2xl border border-[var(--color-border)] bg-[var(--color-surface)] shadow-sm',
className,