LinkSpoolModal.tsx 5.8 KB

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