import { useMemo, useState } from 'react' import { de } from '../strings/de' import { listGerbils } from '../api/gerbils' import { listColorVarieties } from '../api/lookups' import { condition } from '../api/gridify' import type { Gerbil } from '../api/types' import { useApi } from '../hooks/useApi' import { fromDisplayString, genotypeToFarbschlag } from '../genetics' import GroupComposer from '../components/GroupComposer' import './abgabe.css' const UNGROUPED = 'ungrouped' export default function AbgabePage() { const t = de.pages.abgabe const forSale = useApi( () => listGerbils({ filter: condition({ field: 'status', op: '==', value: 'ForSale' }), orderBy: 'name', page: 1, pageSize: 1000, }), [], ) const colorVarieties = useApi(() => listColorVarieties(), []) const animals = useMemo(() => forSale.data?.items ?? [], [forSale.data]) const colorName = useMemo( () => new Map((colorVarieties.data ?? []).map((c) => [c.id, c.name])), [colorVarieties.data], ) // Manual group assignment, seeded from Becken (enclosure) occupancy. const [groupOf, setGroupOf] = useState>({}) const assignment = (g: Gerbil): string => groupOf[g.id] ?? g.enclosureId ?? UNGROUPED // Ordered distinct group ids (in animal order) -> stable 1-based numbering. const groupIds = useMemo(() => { const seen: string[] = [] for (const a of animals) { const gid = assignment(a) if (!seen.includes(gid)) seen.push(gid) } return seen // eslint-disable-next-line react-hooks/exhaustive-deps }, [animals, groupOf]) const farbschlagOf = (g: Gerbil): string => { if (g.colorVarietyId && colorName.has(g.colorVarietyId)) return colorName.get(g.colorVarietyId)! if (g.genotype) { try { return genotypeToFarbschlag(fromDisplayString(g.genotype)) } catch { /* fall through */ } } return '—' } function moveTo(gerbilId: string, groupId: string) { setGroupOf((s) => ({ ...s, [gerbilId]: groupId })) } if (forSale.loading) return

{de.common.loading}

return (

{t.title}

{t.subtitle}

{animals.length === 0 ? (

{t.empty}

) : ( <> {/* Regrouping roster */}

{t.grouping.title}

{t.grouping.byEnclosure}

    {animals.map((a) => (
  • {a.name}
  • ))}
{groupIds.map((gid, i) => ( assignment(a) === gid)} farbschlagOf={farbschlagOf} /> ))} )}
) }