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.
This commit is contained in:
bchanot committed 2026-10-26 17:11:28 +01:00
1 parent e22357630f
commit 930499cd25
36 files changed
+5579 -113

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
import { useCallback } from 'react'
import {
searchAnimals,
searchClients,
type AnimalListItem,
type ClientListItem,
} from '@faunosteo/supabase'
import { getSupabaseClient } from '@/lib/supabase'
/** Matches shown at once: enough to pick from, the term narrows the rest. */
const CHOICES = 8
function backend() {
const client = getSupabaseClient()
if (!client) throw new TypeError('no backend')
return client
}
/** Active clients of the workspace for a term; only an active client can be linked (§7.1). */
export function useClientChoices(workspaceId: string) {
return useCallback(
async (term: string): Promise<ClientListItem[]> =>
(
await searchClients(backend(), {
workspaceId,
query: term,
archived: false,
pageSize: CHOICES,
})
).rows,
[workspaceId],
)
}
/** Active animals of the workspace for a term, every species. */
export function useAnimalChoices(workspaceId: string) {
return useCallback(
async (term: string): Promise<AnimalListItem[]> =>
(
await searchAnimals(backend(), {
workspaceId,
query: term,
speciesId: null,
archived: false,
pageSize: CHOICES,
})
).rows,
[workspaceId],
)
}
+34
View File
@@ -0,0 +1,34 @@
import type { AnimalRelationItem } from '@faunosteo/supabase'
const ROLE_ORDER = { owner: 0, keeper: 1, contact: 2 } as const
const byName = (a: AnimalRelationItem, b: AnimalRelationItem) =>
a.client.display_name.localeCompare(b.client.display_name) ||
a.id.localeCompare(b.id)
/**
* An animal's relations for its page: the current ones by role (owners
* first) then client name; the former ones, most recently ended first.
* `ended_at` orders the history only; the end date shown is `ended_on`.
*/
export function groupRelations(relations: readonly AnimalRelationItem[]) {
const current = relations
.filter((r) => r.ended_at === null)
.sort(
(a, b) =>
ROLE_ORDER[a.relation_role] - ROLE_ORDER[b.relation_role] ||
byName(a, b),
)
const former = relations
.filter((r) => r.ended_at !== null)
.sort(
(a, b) =>
(b.ended_at ?? '').localeCompare(a.ended_at ?? '') || byName(a, b),
)
return {
current,
former,
/** No current owner: the page says « Propriétaire non renseigné », never a placeholder client. */
hasOwner: current.some((r) => r.relation_role === 'owner'),
}
}
@@ -0,0 +1,54 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import {
classifyRecordError,
fetchAnimalRelations,
type AnimalRelationItem,
type RecordFailureKind,
} from '@faunosteo/supabase'
import { getSupabaseClient } from '@/lib/supabase'
export type AnimalRelationsState =
| { status: 'loading' }
| { status: 'ready'; relations: AnimalRelationItem[] }
| { status: 'error'; failure: RecordFailureKind }
/**
* The relations of one animal (voided ones never come back). `reload`
* resolves once the new answer is on screen, so a confirmation can stay open
* until the change it made is shown; the rows stay visible meanwhile. An
* answer older than the last request, or arriving after unmount, is dropped.
*/
export function useAnimalRelations(animalId: string) {
const [state, setState] = useState<AnimalRelationsState>({
status: 'loading',
})
const generation = useRef(0)
const reload = useCallback(async () => {
const current = ++generation.current
const client = getSupabaseClient()
if (!client) {
setState({ status: 'error', failure: 'unavailable' })
return
}
try {
const relations = await fetchAnimalRelations(client, animalId)
if (current === generation.current)
setState({ status: 'ready', relations })
} catch (error) {
if (current === generation.current)
setState({ status: 'error', failure: classifyRecordError(error) })
}
}, [animalId])
useEffect(() => {
// The ref object is stable: bumping it on unmount drops the answer in flight.
const requests = generation
void reload()
return () => {
requests.current++
}
}, [reload])
return { state, reload }
}