LabelTemplatePickerModal.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import { useEffect, useMemo, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { X, Loader2, Printer, CheckSquare, Square, Search } from 'lucide-react';
  4. import { api, type SpoolLabelTemplate, type InventorySpool } from '../api/client';
  5. import { Button } from './Button';
  6. import { useToast } from '../contexts/ToastContext';
  7. /** Subset of InventorySpool the modal needs for checkbox rendering. */
  8. type SpoolForLabel = Pick<
  9. InventorySpool,
  10. 'id' | 'material' | 'subtype' | 'brand' | 'color_name' | 'rgba'
  11. >;
  12. interface LabelTemplatePickerModalProps {
  13. isOpen: boolean;
  14. onClose: () => void;
  15. /** All spools the modal can choose from. Typically the page's current
  16. * filter result so the modal stays consistent with what the user sees. */
  17. availableSpools: SpoolForLabel[];
  18. /** IDs to pre-check when the modal opens. Per-card icon passes a single ID;
  19. * the bulk header button passes every visible ID so the user lands in
  20. * "all checked" and refines downward. */
  21. initialSelectedIds: number[];
  22. spoolmanMode: boolean;
  23. }
  24. interface TemplateOption {
  25. value: SpoolLabelTemplate;
  26. i18nKey: string;
  27. fallbackLabel: string;
  28. fallbackHint: string;
  29. }
  30. const TEMPLATE_OPTIONS: TemplateOption[] = [
  31. {
  32. value: 'ams_holder_74x33',
  33. i18nKey: 'amsHolderSmall',
  34. fallbackLabel: 'AMS holder — small (74 × 33 mm)',
  35. fallbackHint: 'Single label per page; matches the printable label from MakerWorld model 752566 (AMS Filament Label Holder).',
  36. },
  37. {
  38. value: 'ams_holder_75x55',
  39. i18nKey: 'amsHolderLarge',
  40. fallbackLabel: 'AMS holder — large (75 × 55 mm)',
  41. fallbackHint: 'Single label per page; fits the cardstock-insert variant of the AMS Filament Label Holder. Roomy enough for swatch, brand, material, ID, and QR code.',
  42. },
  43. {
  44. value: 'box_40x30',
  45. i18nKey: 'box40x30',
  46. fallbackLabel: 'Box label (40 × 30 mm)',
  47. fallbackHint: 'Single label per page; common DK/Brother roll size, good for filament-bag and storage-bin labels.',
  48. },
  49. {
  50. value: 'box_62x29',
  51. i18nKey: 'box',
  52. fallbackLabel: 'Box label (62 × 29 mm)',
  53. fallbackHint: 'Single label per page; sized for Brother PT/QL and Dymo small labels.',
  54. },
  55. {
  56. value: 'avery_l7160',
  57. i18nKey: 'averyL7160',
  58. fallbackLabel: 'Avery L7160 — A4 sheet (38.1 × 63.5 mm × 21)',
  59. fallbackHint: 'EU sheet stock; 21 labels per A4 page.',
  60. },
  61. {
  62. value: 'avery_5160',
  63. i18nKey: 'avery5160',
  64. fallbackLabel: 'Avery 5160 — US Letter sheet (25.4 × 66.7 mm × 30)',
  65. fallbackHint: 'US sheet stock; 30 labels per Letter page.',
  66. },
  67. ];
  68. function openBlobInNewTab(blob: Blob): void {
  69. const url = window.URL.createObjectURL(blob);
  70. const win = window.open(url, '_blank', 'noopener,noreferrer');
  71. if (!win) {
  72. const a = document.createElement('a');
  73. a.href = url;
  74. a.download = 'bambuddy-labels.pdf';
  75. document.body.appendChild(a);
  76. a.click();
  77. document.body.removeChild(a);
  78. }
  79. setTimeout(() => window.URL.revokeObjectURL(url), 60_000);
  80. }
  81. function swatchStyle(rgba: string | null | undefined): React.CSSProperties {
  82. if (!rgba) return { backgroundColor: '#808080' };
  83. const cleaned = rgba.replace(/^#/, '').slice(0, 6);
  84. return cleaned.length === 6 ? { backgroundColor: `#${cleaned}` } : { backgroundColor: '#808080' };
  85. }
  86. function spoolDisplayName(s: SpoolForLabel): string {
  87. const head = s.color_name ?? `${s.material}${s.subtype ? ` ${s.subtype}` : ''}`;
  88. const brand = s.brand ? ` · ${s.brand}` : '';
  89. return `${head}${brand}`;
  90. }
  91. /** Build a lowercased haystack that the search input matches against. */
  92. function searchableText(s: SpoolForLabel): string {
  93. return [s.color_name, s.material, s.subtype, s.brand, `#${s.id}`]
  94. .filter(Boolean)
  95. .join(' ')
  96. .toLowerCase();
  97. }
  98. type SortMode = 'id' | 'color';
  99. /** Sort key for the "by colour" mode (#1410).
  100. *
  101. * Returns a 2-tuple so JS array compare does the right thing without us having
  102. * to spell out a comparator: ``[bucket, position]``. Chromatic colours
  103. * (saturation above the threshold) go in bucket 0 ordered by HSL hue, so the
  104. * sheet reads as a continuous rainbow. Achromatic colours (white / grey /
  105. * black, plus missing/invalid rgba) go in bucket 1 ordered by lightness so the
  106. * neutrals trail at the end of the rainbow going dark → light. Multi-colour
  107. * spools sort on their primary ``rgba``; their ``extra_colors`` stripe is
  108. * still rendered on the label itself but doesn't drive the sort.
  109. */
  110. function colorSortKey(rgba: string | null | undefined): [number, number] {
  111. if (!rgba) return [1, 0]; // unknown colour — bucket with the neutrals at black
  112. const cleaned = rgba.replace(/^#/, '').slice(0, 6);
  113. if (cleaned.length !== 6) return [1, 0];
  114. const r = parseInt(cleaned.slice(0, 2), 16);
  115. const g = parseInt(cleaned.slice(2, 4), 16);
  116. const b = parseInt(cleaned.slice(4, 6), 16);
  117. if ([r, g, b].some(Number.isNaN)) return [1, 0];
  118. const rn = r / 255;
  119. const gn = g / 255;
  120. const bn = b / 255;
  121. const max = Math.max(rn, gn, bn);
  122. const min = Math.min(rn, gn, bn);
  123. const l = (max + min) / 2;
  124. const delta = max - min;
  125. // Saturation in the HSL definition. Achromatic cutoff at 0.1 is generous —
  126. // matches what feels "grey enough" to a user picking colours, without
  127. // sending dark muted colours like deep navy into the neutrals bucket.
  128. const s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
  129. if (s < 0.1) return [1, l]; // neutrals: ordered black → white
  130. let h = 0;
  131. if (max === rn) h = ((gn - bn) / delta) % 6;
  132. else if (max === gn) h = (bn - rn) / delta + 2;
  133. else h = (rn - gn) / delta + 4;
  134. h = h * 60;
  135. if (h < 0) h += 360;
  136. return [0, h]; // chromatic: ordered by hue 0..360
  137. }
  138. export function LabelTemplatePickerModal({
  139. isOpen,
  140. onClose,
  141. availableSpools,
  142. initialSelectedIds,
  143. spoolmanMode,
  144. }: LabelTemplatePickerModalProps) {
  145. const { t } = useTranslation();
  146. const { showToast } = useToast();
  147. const [pending, setPending] = useState<SpoolLabelTemplate | null>(null);
  148. const [selectedIds, setSelectedIds] = useState<Set<number>>(new Set());
  149. const [search, setSearch] = useState('');
  150. const [materialFilter, setMaterialFilter] = useState<string>('');
  151. const [sortMode, setSortMode] = useState<SortMode>('id');
  152. // Sync from caller and reset transient state on open. Intentionally not
  153. // reactive to props while open — once the user starts editing we don't want
  154. // a parent re-render to clobber their selection / filter / search.
  155. useEffect(() => {
  156. if (isOpen) {
  157. const allowed = new Set(availableSpools.map((s) => s.id));
  158. setSelectedIds(new Set(initialSelectedIds.filter((id) => allowed.has(id))));
  159. setSearch('');
  160. setMaterialFilter('');
  161. setSortMode('id');
  162. setPending(null);
  163. }
  164. // eslint-disable-next-line react-hooks/exhaustive-deps
  165. }, [isOpen]);
  166. const sortedSpools = useMemo(() => {
  167. const copy = [...availableSpools];
  168. if (sortMode === 'color') {
  169. copy.sort((a, b) => {
  170. const ka = colorSortKey(a.rgba);
  171. const kb = colorSortKey(b.rgba);
  172. if (ka[0] !== kb[0]) return ka[0] - kb[0];
  173. if (ka[1] !== kb[1]) return ka[1] - kb[1];
  174. // Stable tiebreaker on ID so identical colours print in a deterministic
  175. // order across renders.
  176. return a.id - b.id;
  177. });
  178. return copy;
  179. }
  180. copy.sort((a, b) => a.id - b.id);
  181. return copy;
  182. }, [availableSpools, sortMode]);
  183. // Material chips are derived from the *full* available set so they stay
  184. // stable when search/material filter narrows the visible list.
  185. const materials = useMemo(() => {
  186. const set = new Set<string>();
  187. for (const s of sortedSpools) {
  188. if (s.material) set.add(s.material.toUpperCase());
  189. }
  190. return [...set].sort();
  191. }, [sortedSpools]);
  192. const visibleSpools = useMemo(() => {
  193. const q = search.trim().toLowerCase();
  194. return sortedSpools.filter((s) => {
  195. if (materialFilter && (s.material || '').toUpperCase() !== materialFilter) return false;
  196. if (q && !searchableText(s).includes(q)) return false;
  197. return true;
  198. });
  199. }, [sortedSpools, search, materialFilter]);
  200. const allVisibleChecked =
  201. visibleSpools.length > 0 && visibleSpools.every((s) => selectedIds.has(s.id));
  202. if (!isOpen) return null;
  203. const selectedCount = selectedIds.size;
  204. const noSelection = selectedCount === 0;
  205. function toggleOne(id: number) {
  206. setSelectedIds((prev) => {
  207. const next = new Set(prev);
  208. if (next.has(id)) next.delete(id);
  209. else next.add(id);
  210. return next;
  211. });
  212. }
  213. function selectAllVisible() {
  214. setSelectedIds((prev) => {
  215. const next = new Set(prev);
  216. for (const s of visibleSpools) next.add(s.id);
  217. return next;
  218. });
  219. }
  220. function deselectVisible() {
  221. setSelectedIds((prev) => {
  222. const next = new Set(prev);
  223. for (const s of visibleSpools) next.delete(s.id);
  224. return next;
  225. });
  226. }
  227. function clearAll() {
  228. setSelectedIds(new Set());
  229. }
  230. async function handlePick(template: SpoolLabelTemplate) {
  231. if (noSelection || pending) return;
  232. // Order matters: the backend (labels.py) prints labels in the same order
  233. // we send IDs. Use the sorted list so a "by colour" sort flows through to
  234. // the PDF instead of being clobbered by an ascending-ID re-sort.
  235. const ids = sortedSpools.filter((s) => selectedIds.has(s.id)).map((s) => s.id);
  236. setPending(template);
  237. try {
  238. const blob = spoolmanMode
  239. ? await api.printSpoolmanSpoolLabels({ spool_ids: ids, template })
  240. : await api.printSpoolLabels({ spool_ids: ids, template });
  241. openBlobInNewTab(blob);
  242. onClose();
  243. } catch (err) {
  244. const msg = err instanceof Error ? err.message : String(err);
  245. showToast(
  246. t('inventory.labels.error', 'Could not generate labels: {{msg}}', { msg }),
  247. 'error',
  248. );
  249. } finally {
  250. setPending(null);
  251. }
  252. }
  253. return (
  254. <div className="fixed inset-0 z-50 flex items-start sm:items-center justify-center p-4 overflow-y-auto">
  255. <div
  256. className="absolute inset-0 bg-black/60 backdrop-blur-sm"
  257. onClick={onClose}
  258. />
  259. <div className="relative w-full max-w-3xl bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] overflow-hidden flex flex-col my-auto">
  260. {/* Header */}
  261. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
  262. <div className="flex items-center gap-2">
  263. <Printer className="w-5 h-5 text-bambu-green" />
  264. <h2 className="text-lg font-semibold text-white">
  265. {t('inventory.labels.title', 'Print spool labels')}
  266. </h2>
  267. {selectedCount > 0 && (
  268. <span className="text-sm text-bambu-gray">
  269. ({t('inventory.labels.selectedCount', '{{count}} selected', { count: selectedCount })})
  270. </span>
  271. )}
  272. </div>
  273. <button
  274. onClick={onClose}
  275. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  276. aria-label={t('common.close', 'Close')}
  277. >
  278. <X className="w-5 h-5" />
  279. </button>
  280. </div>
  281. {/* Search + material chips */}
  282. <div className="p-4 space-y-2 border-b border-bambu-dark-tertiary">
  283. <div className="relative">
  284. <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  285. <input
  286. type="search"
  287. value={search}
  288. onChange={(e) => setSearch(e.target.value)}
  289. placeholder={t('inventory.labels.searchPlaceholder', 'Search name, brand, or #ID')}
  290. className="w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray focus:outline-none focus:border-bambu-green"
  291. />
  292. </div>
  293. {materials.length > 1 && (
  294. <div className="flex flex-wrap items-center gap-1.5">
  295. <span className="text-xs text-bambu-gray mr-1">
  296. {t('inventory.labels.filterByMaterial', 'Material:')}
  297. </span>
  298. <button
  299. type="button"
  300. onClick={() => setMaterialFilter('')}
  301. className={`px-2 py-0.5 text-xs rounded-full border transition ${
  302. materialFilter === ''
  303. ? 'bg-bambu-green text-bambu-dark border-bambu-green'
  304. : 'bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray'
  305. }`}
  306. >
  307. {t('inventory.labels.allMaterials', 'All')}
  308. </button>
  309. {materials.map((m) => (
  310. <button
  311. key={m}
  312. type="button"
  313. onClick={() => setMaterialFilter(m)}
  314. className={`px-2 py-0.5 text-xs rounded-full border transition ${
  315. materialFilter === m
  316. ? 'bg-bambu-green text-bambu-dark border-bambu-green'
  317. : 'bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray'
  318. }`}
  319. >
  320. {m}
  321. </button>
  322. ))}
  323. </div>
  324. )}
  325. <div className="flex flex-wrap items-center gap-1.5">
  326. <span className="text-xs text-bambu-gray mr-1">
  327. {t('inventory.labels.sortBy.label')}
  328. </span>
  329. <button
  330. type="button"
  331. onClick={() => setSortMode('id')}
  332. className={`px-2 py-0.5 text-xs rounded-full border transition ${
  333. sortMode === 'id'
  334. ? 'bg-bambu-green text-bambu-dark border-bambu-green'
  335. : 'bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray'
  336. }`}
  337. >
  338. {t('inventory.labels.sortBy.id')}
  339. </button>
  340. <button
  341. type="button"
  342. onClick={() => setSortMode('color')}
  343. className={`px-2 py-0.5 text-xs rounded-full border transition ${
  344. sortMode === 'color'
  345. ? 'bg-bambu-green text-bambu-dark border-bambu-green'
  346. : 'bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray'
  347. }`}
  348. >
  349. {t('inventory.labels.sortBy.color')}
  350. </button>
  351. </div>
  352. </div>
  353. {/* Action bar */}
  354. <div className="px-4 pt-3 pb-2 flex items-center justify-between gap-3 flex-wrap">
  355. <span className="text-sm text-bambu-gray">
  356. {t('inventory.labels.pickSpools', 'Pick which spools to print labels for:')}
  357. </span>
  358. <div className="flex items-center gap-3 text-xs">
  359. <button
  360. type="button"
  361. onClick={allVisibleChecked ? deselectVisible : selectAllVisible}
  362. disabled={visibleSpools.length === 0}
  363. className="text-bambu-green hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed"
  364. >
  365. {allVisibleChecked
  366. ? t('inventory.labels.deselectVisible', 'Deselect visible')
  367. : t('inventory.labels.selectVisible', 'Select all visible ({{count}})', {
  368. count: visibleSpools.length,
  369. })}
  370. </button>
  371. <button
  372. type="button"
  373. onClick={clearAll}
  374. disabled={selectedCount === 0}
  375. className="text-bambu-gray hover:text-white hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed"
  376. >
  377. {t('inventory.labels.clearAll', 'Clear all')}
  378. </button>
  379. </div>
  380. </div>
  381. {/* Spool list */}
  382. <div className="flex-1 overflow-y-auto px-2 pb-2 min-h-0">
  383. {visibleSpools.length === 0 ? (
  384. <div className="text-center text-sm text-bambu-gray py-6">
  385. {sortedSpools.length === 0
  386. ? t('inventory.labels.noSpoolsToShow', 'No spools to show. Adjust your filter and try again.')
  387. : t('inventory.labels.noMatches', 'No spools match the current search or filter.')}
  388. </div>
  389. ) : (
  390. <ul className="space-y-0.5">
  391. {visibleSpools.map((s) => {
  392. const checked = selectedIds.has(s.id);
  393. return (
  394. <li key={s.id}>
  395. <label className="flex items-center gap-3 px-2 py-1.5 rounded hover:bg-bambu-dark-tertiary/50 cursor-pointer">
  396. {checked ? (
  397. <CheckSquare className="w-4 h-4 text-bambu-green shrink-0" />
  398. ) : (
  399. <Square className="w-4 h-4 text-bambu-gray shrink-0" />
  400. )}
  401. <input
  402. type="checkbox"
  403. checked={checked}
  404. onChange={() => toggleOne(s.id)}
  405. className="sr-only"
  406. />
  407. <span
  408. className="w-4 h-4 rounded border border-black/20 shrink-0"
  409. style={swatchStyle(s.rgba)}
  410. />
  411. <span className="flex-1 min-w-0 truncate text-sm text-white">
  412. {spoolDisplayName(s)}
  413. </span>
  414. <span className="text-xs font-mono text-bambu-gray shrink-0">
  415. #{s.id}
  416. </span>
  417. </label>
  418. </li>
  419. );
  420. })}
  421. </ul>
  422. )}
  423. </div>
  424. {/* Templates — 2x2 grid on >= sm so all 4 plus the Cancel footer fit
  425. inside max-h-[90vh] even when browser chrome eats into the viewport
  426. (#1230). Stacked single column on mobile widths. */}
  427. <div className="px-3 pt-2 pb-2 grid grid-cols-1 sm:grid-cols-2 gap-2 border-t border-bambu-dark-tertiary">
  428. {TEMPLATE_OPTIONS.map((opt) => {
  429. const isPending = pending === opt.value;
  430. const label = t(`inventory.labels.templates.${opt.i18nKey}.label`, opt.fallbackLabel);
  431. const hint = t(`inventory.labels.templates.${opt.i18nKey}.hint`, opt.fallbackHint);
  432. return (
  433. <button
  434. key={opt.value}
  435. disabled={noSelection || pending !== null}
  436. onClick={() => handlePick(opt.value)}
  437. title={`${label} — ${hint}`}
  438. className="w-full text-left p-2.5 rounded-lg border border-bambu-dark-tertiary bg-bambu-dark hover:border-bambu-green hover:bg-bambu-green/10 disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:border-bambu-dark-tertiary disabled:hover:bg-bambu-dark transition flex items-center gap-3"
  439. >
  440. <div className="flex-1 min-w-0">
  441. <div className="font-medium text-white text-sm truncate">{label}</div>
  442. <div className="text-xs text-bambu-gray mt-0.5 truncate">{hint}</div>
  443. </div>
  444. {isPending && <Loader2 className="w-4 h-4 animate-spin text-bambu-green shrink-0" />}
  445. </button>
  446. );
  447. })}
  448. </div>
  449. <div className="flex justify-end gap-2 px-5 py-2 border-t border-bambu-dark-tertiary">
  450. <Button variant="secondary" onClick={onClose} disabled={pending !== null}>
  451. {t('common.cancel', 'Cancel')}
  452. </Button>
  453. </div>
  454. </div>
  455. </div>
  456. );
  457. }