LinkSpoolModal.tsx 6.1 KB

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