Files
faunosteo/apps/app/src/animals/useAnimalList.ts
T
bchanot 930499cd25 feat(app): animal screens, relation panels, animals on the client page
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.
2026-10-26 17:11:28 +01:00

111 lines
3.2 KiB
TypeScript

import { useCallback, useEffect, useRef, useState } from 'react'
import {
classifyRecordError,
searchAnimals,
type AnimalListItem,
type NameCursor,
type RecordFailureKind,
} from '@faunosteo/supabase'
import { getSupabaseClient } from '@/lib/supabase'
import { SEARCH_DELAY_MS } from '@/clients/useClientList'
export type AnimalListState =
| { status: 'loading' }
| {
status: 'ready'
rows: AnimalListItem[]
next: NameCursor | null
moreFailure: RecordFailureKind | null
}
| { status: 'error'; failure: RecordFailureKind }
/**
* The animals of the workspace for a term, a species and the archived
* toggle: the server searches (search_animals, POST), pages of 25 in name
* order, « Afficher plus » appends the next page. An answer to an older
* search is dropped.
*/
export function useAnimalList(workspaceId: string) {
const [query, setQuery] = useState('')
const [term, setTerm] = useState('')
const [speciesId, setSpeciesId] = useState<string | null>(null)
const [archived, setArchived] = useState(false)
const [state, setState] = useState<AnimalListState>({ status: 'loading' })
const [loadingMore, setLoadingMore] = useState(false)
const [reload, setReload] = useState(0)
const generation = useRef(0)
useEffect(() => {
const timer = setTimeout(() => setTerm(query.trim()), SEARCH_DELAY_MS)
return () => clearTimeout(timer)
}, [query])
useEffect(() => {
const current = ++generation.current
const client = getSupabaseClient()
setState({ status: 'loading' })
if (!client) {
setState({ status: 'error', failure: 'unavailable' })
return
}
searchAnimals(client, { workspaceId, query: term, speciesId, archived })
.then((page) => {
if (current === generation.current)
setState({
status: 'ready',
rows: page.rows,
next: page.next,
moreFailure: null,
})
})
.catch((error: unknown) => {
if (current === generation.current)
setState({ status: 'error', failure: classifyRecordError(error) })
})
}, [workspaceId, term, speciesId, archived, reload])
const loadMore = useCallback(async () => {
const client = getSupabaseClient()
if (state.status !== 'ready' || !state.next || !client || loadingMore)
return
const current = generation.current
setLoadingMore(true)
try {
const page = await searchAnimals(client, {
workspaceId,
query: term,
speciesId,
archived,
after: state.next,
})
if (current === generation.current)
setState({
status: 'ready',
rows: [...state.rows, ...page.rows],
next: page.next,
moreFailure: null,
})
} catch (error) {
if (current === generation.current)
setState({ ...state, moreFailure: classifyRecordError(error) })
} finally {
setLoadingMore(false)
}
}, [state, loadingMore, workspaceId, term, speciesId, archived])
return {
query,
setQuery,
/** The term the shown rows answer (the typed one after the pause). */
term,
speciesId,
setSpeciesId,
archived,
setArchived,
state,
loadingMore,
loadMore,
retry: () => setReload((n) => n + 1),
}
}