Routes /app/animals, new, :animalId, edit under one workspace gate; list with species filter; form (D-R7, U3 birth, no default sex or role, optional first client); page with current and former relations, end, dates, void; client page links its animals, adds one pre-associated, links an existing one. Card now passes aria-labelledby, which it silently dropped.
101 lines
3.0 KiB
TypeScript
101 lines
3.0 KiB
TypeScript
import {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useMemo,
|
|
useState,
|
|
type ReactNode,
|
|
} from 'react'
|
|
import {
|
|
classifyRecordError,
|
|
fetchSpeciesLabels,
|
|
type RecordFailureKind,
|
|
type SpeciesLabel,
|
|
} from '@faunosteo/supabase'
|
|
import { getSupabaseClient } from '@/lib/supabase'
|
|
import { pickLocaleLabel } from '@/i18n/pickLocaleLabel'
|
|
import type { Locale } from '@/i18n/types'
|
|
|
|
export type SpeciesState =
|
|
| { status: 'idle' }
|
|
| { status: 'loading' }
|
|
| { status: 'ready'; labels: SpeciesLabel[] }
|
|
| { status: 'error'; failure: RecordFailureKind }
|
|
|
|
interface SpeciesContextValue {
|
|
state: SpeciesState
|
|
/** Starts the read once; later calls change nothing. */
|
|
request: () => void
|
|
retry: () => void
|
|
}
|
|
|
|
const SpeciesContext = createContext<SpeciesContextValue | null>(null)
|
|
|
|
/**
|
|
* The active species (reference rows of contract §5.4), read once per
|
|
* workspace and only when a page needs them: the list filter, the form and
|
|
* the record pages share one answer. Mounted under RequireWorkspace, it
|
|
* leaves with the workspace on a change of account.
|
|
*/
|
|
export function SpeciesProvider({ children }: { children: ReactNode }) {
|
|
const [state, setState] = useState<SpeciesState>({ status: 'idle' })
|
|
// 0 until a page asks; each retry bumps it.
|
|
const [attempt, setAttempt] = useState(0)
|
|
|
|
useEffect(() => {
|
|
if (attempt === 0) return
|
|
let live = true
|
|
const client = getSupabaseClient()
|
|
setState({ status: 'loading' })
|
|
if (!client) {
|
|
setState({ status: 'error', failure: 'unavailable' })
|
|
return
|
|
}
|
|
fetchSpeciesLabels(client)
|
|
.then((labels) => {
|
|
if (live) setState({ status: 'ready', labels })
|
|
})
|
|
.catch((error: unknown) => {
|
|
if (live)
|
|
setState({ status: 'error', failure: classifyRecordError(error) })
|
|
})
|
|
return () => {
|
|
live = false
|
|
}
|
|
}, [attempt])
|
|
|
|
const request = useCallback(() => setAttempt((n) => (n === 0 ? 1 : n)), [])
|
|
const retry = useCallback(() => setAttempt((n) => n + 1), [])
|
|
const value = useMemo(
|
|
() => ({ state, request, retry }),
|
|
[state, request, retry],
|
|
)
|
|
return (
|
|
<SpeciesContext.Provider value={value}>{children}</SpeciesContext.Provider>
|
|
)
|
|
}
|
|
|
|
/** The species labels of the workspace; the first page that asks starts the read. */
|
|
export function useSpecies(): Omit<SpeciesContextValue, 'request'> {
|
|
const context = useContext(SpeciesContext)
|
|
if (!context) throw new Error('useSpecies needs a SpeciesProvider')
|
|
const { request } = context
|
|
useEffect(() => request(), [request])
|
|
return { state: context.state, retry: context.retry }
|
|
}
|
|
|
|
/** The species name in the locale, followed by the « Autre » detail when there is one. */
|
|
export function speciesName(
|
|
labels: readonly SpeciesLabel[],
|
|
speciesId: string,
|
|
detail: string | null,
|
|
locale: Locale,
|
|
): string {
|
|
const label = labels.find((s) => s.id === speciesId)
|
|
const name = label
|
|
? pickLocaleLabel(label.name_fr, label.name_en, locale, speciesId)
|
|
: speciesId
|
|
return detail ? `${name} (${detail})` : name
|
|
}
|