import { useState } from 'react'; import { useStore } from '@tanstack/react-form'; import { toast } from 'sonner'; import { useAppForm, Form } from '@/components/form'; import Select, { type SelectGroup } from '@/components/ui/Select'; import { type LocationWithNodes, useAdminNodesGroupedByLocation, useAllAdminNodeTags } from '@/api/admin/nodes/queries'; import { type AdminServer, transferAdminServerInput, useAdminEggForServer, useAdminServerUnassignedAllocations, useTransferAdminServer, } from '@/api/admin/servers/queries'; import { relationshipData } from '@/api/relationships'; import { allocationLabel, transferServerBodyFromFormValues } from '@/components/admin/servers/helpers'; import TagCheckAlert from '@/components/admin/tags/TagCheckAlert'; import { nodeAcceptsEgg, tagCheckError } from '@/components/admin/tags/helpers'; import { Dialog } from '@/components/elements/dialog'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import Label from '@/components/elements/Label'; import Button from '@/components/elements/Button'; interface Props { server: AdminServer; open: boolean; onClose: () => void; } interface Values { allocationId: number; allocationAdditional: number[]; } /** Whether a node accepts the egg; every node does until the tags have loaded. */ const useNodeAcceptsEgg = (eggId: number, enabled: boolean) => { const { data: nodes } = useAllAdminNodeTags({ enabled }); const { data: egg } = useAdminEggForServer(eggId, { enabled }); const eggTags = relationshipData(egg?.attributes.relationships?.tags); const nodeTags = new Map( (nodes?.data ?? []).map((node) => [node.attributes.id, relationshipData(node.attributes.relationships?.tags)]) ); return (nodeId: number) => !egg || nodeAcceptsEgg(nodeTags.get(nodeId) ?? [], eggTags); }; const transferNodeGroups = ( groups: LocationWithNodes[], currentNodeId: number, acceptsEgg: (nodeId: number) => boolean ): SelectGroup[] => groups.flatMap(({ location, nodes }) => { const options = nodes .filter((node) => node.id !== currentNodeId) .map((node) => ({ value: node.id, label: acceptsEgg(node.id) ? node.name : `${node.name} (does not accept this egg)`, })); const { long, short } = location.attributes; return options.length > 0 ? [{ label: long ? `${long} (${short})` : short, options }] : []; }); export default function TransferServerModal({ server, open, onClose }: Props) { const { attributes } = server; const transferServer = useTransferAdminServer(); const [nodeId, setNodeId] = useState(0); const [rejection, setRejection] = useState(null); const { data: groups = [] } = useAdminNodesGroupedByLocation({ enabled: open, }); const acceptsEgg = useNodeAcceptsEgg(attributes.egg, open); const { data: allocationsResponse } = useAdminServerUnassignedAllocations(nodeId, { enabled: open && nodeId > 0, }); const allocations = allocationsResponse?.data ?? []; const form = useAppForm({ defaultValues: { allocationId: 0, allocationAdditional: [] } as Values, onSubmitMeta: { ignoreTags: false }, onSubmit: async ({ value, meta }) => { if (!nodeId) { toast.error('A target node must be selected.'); return; } if (!value.allocationId) { toast.error('A default allocation must be selected.'); return; } try { await transferServer.mutateAsync( transferAdminServerInput(attributes.id, { ...transferServerBodyFromFormValues({ nodeId: Number(nodeId), allocationId: Number(value.allocationId), allocationAdditional: value.allocationAdditional.map(Number), }), ignore_tags: meta.ignoreTags, }) ); setRejection(null); onClose(); } catch (error) { setRejection(tagCheckError(error, 'node_id')); } }, }); const isSubmitting = useStore(form.store, (state) => state.isSubmitting); const defaultAllocationId = useStore(form.store, (state) => state.values.allocationId); const allocationOptions = allocations.map((allocation) => ({ value: allocation.attributes.id, label: allocationLabel(allocation), })); const additionalOptions = allocationOptions.filter((option) => option.value !== defaultAllocationId); return (