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.
107 lines
4.0 KiB
TypeScript
107 lines
4.0 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
import { listSheetVersions, type FaunosteoClient } from '@faunosteo/supabase'
|
|
import { coreRuntime } from '@/core/runtime'
|
|
import { getSupabaseClient } from '@/lib/supabase'
|
|
import { loadVersion, type CatalogEntry } from './catalogue'
|
|
import { coreFailure, type CatalogueFailure } from './failure'
|
|
|
|
export interface CardDetails {
|
|
publishedAt: string | null
|
|
viewCount: number
|
|
zoneCount: number
|
|
/** Blob URL of the core's composed main view, shown as an image; null when it cannot be drawn. */
|
|
thumbnail: string | null
|
|
/** The core refused to compose the main view (owner D2): said in the card, counts kept. */
|
|
viewRefused: boolean
|
|
}
|
|
|
|
export type CardState =
|
|
| { status: 'loading' }
|
|
| { status: 'ready'; details: CardDetails }
|
|
| { status: 'failed'; failure: CatalogueFailure; retrying: boolean; attempt: number }
|
|
|
|
type CardLoad =
|
|
| { ok: true; details: Omit<CardDetails, 'thumbnail'>; svg: string | null }
|
|
| { ok: false; failure: CatalogueFailure }
|
|
|
|
/** One card's reads; the core handle is freed as soon as the main view is composed. */
|
|
async function loadCard(
|
|
client: FaunosteoClient,
|
|
sheetId: string,
|
|
versionId: string,
|
|
): Promise<CardLoad> {
|
|
const [versions, load] = await Promise.all([
|
|
listSheetVersions(client, sheetId).catch(() => []),
|
|
loadVersion(client, versionId),
|
|
])
|
|
if (!load.ok) return load
|
|
const { state, preview } = load
|
|
const main = state.index ? preview.canvas(state.index.main_view_id) : null
|
|
preview.free()
|
|
// The engine failing on the view is the engine's failure, not the document's (D2).
|
|
if (main && !main.ok && coreFailure(main.kind) === 'core_unavailable') {
|
|
return { ok: false, failure: 'core_unavailable' }
|
|
}
|
|
return {
|
|
ok: true,
|
|
svg: main?.ok ? main.value : null,
|
|
details: {
|
|
publishedAt: versions.find((v) => v.id === versionId)?.published_at ?? null,
|
|
viewCount: state.document.views.length,
|
|
zoneCount: state.document.finding_zones.length,
|
|
viewRefused: main !== null && !main.ok,
|
|
},
|
|
}
|
|
}
|
|
|
|
/**
|
|
* What a catalogue card adds to the list answer: the publication date, the
|
|
* counts the core read from the document, and a thumbnail of the main view.
|
|
* Loaded per card once the list is shown (one version and its drawings each).
|
|
* A failure is the card's to say (board 30); `retry` reloads this card only,
|
|
* keeping the failed state (`retrying`) so its line and button stay in place.
|
|
*/
|
|
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
|
|
let thumbnail: string | null = null
|
|
const client = getSupabaseClient()
|
|
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
|
|
}
|
|
if (load.svg !== null) {
|
|
thumbnail = URL.createObjectURL(new Blob([load.svg], { type: 'image/svg+xml' }))
|
|
}
|
|
setState({ status: 'ready', details: { ...load.details, thumbnail } })
|
|
})
|
|
return () => {
|
|
live = false
|
|
if (thumbnail) URL.revokeObjectURL(thumbnail)
|
|
}
|
|
}, [entry.sheet_id, entry.version_id, attempt])
|
|
|
|
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 || running.current) return
|
|
running.current = true
|
|
setState({ ...failed, retrying: true })
|
|
const restart =
|
|
failed.failure === 'core_unavailable' ? coreRuntime().retry() : Promise.resolve(true)
|
|
void restart.finally(() => setAttempt((n) => n + 1))
|
|
}, [failed])
|
|
|
|
return { state, retry }
|
|
}
|