feat(app): catalogue cards say an integrity or load failure (board 30)
This commit is contained in:
1 parent
f061111e25
commit
6ad8d8347c
8 files changed
+564
-63
No files matched your search
@@ -58,3 +58,21 @@ export function coreFailure(kind: string): CatalogueFailure {
|
||||
if (kind === 'core_unavailable' || kind === 'internal') return 'core_unavailable'
|
||||
return 'invalid_document'
|
||||
}
|
||||
|
||||
const INTEGRITY: ReadonlySet<CatalogueFailure> = new Set([
|
||||
'fingerprint_mismatch',
|
||||
'drawing_hash_mismatch',
|
||||
'drawing_missing',
|
||||
'invalid_document',
|
||||
])
|
||||
|
||||
/**
|
||||
* How a catalogue card treats its failure (board 30, A10, owner D1): never hidden; `refused` when the
|
||||
* version itself is refused (integrity, or over the size limits), retried only where a retry can work.
|
||||
*/
|
||||
export function cardFailure(failure: CatalogueFailure, stopped: boolean) {
|
||||
const integrity = INTEGRITY.has(failure)
|
||||
const refused = integrity || failure === 'caps'
|
||||
const retry = !refused && !(failure === 'core_unavailable' && stopped)
|
||||
return { integrity, refused, retry }
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { listSheetVersions } from '@faunosteo/supabase'
|
||||
import { useCallback, useEffect, 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
|
||||
@@ -9,48 +11,91 @@ export interface CardDetails {
|
||||
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);
|
||||
* the core handle is freed as soon as the canvas is composed. Any failure
|
||||
* leaves the card with its name and its link only.
|
||||
* 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): CardDetails | null {
|
||||
const [details, setDetails] = useState<CardDetails | null>(null)
|
||||
export function useCardDetails(entry: CatalogEntry): { state: CardState; retry: () => void } {
|
||||
const [state, setState] = useState<CardState>({ status: 'loading' })
|
||||
const [attempt, setAttempt] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
let live = true
|
||||
let thumbnail: string | null = null
|
||||
const client = getSupabaseClient()
|
||||
if (!client) return
|
||||
void Promise.all([
|
||||
listSheetVersions(client, entry.sheet_id).catch(() => []),
|
||||
loadVersion(client, entry.version_id),
|
||||
]).then(([versions, load]) => {
|
||||
if (!load.ok) return
|
||||
const { state, preview } = load
|
||||
const main = state.index ? preview.canvas(state.index.main_view_id) : null
|
||||
preview.free()
|
||||
void loadCard(client, entry.sheet_id, entry.version_id).then((load) => {
|
||||
if (!live) return
|
||||
if (main?.ok) {
|
||||
thumbnail = URL.createObjectURL(new Blob([main.value], { type: 'image/svg+xml' }))
|
||||
if (!load.ok) {
|
||||
setState({ status: 'failed', failure: load.failure, retrying: false, attempt })
|
||||
return
|
||||
}
|
||||
setDetails({
|
||||
publishedAt:
|
||||
versions.find((v) => v.id === entry.version_id)?.published_at ?? null,
|
||||
viewCount: state.document.views.length,
|
||||
zoneCount: state.document.finding_zones.length,
|
||||
thumbnail,
|
||||
})
|
||||
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])
|
||||
}, [entry.sheet_id, entry.version_id, attempt])
|
||||
|
||||
return details
|
||||
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
|
||||
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 }
|
||||
}
|
||||
Reference in new issue
Block a user