import { useState } from 'react'; import { Egg } from 'lucide-react'; import { attachAdminMountEggsInput, type AdminMountWithRelations, useAttachAdminMountEggs, } from '@/api/admin/mounts/queries'; import { useAdminEggs } from '@/api/admin/eggs/queries'; import { Dialog } from '@/components/elements/dialog'; import Button from '@/components/elements/Button'; import Select from '@/components/ui/Select'; import Label from '@/components/elements/Label'; import Spinner from '@/components/elements/Spinner'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from '@/components/ui/empty'; import { relationshipData } from '@/api/relationships'; import { cn } from '@/lib/cn'; interface Props { mount: AdminMountWithRelations; open: boolean; onClose: () => void; onAttached: () => void; } export default function AttachEggsModal({ mount, open, onClose, onAttached }: Props) { const [selected, setSelected] = useState([]); const attachEggs = useAttachAdminMountEggs(); const submitting = attachEggs.isPending; const { data: eggs } = useAdminEggs(); const attachedIds = new Set(relationshipData(mount.attributes.relationships?.eggs).map((egg) => egg.attributes.id)); const availableEggs = (eggs?.data ?? []).filter((egg) => !attachedIds.has(egg.attributes.id)); const submit = () => { if (selected.length === 0) { return; } attachEggs .mutateAsync(attachAdminMountEggsInput(mount.attributes.id, selected)) .then(() => { setSelected([]); onAttached(); }) .catch(() => { // Error toast is handled by the mutation. }); }; return ( {eggs === undefined ? ( ) : ( <> {availableEggs.length === 0 ? ( {eggs.data.length === 0 ? 'No eggs yet' : 'All eggs attached'} {eggs.data.length === 0 ? 'Create or import an egg before attaching it to this mount.' : 'Every egg is already attached to this mount.'} ) : ( <>