import { useState } from 'react'; import { HardDrive } from 'lucide-react'; import { attachAdminMountNodesInput, type AdminMountWithRelations, useAttachAdminMountNodes, } from '@/api/admin/mounts/queries'; import { useAdminNodesGroupedByLocation } from '@/api/admin/nodes/queries'; import { Dialog } from '@/components/elements/dialog'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; 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 { 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 AttachNodesModal({ mount, open, onClose, onAttached }: Props) { const [selected, setSelected] = useState([]); const attachNodes = useAttachAdminMountNodes(); const submitting = attachNodes.isPending; const { data: locations } = useAdminNodesGroupedByLocation({ enabled: open }); const attachedIds = new Set( relationshipData(mount.attributes.relationships?.nodes).map((node) => node.attributes.id) ); type LocationWithAvailableNodes = { location: NonNullable[number]['location']; nodes: NonNullable[number]['nodes']; }; const groups = (locations ?? []).reduce((groups, group) => { const nodes = group.nodes.filter((node) => !attachedIds.has(node.id)); if (nodes.length > 0) { groups.push({ location: group.location, nodes }); } return groups; }, []); const hasNodes = (locations ?? []).some((group) => group.nodes.length > 0); const submit = () => { if (selected.length === 0) { return; } attachNodes .mutateAsync(attachAdminMountNodesInput(mount.attributes.id, selected)) .then(() => { setSelected([]); onAttached(); }) .catch(() => { // Error toast is handled by the mutation. }); }; return ( {locations === undefined ? ( ) : ( <> {groups.length === 0 ? ( {hasNodes ? 'All nodes attached' : 'No nodes yet'} {hasNodes ? 'Every node is already attached to this mount.' : 'Create a node before attaching it to this mount.'} ) : ( <>