import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { useTranslation } from 'react-i18next'; import { X, Loader2, Link2, Check } from 'lucide-react'; import { api } from '../api/client'; import { Button } from './Button'; import { useToast } from '../contexts/ToastContext'; interface LinkSpoolModalProps { isOpen: boolean; onClose: () => void; trayUuid: string; trayInfo?: { type: string; color: string; location: string; }; } export function LinkSpoolModal({ isOpen, onClose, trayUuid, trayInfo }: LinkSpoolModalProps) { const { t } = useTranslation(); const queryClient = useQueryClient(); const { showToast } = useToast(); const [selectedSpoolId, setSelectedSpoolId] = useState(null); // Fetch unlinked spools const { data: unlinkedSpools, isLoading } = useQuery({ queryKey: ['unlinked-spools'], queryFn: api.getUnlinkedSpools, enabled: isOpen, }); // Link mutation const linkMutation = useMutation({ mutationFn: (spoolId: number) => api.linkSpool(spoolId, trayUuid), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['unlinked-spools'] }); queryClient.invalidateQueries({ queryKey: ['linked-spools'] }); queryClient.invalidateQueries({ queryKey: ['spoolman-status'] }); showToast(t('spoolman.linkSuccess'), 'success'); onClose(); }, onError: (error: Error) => { showToast(`${t('spoolman.linkFailed')}: ${error.message}`, 'error'); }, }); if (!isOpen) return null; const handleLink = () => { if (selectedSpoolId) { linkMutation.mutate(selectedSpoolId); } }; return (
{/* Backdrop */}
{/* Modal */}
{/* Header */}

{t('spoolman.linkToSpoolman')}

{/* Content */}
{/* Tray info */} {trayInfo && (

Linking AMS tray:

{trayInfo.color && ( )} {trayInfo.type} ({trayInfo.location})
)} {/* Spool UUID */}

{t('spoolman.spoolId')}:

{trayUuid}
{/* Spool list */}

{t('spoolman.selectSpool')}:

{isLoading ? (
) : unlinkedSpools && unlinkedSpools.length > 0 ? (
{unlinkedSpools.map((spool) => ( ))}
) : (

{t('spoolman.noUnlinkedSpools')}

)}
{/* Footer */}
{/* Error */} {linkMutation.isError && (
{(linkMutation.error as Error).message}
)}
); }