import { useMemo } from 'react' import { Link, useParams } from 'react-router-dom' import { de } from '../strings/de' import { getLitter } from '../api/litters' import { getGerbil, listGerbils } from '../api/gerbils' import { condition } from '../api/gridify' import { useApi } from '../hooks/useApi' import { formatDate } from '../format/labels' import { isValidGenotype } from '../format/genotypeText' import { breed, fromDisplayString, type BreedingResult } from '../genetics' import BreedingResultView from '../components/BreedingResultView' export default function WurfDetailPage() { const t = de.pages.litters const { id = '' } = useParams() const litter = useApi(() => getLitter(id), [id]) const fatherId = litter.data?.fatherId ?? null const motherId = litter.data?.motherId ?? null const father = useApi(() => (fatherId ? getGerbil(fatherId) : Promise.resolve(null)), [fatherId]) const mother = useApi(() => (motherId ? getGerbil(motherId) : Promise.resolve(null)), [motherId]) // Juveniles = gerbils whose litterId is this litter. const juveniles = useApi( () => id ? listGerbils({ filter: condition({ field: 'litterId', op: '==', value: id }), orderBy: 'name', page: 1, pageSize: 1000, }) : Promise.resolve(null), [id], ) // Expected Farbschlag distribution for this pairing (client-side GEN-1). const expected: BreedingResult | null = useMemo(() => { const fg = father.data?.genotype const mg = mother.data?.genotype if (!fg || !mg || !isValidGenotype(fg) || !isValidGenotype(mg)) return null return breed(fromDisplayString(fg), fromDisplayString(mg)) }, [father.data, mother.data]) if (litter.loading) return
{de.common.loading}
if (litter.error || !litter.data) { return ({litter.error ?? t.detail.notFound}
{t.detail.back}{de.common.loading}
} {!juveniles.loading && registered === 0 &&{t.detail.noJuveniles}
} {registered > 0 && ({t.detail.needParentsGenotype}
)}