import { useState, useMemo } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { useTranslation } from 'react-i18next'; import { X, Loader2, Search, Link } from 'lucide-react'; import { api } from '../api/client'; import type { UnlinkedSpool } from '../api/client'; import { Button } from './Button'; import { useToast } from '../contexts/ToastContext'; interface LinkSpoolModalProps { isOpen: boolean; onClose: () => void; tagUid: string; trayUuid: string; printerId: number; amsId: number; trayId: number; } export function LinkSpoolModal({ isOpen, onClose, tagUid, trayUuid, printerId, amsId, trayId }: LinkSpoolModalProps) { const { t } = useTranslation(); const queryClient = useQueryClient(); const { showToast } = useToast(); const [search, setSearch] = useState(''); const spoolTag = trayUuid || tagUid; const { data: spools, isLoading } = useQuery({ queryKey: ['unlinked-spools'], queryFn: api.getUnlinkedSpools, enabled: isOpen, }); // Filter Spoolman unlinked spools matching search const filteredSpools = useMemo(() => { if (!spools) return []; return spools.filter((s: UnlinkedSpool) => { if (!search) return true; const q = search.toLowerCase(); return ( (s.filament_name && s.filament_name.toLowerCase().includes(q)) || (s.filament_vendor && s.filament_vendor.toLowerCase().includes(q)) || (s.filament_material && s.filament_material.toLowerCase().includes(q)) || String(s.id).includes(q) ); }); }, [spools, search]); const linkMutation = useMutation({ mutationFn: (spoolId: number) => api.linkSpool(spoolId, { spoolTag: spoolTag!, printerId, amsId, trayId, }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['unlinked-spools'] }); queryClient.invalidateQueries({ queryKey: ['linked-spools'] }); queryClient.invalidateQueries({ queryKey: ['spoolman-slot-assignments'] }); showToast(t('spoolman.linkSuccess'), 'success'); onClose(); }, onError: (err: Error) => { showToast(err.message || t('spoolman.linkFailed'), 'error'); }, }); if (!isOpen) return null; return (
{/* Header */}

{t('spoolman.selectSpool')}

AMS {amsId} T{trayId} · Printer #{printerId}

{/* Search */}
setSearch(e.target.value)} placeholder={t('inventory.searchSpools')} className="w-full pl-9 pr-3 py-2 bg-bambu-dark rounded-lg border border-white/10 text-white text-sm placeholder:text-bambu-gray focus:outline-none focus:border-bambu-green" />
{(trayUuid || tagUid) && (

Tag: {trayUuid || tagUid}

)}
{/* Spool List */}
{isLoading ? (
) : filteredSpools.length === 0 ? (

{t('inventory.noSpoolsMatch')}

) : ( filteredSpools.map((spool: UnlinkedSpool) => ( )) )}
{/* Footer */}
); }