import { useMemo, useState } from 'react' import { useSearchParams } from 'react-router-dom' import { de } from '../strings/de' import { breed, fromDisplayString, type BreedingResult } from '../genetics' import { getGerbil } from '../api/gerbils' import { useApi } from '../hooks/useApi' import ParentSelector, { type ParentValue } from '../components/ParentSelector' import AnimalPicker from '../components/AnimalPicker' import BreedingResultView from '../components/BreedingResultView' import { isValidGenotype } from '../format/genotypeText' const EMPTY_PARENT: ParentValue = { genotype: '', animalName: null } export default function GenetikPage() { const t = de.pages.genetik const [father, setFather] = useState(EMPTY_PARENT) const [mother, setMother] = useState(EMPTY_PARENT) // Pre-fill parents from query params: // ?parent= single animal from its detail page (gender picks side) // ?vater=&mutter= a whole pair, e.g. from the Zuchtpaar overview const [searchParams] = useSearchParams() const parentId = searchParams.get('parent') const vaterId = searchParams.get('vater') const mutterId = searchParams.get('mutter') const prefill = useApi(() => (parentId ? getGerbil(parentId) : Promise.resolve(null)), [parentId]) const prefillVater = useApi(() => (vaterId ? getGerbil(vaterId) : Promise.resolve(null)), [vaterId]) const prefillMutter = useApi(() => (mutterId ? getGerbil(mutterId) : Promise.resolve(null)), [mutterId]) const [prefilledKey, setPrefilledKey] = useState(null) const prefillKey = `${parentId ?? ''}|${vaterId ?? ''}|${mutterId ?? ''}` const allReady = (!parentId || prefill.data) && (!vaterId || prefillVater.data) && (!mutterId || prefillMutter.data) if (prefillKey !== '||' && allReady && prefilledKey !== prefillKey) { setPrefilledKey(prefillKey) const toValue = (g: { genotype: string | null; name: string }): ParentValue => ({ genotype: g.genotype ?? '', animalName: g.name, }) if (prefillVater.data) setFather(toValue(prefillVater.data)) if (prefillMutter.data) setMother(toValue(prefillMutter.data)) if (prefill.data) { if (prefill.data.gender === 'female') setMother(toValue(prefill.data)) else setFather(toValue(prefill.data)) } } const bothValid = isValidGenotype(father.genotype) && isValidGenotype(mother.genotype) const result: BreedingResult | null = useMemo(() => { if (!bothValid) return null // breed() is pure & client-side; ParentSelector already validated both, so // fromDisplayString won't throw. return breed(fromDisplayString(father.genotype), fromDisplayString(mother.genotype)) }, [bothValid, father.genotype, mother.genotype]) return (

{t.title}

{t.subtitle}

setFather({ genotype: g.genotype ?? '', animalName: g.name })} /> } /> setMother({ genotype: g.genotype ?? '', animalName: g.name })} /> } />
{!bothValid &&

{t.needBoth}

} {result && }
) }