fix(app): card retry keeps focus and voice when no retry is left

A retry ending on a failure with no retry (network to integrity, engine
stopped for good) keeps the polite region and moves focus to Consulter.
Busy look on Réessayer, error tone once the engine stopped, one attempt
for two calls in one tick; tests for the stopped engine, two core cards
and no blob URL after unmount.
This commit is contained in:
bchanot committed 2026-11-17 00:59:30 +01:00
1 parent 4a7919c763
commit b378cf066f
4 files changed
+97 -15

No files matched your search

+7 -2
View File
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from 'react'
import { useCallback, useEffect, useRef, useState } from 'react'
import { listSheetVersions, type FaunosteoClient } from '@faunosteo/supabase'
import { coreRuntime } from '@/core/runtime'
import { getSupabaseClient } from '@/lib/supabase'
@@ -64,6 +64,9 @@ async function loadCard(
export function useCardDetails(entry: CatalogEntry): { state: CardState; retry: () => void } {
const [state, setState] = useState<CardState>({ status: 'loading' })
const [attempt, setAttempt] = useState(0)
// Set by the first call, cleared when its attempt settles: two calls in one
// tick start one attempt.
const running = useRef(false)
useEffect(() => {
let live = true
@@ -72,6 +75,7 @@ export function useCardDetails(entry: CatalogEntry): { state: CardState; retry:
if (!client) return
void loadCard(client, entry.sheet_id, entry.version_id).then((load) => {
if (!live) return
running.current = false
if (!load.ok) {
setState({ status: 'failed', failure: load.failure, retrying: false, attempt })
return
@@ -90,7 +94,8 @@ export function useCardDetails(entry: CatalogEntry): { state: CardState; retry:
const failed = state.status === 'failed' && !state.retrying ? state : null
// Ignored while an attempt runs; the core may need a restart before the card loads again.
const retry = useCallback(() => {
if (!failed) return
if (!failed || running.current) return
running.current = true
setState({ ...failed, retrying: true })
const restart =
failed.failure === 'core_unavailable' ? coreRuntime().retry() : Promise.resolve(true)
@@ -26,27 +26,30 @@ export interface CardFailureProps {
/**
* 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).
* region, so several cards failing at load stay quiet), in the warning tone
* where « Réessayer » can work and the error tone where it cannot (a refused
* version, the engine stopped for good), and a polite region that says the
* line again only after a retry failed (empty at load and while an attempt
* runs). The region stays once a retry ran, even when the new failure offers
* no retry, so the line it ends on is still said.
*/
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 { integrity, retry } = cardFailure(failure, stopped)
const text = failureText(t, failure, integrity)
return (
<>
<Notice
tone={refused ? 'error' : 'warning'}
tone={retry ? 'warning' : 'error'}
icon={failure === 'network' ? 'wifi-off' : 'alert'}
live={false}
>
{integrity ? <strong className="font-semibold">{text}</strong> : text}
</Notice>
{retry && (
{(retry || attempt > 0) && (
<div aria-live="polite" className="sr-only">
{!retrying && attempt > 0 ? text : ''}
</div>
@@ -57,7 +60,7 @@ export default function CardFailure({ failed, onRetry, retryRef, children }: Car
ref={retryRef}
type="button"
variant="secondary"
className="min-h-11"
className="min-h-11 aria-disabled:cursor-wait aria-disabled:opacity-60"
aria-disabled={retrying ? 'true' : undefined}
onClick={onRetry}
>
@@ -56,8 +56,10 @@ function ConsultLink({ sheetId, name }: { sheetId: string; name: string }) {
}
/**
* 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.
* When a retry settles without its « Réessayer » (success, or a failure with no
* retry left), the focus left on the removed button (or on nothing) moves to
* the card's « Consulter » (R-3); focus elsewhere, or on a « Réessayer » still
* there, is kept.
*/
function useFocusAfterRetry(state: CardState) {
const item = useRef<HTMLLIElement>(null)
@@ -65,11 +67,17 @@ function useFocusAfterRetry(state: CardState) {
const wasRetrying = useRef(false)
useEffect(() => {
if (state.status === 'failed') wasRetrying.current = state.retrying
if (state.status !== 'ready' || !wasRetrying.current) return
if (state.status === 'loading') return
if (state.status === 'failed' && state.retrying) {
wasRetrying.current = true
return
}
if (!wasRetrying.current) return
wasRetrying.current = false
const button = lastRetry.current
if (button?.isConnected) return
const active = document.activeElement
if (!active || active === document.body || active === lastRetry.current) {
if (!active || active === document.body || active === button) {
item.current?.querySelector('a')?.focus()
}
}, [state])