LinkSpoolModal.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { useState, useMemo } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import { X, Loader2, Search, Link } from 'lucide-react';
  5. import { api } from '../api/client';
  6. import type { UnlinkedSpool } from '../api/client';
  7. import { Button } from './Button';
  8. import { useToast } from '../contexts/ToastContext';
  9. interface LinkSpoolModalProps {
  10. isOpen: boolean;
  11. onClose: () => void;
  12. tagUid: string;
  13. trayUuid: string;
  14. printerId: number;
  15. amsId: number;
  16. trayId: number;
  17. }
  18. export function LinkSpoolModal({ isOpen, onClose, tagUid, trayUuid, printerId, amsId, trayId }: LinkSpoolModalProps) {
  19. const { t } = useTranslation();
  20. const queryClient = useQueryClient();
  21. const { showToast } = useToast();
  22. const [search, setSearch] = useState('');
  23. const spoolTag = trayUuid || tagUid;
  24. const { data: spools, isLoading } = useQuery({
  25. queryKey: ['unlinked-spools'],
  26. queryFn: api.getUnlinkedSpools,
  27. enabled: isOpen,
  28. });
  29. // Filter Spoolman unlinked spools matching search
  30. const filteredSpools = useMemo(() => {
  31. if (!spools) return [];
  32. return spools.filter((s: UnlinkedSpool) => {
  33. if (!search) return true;
  34. const q = search.toLowerCase();
  35. return (
  36. (s.filament_name && s.filament_name.toLowerCase().includes(q)) ||
  37. (s.filament_vendor && s.filament_vendor.toLowerCase().includes(q)) ||
  38. (s.filament_material && s.filament_material.toLowerCase().includes(q)) ||
  39. String(s.id).includes(q)
  40. );
  41. });
  42. }, [spools, search]);
  43. const linkMutation = useMutation({
  44. mutationFn: (spoolId: number) =>
  45. api.linkSpool(spoolId, {
  46. spoolTag: spoolTag!,
  47. printerId,
  48. amsId,
  49. trayId,
  50. }),
  51. onSuccess: () => {
  52. queryClient.invalidateQueries({ queryKey: ['unlinked-spools'] });
  53. queryClient.invalidateQueries({ queryKey: ['linked-spools'] });
  54. queryClient.invalidateQueries({ queryKey: ['spoolman-slot-assignments'] });
  55. showToast(t('spoolman.linkSuccess'), 'success');
  56. onClose();
  57. },
  58. onError: (err: Error) => {
  59. showToast(err.message || t('spoolman.linkFailed'), 'error');
  60. },
  61. });
  62. if (!isOpen) return null;
  63. return (
  64. <div className="fixed inset-0 z-50 flex items-center justify-center">
  65. <div className="absolute inset-0 bg-black/60 backdrop-blur-sm" onClick={onClose} />
  66. <div className="relative bg-bambu-dark-secondary rounded-xl shadow-xl w-full max-w-md mx-4 max-h-[80vh] flex flex-col border border-bambu-dark-tertiary">
  67. {/* Header */}
  68. <div className="flex items-center justify-between p-4 border-b border-white/10">
  69. <div>
  70. <h3 className="text-lg font-semibold text-white flex items-center gap-2">
  71. <Link className="w-5 h-5 text-bambu-green" />
  72. {t('spoolman.selectSpool')}
  73. </h3>
  74. <p className="text-xs text-bambu-gray mt-1">
  75. AMS {amsId} T{trayId} &middot; Printer #{printerId}
  76. </p>
  77. </div>
  78. <button onClick={onClose} className="p-1 text-bambu-gray hover:text-white rounded transition-colors">
  79. <X className="w-5 h-5" />
  80. </button>
  81. </div>
  82. {/* Search */}
  83. <div className="p-4 border-b border-white/10">
  84. <div className="relative">
  85. <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray" />
  86. <input
  87. type="text"
  88. value={search}
  89. onChange={(e) => setSearch(e.target.value)}
  90. placeholder={t('inventory.searchSpools')}
  91. 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"
  92. />
  93. </div>
  94. {(trayUuid || tagUid) && (
  95. <p className="text-xs text-bambu-gray mt-2 font-mono truncate" title={trayUuid || tagUid}>
  96. Tag: {trayUuid || tagUid}
  97. </p>
  98. )}
  99. </div>
  100. {/* Spool List */}
  101. <div className="flex-1 overflow-y-auto p-2 min-h-0">
  102. {isLoading ? (
  103. <div className="flex justify-center py-8">
  104. <Loader2 className="w-6 h-6 animate-spin text-bambu-green" />
  105. </div>
  106. ) : filteredSpools.length === 0 ? (
  107. <p className="text-center text-bambu-gray py-8 text-sm">
  108. {t('inventory.noSpoolsMatch')}
  109. </p>
  110. ) : (
  111. filteredSpools.map((spool: UnlinkedSpool) => (
  112. <button
  113. key={spool.id}
  114. onClick={() => linkMutation.mutate(spool.id)}
  115. disabled={linkMutation.isPending || !spoolTag}
  116. className="w-full flex items-center gap-3 p-3 rounded-lg hover:bg-white/5 transition-colors text-left"
  117. >
  118. <span
  119. className="w-6 h-6 rounded-full border border-black/20 flex-shrink-0"
  120. style={{ backgroundColor: spool.filament_color_hex ? `#${spool.filament_color_hex}` : '#808080' }}
  121. />
  122. <div className="flex-1 min-w-0">
  123. <div className="text-sm text-white font-medium truncate">
  124. {spool.filament_name || t('spoolman.spoolId')}
  125. </div>
  126. <div className="text-xs text-bambu-gray truncate">
  127. {spool.filament_vendor ? `${spool.filament_vendor} · ` : ''}
  128. {spool.filament_material || 'Unknown'} &middot; #{spool.id}
  129. </div>
  130. </div>
  131. <span className="text-xs text-bambu-gray">
  132. {spool.remaining_weight != null ? `${Math.round(spool.remaining_weight)}g` : '—'}
  133. </span>
  134. </button>
  135. ))
  136. )}
  137. </div>
  138. {/* Footer */}
  139. <div className="p-4 border-t border-white/10 flex justify-end">
  140. <Button variant="ghost" onClick={onClose}>
  141. {t('inventory.cancel') || 'Cancel'}
  142. </Button>
  143. </div>
  144. </div>
  145. </div>
  146. );
  147. }