Files
faunosteo/apps/app/src/catalogue/useCardDetails.ts
T
bchanot 0176b3184d feat(app): administration screens and anatomical catalogue (V1-2.5 G, H, I)
Administration page (board 10), species, rights, official sheets list with filters, creation from the core's empty document, overview with the working-copy action; catalogue list (search by sheet name), sheet and version pages through PreviewSheet with zoom and pan. App 650, typecheck, lint, build green.
2026-11-02 17:18:27 +01:00

57 lines
1.9 KiB
TypeScript

import { useEffect, useState } from 'react'
import { listSheetVersions } from '@faunosteo/supabase'
import { getSupabaseClient } from '@/lib/supabase'
import { loadVersion, type CatalogEntry } from './catalogue'
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
}
/**
* 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.
*/
export function useCardDetails(entry: CatalogEntry): CardDetails | null {
const [details, setDetails] = useState<CardDetails | null>(null)
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()
if (!live) return
if (main?.ok) {
thumbnail = URL.createObjectURL(new Blob([main.value], { type: 'image/svg+xml' }))
}
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,
})
})
return () => {
live = false
if (thumbnail) URL.revokeObjectURL(thumbnail)
}
}, [entry.sheet_id, entry.version_id])
return details
}