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}
) } const l = litter.data const registered = juveniles.data?.totalCount ?? 0 const parentLink = (id: string | null, g: { name: string } | null) => id && g ? {g.name} : t.detail.unknownParent return (

{l.name}

{t.detail.edit} {t.detail.back}
{t.fields.date}
{formatDate(l.date)}
{t.detail.parents}
{parentLink(fatherId, father.data)} × {parentLink(motherId, mother.data)}
{t.detail.registered}
{registered} {l.totalBorn != null ? ` / ${l.totalBorn}` : ''}
{t.fields.expectedGoHomeDate}
{formatDate(l.expectedGoHomeDate)}
{l.notes && (
{t.fields.notes}
{l.notes}
)}

{t.detail.juveniles}

+ {t.detail.registerJuvenile}
{juveniles.loading &&

{de.common.loading}

} {!juveniles.loading && registered === 0 &&

{t.detail.noJuveniles}

} {registered > 0 && ( )}

{t.detail.expectedColors}

{expected ? ( ) : (

{t.detail.needParentsGenotype}

)}
) }