Files
faunosteo/apps/app/src/catalogue/useCatalogue.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

82 lines
2.7 KiB
TypeScript

import { useCallback, useEffect, useState } from 'react'
import { fetchSpeciesLabels, type SpeciesLabel } from '@faunosteo/supabase'
import { getSupabaseClient } from '@/lib/supabase'
import { fetchCatalog, type CatalogEntry } from './catalogue'
import { serverFailure, type CatalogueFailure } from './failure'
export type CatalogueState =
| { status: 'loading' }
| { status: 'ready'; entries: CatalogEntry[]; species: SpeciesLabel[] }
| { status: 'error'; failure: CatalogueFailure }
/** Species names are display only: a failed read leaves the ids as names. */
async function speciesLabels(
client: NonNullable<ReturnType<typeof getSupabaseClient>>,
): Promise<SpeciesLabel[]> {
try {
return await fetchSpeciesLabels(client)
} catch {
return []
}
}
/** The published catalogue and the species names, read once per visit (retry reads again). */
export function useCatalogue() {
const [state, setState] = useState<CatalogueState>({ status: 'loading' })
const [attempt, setAttempt] = useState(0)
useEffect(() => {
let live = true
const client = getSupabaseClient()
setState({ status: 'loading' })
if (!client) {
setState({ status: 'error', failure: 'unavailable' })
return
}
Promise.all([fetchCatalog(client), speciesLabels(client)])
.then(([entries, species]) => {
if (live) setState({ status: 'ready', entries, species })
})
.catch((error: unknown) => {
if (live) setState({ status: 'error', failure: serverFailure(error) })
})
return () => {
live = false
}
}, [attempt])
const retry = useCallback(() => setAttempt((n) => n + 1), [])
return { state, retry }
}
/** Lower case without accents, so « epaule » finds « Épaule ». */
function folded(text: string): string {
return text.normalize('NFD').replace(/\p{Diacritic}/gu, '').toLowerCase()
}
/** Search by sheet name only, French or English (BDR-095), then the species chip. */
export function filterCatalog(
entries: readonly CatalogEntry[],
query: string,
speciesId: string | null,
): CatalogEntry[] {
const term = folded(query.trim())
return entries.filter(
(entry) =>
(speciesId === null || entry.species_id === speciesId) &&
(term === '' ||
folded(entry.name.fr).includes(term) ||
folded(entry.name.en).includes(term)),
)
}
/** The species that have a listed sheet, in the reference order, unknown ids last. */
export function catalogSpecies(
entries: readonly CatalogEntry[],
labels: readonly SpeciesLabel[],
): string[] {
const present = new Set(entries.map((entry) => entry.species_id))
const known = labels.map((label) => label.id).filter((id) => present.has(id))
return [...known, ...[...present].filter((id) => !known.includes(id))]
}