QueuePage.tsx 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. import { useState, useMemo, useEffect, useCallback } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useQuery, useQueries, useMutation, useQueryClient } from '@tanstack/react-query';
  4. import { Link } from 'react-router-dom';
  5. import {
  6. DndContext,
  7. closestCenter,
  8. KeyboardSensor,
  9. PointerSensor,
  10. useSensor,
  11. useSensors,
  12. } from '@dnd-kit/core';
  13. import type { DragEndEvent } from '@dnd-kit/core';
  14. import {
  15. arrayMove,
  16. SortableContext,
  17. sortableKeyboardCoordinates,
  18. useSortable,
  19. verticalListSortingStrategy,
  20. } from '@dnd-kit/sortable';
  21. import { CSS } from '@dnd-kit/utilities';
  22. import {
  23. Clock,
  24. Trash2,
  25. Play,
  26. X,
  27. CheckCircle,
  28. XCircle,
  29. AlertCircle,
  30. Calendar,
  31. Printer,
  32. GripVertical,
  33. SkipForward,
  34. ExternalLink,
  35. Power,
  36. StopCircle,
  37. Pencil,
  38. RefreshCw,
  39. Timer,
  40. ListOrdered,
  41. Layers,
  42. ArrowUp,
  43. ArrowDown,
  44. Hand,
  45. Check,
  46. CheckSquare,
  47. Square,
  48. User,
  49. Pause,
  50. } from 'lucide-react';
  51. import { api } from '../api/client';
  52. import { parseUTCDate, formatDateTime, type TimeFormat } from '../utils/date';
  53. import type { PrintQueueItem, PrintQueueBulkUpdate, Permission } from '../api/client';
  54. import { Card, CardContent } from '../components/Card';
  55. import { Button } from '../components/Button';
  56. import { ConfirmModal } from '../components/ConfirmModal';
  57. import { PrintModal } from '../components/PrintModal';
  58. import { useToast } from '../contexts/ToastContext';
  59. import { useAuth } from '../contexts/AuthContext';
  60. function formatDuration(seconds: number | null | undefined): string {
  61. if (!seconds) return '--';
  62. const hours = Math.floor(seconds / 3600);
  63. const minutes = Math.floor((seconds % 3600) / 60);
  64. if (hours > 0) return `${hours}h ${minutes}m`;
  65. return `${minutes}m`;
  66. }
  67. function formatRelativeTime(dateString: string | null, timeFormat: TimeFormat = 'system', t?: (key: string, options?: Record<string, unknown>) => string): string {
  68. if (!dateString) return t?.('queue.time.asap') ?? 'ASAP';
  69. const date = parseUTCDate(dateString);
  70. if (!date) return t?.('queue.time.asap') ?? 'ASAP';
  71. const now = new Date();
  72. const diff = date.getTime() - now.getTime();
  73. if (diff < -60000) return t?.('queue.time.overdue') ?? 'Overdue';
  74. if (diff < 0) return t?.('queue.time.now') ?? 'Now';
  75. if (diff < 60000) return t?.('queue.time.lessThanMinute') ?? 'In less than a minute';
  76. if (diff < 3600000) return t?.('queue.time.inMinutes', { count: Math.round(diff / 60000) }) ?? `In ${Math.round(diff / 60000)} min`;
  77. if (diff < 86400000) return t?.('queue.time.inHours', { count: Math.round(diff / 3600000) }) ?? `In ${Math.round(diff / 3600000)} hours`;
  78. return formatDateTime(dateString, timeFormat);
  79. }
  80. function StatusBadge({ status, waitingReason, printerState, t }: { status: PrintQueueItem['status']; waitingReason?: string | null; printerState?: string | null; t: (key: string) => string }) {
  81. // Special case: pending with waiting_reason shows as "Waiting"
  82. if (status === 'pending' && waitingReason) {
  83. return (
  84. <span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium border text-purple-400 bg-purple-400/10 border-purple-400/20">
  85. <Clock className="w-3.5 h-3.5" />
  86. {t('queue.status.waiting')}
  87. </span>
  88. );
  89. }
  90. // Special case: printing but printer is paused
  91. if (status === 'printing' && (printerState === 'PAUSE' || printerState === 'PAUSED')) {
  92. return (
  93. <span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium border text-yellow-400 bg-yellow-400/10 border-yellow-400/20">
  94. <Pause className="w-3.5 h-3.5" />
  95. {t('queue.status.paused')}
  96. </span>
  97. );
  98. }
  99. const config = {
  100. pending: { icon: Clock, color: 'text-status-warning bg-status-warning/10 border-status-warning/20', label: t('queue.status.pending') },
  101. printing: { icon: Play, color: 'text-blue-400 bg-blue-400/10 border-blue-400/20', label: t('queue.status.printing') },
  102. completed: { icon: CheckCircle, color: 'text-status-ok bg-status-ok/10 border-status-ok/20', label: t('queue.status.completed') },
  103. failed: { icon: XCircle, color: 'text-status-error bg-status-error/10 border-status-error/20', label: t('queue.status.failed') },
  104. skipped: { icon: SkipForward, color: 'text-orange-400 bg-orange-400/10 border-orange-400/20', label: t('queue.status.skipped') },
  105. cancelled: { icon: X, color: 'text-gray-400 bg-gray-400/10 border-gray-400/20', label: t('queue.status.cancelled') },
  106. };
  107. const { icon: Icon, color, label } = config[status];
  108. return (
  109. <span className={`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium border ${color}`}>
  110. <Icon className="w-3.5 h-3.5" />
  111. {label}
  112. </span>
  113. );
  114. }
  115. // Bulk edit modal for multiple queue items
  116. function BulkEditModal({
  117. selectedCount,
  118. printers,
  119. onSave,
  120. onClose,
  121. isSaving,
  122. t,
  123. }: {
  124. selectedCount: number;
  125. printers: { id: number; name: string }[];
  126. onSave: (data: Partial<PrintQueueBulkUpdate>) => void;
  127. onClose: () => void;
  128. isSaving: boolean;
  129. t: (key: string, options?: Record<string, unknown>) => string;
  130. }) {
  131. const [printerId, setPrinterId] = useState<number | null | 'unchanged'>('unchanged');
  132. const [manualStart, setManualStart] = useState<boolean | 'unchanged'>('unchanged');
  133. const [autoOffAfter, setAutoOffAfter] = useState<boolean | 'unchanged'>('unchanged');
  134. const [requirePreviousSuccess, setRequirePreviousSuccess] = useState<boolean | 'unchanged'>('unchanged');
  135. const [bedLevelling, setBedLevelling] = useState<boolean | 'unchanged'>('unchanged');
  136. const [flowCali, setFlowCali] = useState<boolean | 'unchanged'>('unchanged');
  137. const [vibrationCali, setVibrationCali] = useState<boolean | 'unchanged'>('unchanged');
  138. const [layerInspect, setLayerInspect] = useState<boolean | 'unchanged'>('unchanged');
  139. const [timelapse, setTimelapse] = useState<boolean | 'unchanged'>('unchanged');
  140. const [useAms, setUseAms] = useState<boolean | 'unchanged'>('unchanged');
  141. const handleSave = () => {
  142. const data: Partial<PrintQueueBulkUpdate> = {};
  143. if (printerId !== 'unchanged') data.printer_id = printerId;
  144. if (manualStart !== 'unchanged') data.manual_start = manualStart;
  145. if (autoOffAfter !== 'unchanged') data.auto_off_after = autoOffAfter;
  146. if (requirePreviousSuccess !== 'unchanged') data.require_previous_success = requirePreviousSuccess;
  147. if (bedLevelling !== 'unchanged') data.bed_levelling = bedLevelling;
  148. if (flowCali !== 'unchanged') data.flow_cali = flowCali;
  149. if (vibrationCali !== 'unchanged') data.vibration_cali = vibrationCali;
  150. if (layerInspect !== 'unchanged') data.layer_inspect = layerInspect;
  151. if (timelapse !== 'unchanged') data.timelapse = timelapse;
  152. if (useAms !== 'unchanged') data.use_ams = useAms;
  153. onSave(data);
  154. };
  155. const hasChanges = printerId !== 'unchanged' || manualStart !== 'unchanged' || autoOffAfter !== 'unchanged' ||
  156. requirePreviousSuccess !== 'unchanged' || bedLevelling !== 'unchanged' || flowCali !== 'unchanged' ||
  157. vibrationCali !== 'unchanged' || layerInspect !== 'unchanged' || timelapse !== 'unchanged' || useAms !== 'unchanged';
  158. return (
  159. <div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/50 backdrop-blur-sm">
  160. <div className="bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-lg max-h-[90vh] overflow-y-auto">
  161. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
  162. <h2 className="text-lg font-semibold text-white">
  163. {t('queue.bulkEdit.title', { count: selectedCount })}
  164. </h2>
  165. <button onClick={onClose} className="p-1 hover:bg-bambu-dark rounded">
  166. <X className="w-5 h-5 text-bambu-gray" />
  167. </button>
  168. </div>
  169. <div className="p-4 space-y-4">
  170. <p className="text-sm text-bambu-gray">
  171. {t('queue.bulkEdit.description')}
  172. </p>
  173. {/* Printer Assignment */}
  174. <div>
  175. <label className="block text-sm font-medium text-white mb-2">{t('queue.bulkEdit.printer')}</label>
  176. <select
  177. value={printerId === null ? 'null' : printerId === 'unchanged' ? 'unchanged' : String(printerId)}
  178. onChange={(e) => {
  179. const val = e.target.value;
  180. if (val === 'unchanged') setPrinterId('unchanged');
  181. else if (val === 'null') setPrinterId(null);
  182. else setPrinterId(Number(val));
  183. }}
  184. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  185. >
  186. <option value="unchanged">{t('queue.bulkEdit.noChange')}</option>
  187. <option value="null">{t('queue.filter.unassigned')}</option>
  188. {printers.map(p => (
  189. <option key={p.id} value={p.id}>{p.name}</option>
  190. ))}
  191. </select>
  192. </div>
  193. {/* Queue Options */}
  194. <div>
  195. <label className="block text-sm font-medium text-white mb-2">{t('queue.bulkEdit.queueOptions')}</label>
  196. <div className="space-y-2">
  197. <TriStateToggle label={t('queue.bulkEdit.staged')} value={manualStart} onChange={setManualStart} t={t} />
  198. <TriStateToggle label={t('queue.bulkEdit.autoPowerOff')} value={autoOffAfter} onChange={setAutoOffAfter} t={t} />
  199. <TriStateToggle label={t('queue.bulkEdit.requirePrevious')} value={requirePreviousSuccess} onChange={setRequirePreviousSuccess} t={t} />
  200. </div>
  201. </div>
  202. {/* Print Options */}
  203. <div>
  204. <label className="block text-sm font-medium text-white mb-2">{t('queue.bulkEdit.printOptions')}</label>
  205. <div className="space-y-2">
  206. <TriStateToggle label={t('queue.bulkEdit.bedLevelling')} value={bedLevelling} onChange={setBedLevelling} t={t} />
  207. <TriStateToggle label={t('queue.bulkEdit.flowCalibration')} value={flowCali} onChange={setFlowCali} t={t} />
  208. <TriStateToggle label={t('queue.bulkEdit.vibrationCalibration')} value={vibrationCali} onChange={setVibrationCali} t={t} />
  209. <TriStateToggle label={t('queue.bulkEdit.layerInspection')} value={layerInspect} onChange={setLayerInspect} t={t} />
  210. <TriStateToggle label={t('queue.bulkEdit.timelapse')} value={timelapse} onChange={setTimelapse} t={t} />
  211. <TriStateToggle label={t('queue.bulkEdit.useAms')} value={useAms} onChange={setUseAms} t={t} />
  212. </div>
  213. </div>
  214. </div>
  215. <div className="flex justify-end gap-3 p-4 border-t border-bambu-dark-tertiary">
  216. <Button variant="secondary" onClick={onClose}>{t('common.cancel')}</Button>
  217. <Button
  218. onClick={handleSave}
  219. disabled={!hasChanges || isSaving}
  220. >
  221. {isSaving ? t('common.saving') : t('queue.bulkEdit.applyChanges')}
  222. </Button>
  223. </div>
  224. </div>
  225. </div>
  226. );
  227. }
  228. // Tri-state toggle for bulk edit (unchanged / on / off)
  229. function TriStateToggle({
  230. label,
  231. value,
  232. onChange,
  233. t,
  234. }: {
  235. label: string;
  236. value: boolean | 'unchanged';
  237. onChange: (val: boolean | 'unchanged') => void;
  238. t: (key: string) => string;
  239. }) {
  240. return (
  241. <div className="flex items-center justify-between py-1">
  242. <span className="text-sm text-bambu-gray">{label}</span>
  243. <div className="flex items-center gap-1 bg-bambu-dark rounded-lg p-0.5">
  244. <button
  245. onClick={() => onChange('unchanged')}
  246. className={`px-2 py-1 text-xs rounded ${value === 'unchanged' ? 'bg-bambu-dark-tertiary text-white' : 'text-bambu-gray hover:text-white'}`}
  247. >
  248. </button>
  249. <button
  250. onClick={() => onChange(false)}
  251. className={`px-2 py-1 text-xs rounded ${value === false ? 'bg-red-500/20 text-red-400' : 'text-bambu-gray hover:text-white'}`}
  252. >
  253. {t('common.off')}
  254. </button>
  255. <button
  256. onClick={() => onChange(true)}
  257. className={`px-2 py-1 text-xs rounded ${value === true ? 'bg-bambu-green/20 text-bambu-green' : 'text-bambu-gray hover:text-white'}`}
  258. >
  259. {t('common.on')}
  260. </button>
  261. </div>
  262. </div>
  263. );
  264. }
  265. // Sortable queue item for drag and drop
  266. function SortableQueueItem({
  267. item,
  268. position,
  269. onEdit,
  270. onCancel,
  271. onRemove,
  272. onStop,
  273. onRequeue,
  274. onStart,
  275. timeFormat = 'system',
  276. isSelected = false,
  277. onToggleSelect,
  278. hasPermission,
  279. canModify,
  280. printerState,
  281. t,
  282. }: {
  283. item: PrintQueueItem;
  284. position?: number;
  285. onEdit: () => void;
  286. onCancel: () => void;
  287. onRemove: () => void;
  288. onStop: () => void;
  289. onRequeue: () => void;
  290. onStart: () => void;
  291. timeFormat?: TimeFormat;
  292. isSelected?: boolean;
  293. onToggleSelect?: () => void;
  294. hasPermission: (permission: Permission) => boolean;
  295. canModify: (resource: 'queue' | 'archives' | 'library', action: 'update' | 'delete' | 'reprint', createdById: number | null | undefined) => boolean;
  296. printerState?: string | null;
  297. t: (key: string, options?: Record<string, unknown>) => string;
  298. }) {
  299. const canReorder = hasPermission('queue:reorder');
  300. const {
  301. attributes,
  302. listeners,
  303. setNodeRef,
  304. transform,
  305. transition,
  306. isDragging,
  307. } = useSortable({ id: item.id, disabled: item.status !== 'pending' || !canReorder });
  308. const style = {
  309. transform: CSS.Transform.toString(transform),
  310. transition,
  311. };
  312. const isPrinting = item.status === 'printing';
  313. const isPending = item.status === 'pending';
  314. const isHistory = ['completed', 'failed', 'skipped', 'cancelled'].includes(item.status);
  315. return (
  316. <div
  317. ref={setNodeRef}
  318. style={style}
  319. className={`
  320. group relative bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary
  321. transition-all duration-200 hover:border-bambu-dark-tertiary/80
  322. ${isDragging ? 'opacity-50 scale-[1.02] shadow-xl z-50' : ''}
  323. ${isPrinting ? 'border-blue-500/30 bg-gradient-to-r from-blue-500/5 to-transparent' : ''}
  324. `}
  325. >
  326. <div className="flex items-center gap-4 p-4">
  327. {/* Selection checkbox for pending items */}
  328. {isPending && onToggleSelect && (
  329. <button
  330. onClick={(e) => {
  331. e.stopPropagation();
  332. onToggleSelect();
  333. }}
  334. className={`flex items-center justify-center w-6 h-6 rounded border transition-colors ${
  335. isSelected
  336. ? 'bg-bambu-green border-bambu-green text-white'
  337. : 'border-white/30 bg-black/30 hover:border-bambu-green/50'
  338. }`}
  339. >
  340. {isSelected && <Check className="w-4 h-4" />}
  341. </button>
  342. )}
  343. {/* Drag handle or position number */}
  344. {isPending ? (
  345. <div
  346. {...attributes}
  347. {...listeners}
  348. className="flex items-center justify-center w-10 h-10 md:w-8 md:h-8 rounded-lg bg-bambu-dark cursor-grab active:cursor-grabbing hover:bg-bambu-dark-tertiary transition-colors touch-manipulation"
  349. >
  350. <GripVertical className="w-6 h-6 md:w-4 md:h-4 text-bambu-gray" />
  351. </div>
  352. ) : position !== undefined ? (
  353. <div className="flex items-center justify-center w-8 h-8 rounded-lg bg-bambu-dark text-bambu-gray text-sm font-medium">
  354. #{position}
  355. </div>
  356. ) : (
  357. <div className="w-8" />
  358. )}
  359. {/* Thumbnail - use plate-specific thumbnail if plate_id is set */}
  360. <div className="w-14 h-14 flex-shrink-0 bg-bambu-dark rounded-lg overflow-hidden">
  361. {item.archive_thumbnail ? (
  362. <img
  363. src={
  364. item.plate_id != null
  365. ? api.getArchivePlateThumbnail(item.archive_id!, item.plate_id)
  366. : api.getArchiveThumbnail(item.archive_id!)
  367. }
  368. alt=""
  369. className="w-full h-full object-cover"
  370. />
  371. ) : item.library_file_thumbnail ? (
  372. <img
  373. src={
  374. item.plate_id != null
  375. ? api.getLibraryFilePlateThumbnail(item.library_file_id!, item.plate_id)
  376. : api.getLibraryFileThumbnailUrl(item.library_file_id!)
  377. }
  378. alt=""
  379. className="w-full h-full object-cover"
  380. />
  381. ) : (
  382. <div className="w-full h-full flex items-center justify-center text-bambu-gray">
  383. <Layers className="w-6 h-6" />
  384. </div>
  385. )}
  386. </div>
  387. {/* Info */}
  388. <div className="flex-1 min-w-0">
  389. <div className="flex items-center gap-2 mb-1">
  390. <p className="text-white font-medium truncate">
  391. {item.archive_name || item.library_file_name || `File #${item.archive_id || item.library_file_id}`}
  392. </p>
  393. {item.archive_id ? (
  394. <Link
  395. to={`/archives?highlight=${item.archive_id}`}
  396. className="text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0"
  397. title={t('queue.viewArchive')}
  398. >
  399. <ExternalLink className="w-3.5 h-3.5" />
  400. </Link>
  401. ) : item.library_file_id ? (
  402. <Link
  403. to={`/library?highlight=${item.library_file_id}`}
  404. className="text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0"
  405. title={t('queue.viewInFileManager')}
  406. >
  407. <ExternalLink className="w-3.5 h-3.5" />
  408. </Link>
  409. ) : null}
  410. </div>
  411. <div className="flex items-center gap-3 text-sm text-bambu-gray">
  412. <span className={`flex items-center gap-1.5 ${item.printer_id === null && !item.target_model ? 'text-orange-400' : ''} ${item.target_model ? 'text-blue-400' : ''}`}>
  413. <Printer className="w-3.5 h-3.5" />
  414. {item.target_model
  415. ? `${t('queue.filter.any')} ${item.target_model}${item.target_location ? ` @ ${item.target_location}` : ''}${item.required_filament_types?.length ? ` (${item.required_filament_types.join(', ')})` : ''}`
  416. : item.printer_id === null
  417. ? t('queue.filter.unassigned')
  418. : (item.printer_name || `${t('common.printer')} #${item.printer_id}`)}
  419. </span>
  420. {item.print_time_seconds && (
  421. <span className="flex items-center gap-1.5">
  422. <Timer className="w-3.5 h-3.5" />
  423. {formatDuration(item.print_time_seconds)}
  424. </span>
  425. )}
  426. {item.created_by_username && (
  427. <span className="flex items-center gap-1.5" title={t('queue.addedBy', { name: item.created_by_username })}>
  428. <User className="w-3.5 h-3.5" />
  429. {item.created_by_username}
  430. </span>
  431. )}
  432. {isPending && !item.manual_start && (
  433. <span className="flex items-center gap-1.5">
  434. <Clock className="w-3.5 h-3.5" />
  435. {formatRelativeTime(item.scheduled_time, timeFormat, t)}
  436. </span>
  437. )}
  438. </div>
  439. {/* Options badges */}
  440. <div className="flex items-center gap-2 mt-2">
  441. {item.manual_start && (
  442. <span className="text-xs px-2 py-0.5 bg-purple-500/10 text-purple-400 rounded-full border border-purple-500/20 flex items-center gap-1">
  443. <Hand className="w-3 h-3" />
  444. {t('queue.badges.staged')}
  445. </span>
  446. )}
  447. {item.require_previous_success && (
  448. <span className="text-xs px-2 py-0.5 bg-orange-500/10 text-orange-400 rounded-full border border-orange-500/20">
  449. {t('queue.badges.requiresPrevious')}
  450. </span>
  451. )}
  452. {item.auto_off_after && (
  453. <span className="text-xs px-2 py-0.5 bg-blue-500/10 text-blue-400 rounded-full border border-blue-500/20 flex items-center gap-1">
  454. <Power className="w-3 h-3" />
  455. {t('queue.badges.autoPowerOff')}
  456. </span>
  457. )}
  458. </div>
  459. {/* Progress bar for printing items - TODO: integrate with WebSocket */}
  460. {isPrinting && (
  461. <div className="mt-3">
  462. <div className="h-2 bg-bambu-dark rounded-full overflow-hidden">
  463. <div className="h-full bg-gradient-to-r from-blue-500 to-blue-400 animate-pulse w-full opacity-50" />
  464. </div>
  465. <p className="text-xs text-bambu-gray mt-1">{t('queue.printingInProgress')}</p>
  466. </div>
  467. )}
  468. {/* Waiting reason for model-based assignments */}
  469. {item.waiting_reason && item.status === 'pending' && (
  470. <p className="text-xs text-purple-400 mt-2 flex items-start gap-1">
  471. <AlertCircle className="w-3 h-3 mt-0.5 flex-shrink-0" />
  472. <span>{item.waiting_reason}</span>
  473. </p>
  474. )}
  475. {/* Error message */}
  476. {item.error_message && (
  477. <p className="text-xs text-red-400 mt-2 flex items-center gap-1">
  478. <AlertCircle className="w-3 h-3" />
  479. {item.error_message}
  480. </p>
  481. )}
  482. </div>
  483. {/* Status badge */}
  484. <StatusBadge status={item.status} waitingReason={item.waiting_reason} printerState={printerState} t={t} />
  485. {/* Actions */}
  486. <div className="flex items-center gap-1">
  487. {isPrinting && (
  488. <Button
  489. variant="ghost"
  490. size="sm"
  491. onClick={onStop}
  492. disabled={!hasPermission('printers:control')}
  493. title={!hasPermission('printers:control') ? t('queue.permissions.noStopPrint') : t('queue.actions.stopPrint')}
  494. className="text-red-400 hover:text-red-300 hover:bg-red-500/10"
  495. >
  496. <StopCircle className="w-4 h-4" />
  497. </Button>
  498. )}
  499. {isPending && (
  500. <>
  501. {item.manual_start && (
  502. <Button
  503. variant="ghost"
  504. size="sm"
  505. onClick={onStart}
  506. disabled={!hasPermission('printers:control')}
  507. title={!hasPermission('printers:control') ? t('queue.permissions.noStartPrint') : t('queue.actions.startPrint')}
  508. className="text-bambu-green hover:text-bambu-green-light hover:bg-bambu-green/10"
  509. >
  510. <Play className="w-4 h-4" />
  511. </Button>
  512. )}
  513. <Button
  514. variant="ghost"
  515. size="sm"
  516. onClick={onEdit}
  517. disabled={!canModify('queue', 'update', item.created_by_id)}
  518. title={!canModify('queue', 'update', item.created_by_id) ? t('queue.permissions.noEdit') : t('common.edit')}
  519. >
  520. <Pencil className="w-4 h-4" />
  521. </Button>
  522. <Button
  523. variant="ghost"
  524. size="sm"
  525. onClick={onCancel}
  526. disabled={!canModify('queue', 'delete', item.created_by_id)}
  527. title={!canModify('queue', 'delete', item.created_by_id) ? t('queue.permissions.noCancel') : t('common.cancel')}
  528. className="text-red-400 hover:text-red-300 hover:bg-red-500/10"
  529. >
  530. <X className="w-4 h-4" />
  531. </Button>
  532. </>
  533. )}
  534. {isHistory && (
  535. <>
  536. <Button
  537. variant="ghost"
  538. size="sm"
  539. onClick={onRequeue}
  540. disabled={!hasPermission('queue:create')}
  541. title={!hasPermission('queue:create') ? t('queue.permissions.noRequeue') : t('queue.actions.requeue')}
  542. className="text-bambu-green hover:text-bambu-green/80 hover:bg-bambu-green/10"
  543. >
  544. <RefreshCw className="w-4 h-4" />
  545. </Button>
  546. <Button
  547. variant="ghost"
  548. size="sm"
  549. onClick={onRemove}
  550. disabled={!canModify('queue', 'delete', item.created_by_id)}
  551. title={!canModify('queue', 'delete', item.created_by_id) ? t('queue.permissions.noRemove') : t('common.remove')}
  552. >
  553. <Trash2 className="w-4 h-4" />
  554. </Button>
  555. </>
  556. )}
  557. </div>
  558. </div>
  559. </div>
  560. );
  561. }
  562. export function QueuePage() {
  563. const { t } = useTranslation();
  564. const queryClient = useQueryClient();
  565. const { showToast } = useToast();
  566. const { hasPermission, hasAnyPermission, canModify } = useAuth();
  567. const [filterPrinter, setFilterPrinter] = useState<number | null>(null);
  568. const [filterStatus, setFilterStatus] = useState<string>('');
  569. const [filterLocation, setFilterLocation] = useState<string>('');
  570. const [showClearHistoryConfirm, setShowClearHistoryConfirm] = useState(false);
  571. const [editItem, setEditItem] = useState<PrintQueueItem | null>(null);
  572. const [requeueItem, setRequeueItem] = useState<PrintQueueItem | null>(null);
  573. const [confirmAction, setConfirmAction] = useState<{
  574. type: 'cancel' | 'remove' | 'stop';
  575. item: PrintQueueItem;
  576. } | null>(null);
  577. const [selectedItems, setSelectedItems] = useState<number[]>([]);
  578. const [showBulkEditModal, setShowBulkEditModal] = useState(false);
  579. const [historySortBy, setHistorySortBy] = useState<'date' | 'name' | 'printer'>(() => {
  580. const saved = localStorage.getItem('queue.historySortBy');
  581. return (saved as 'date' | 'name' | 'printer') || 'date';
  582. });
  583. const [historySortAsc, setHistorySortAsc] = useState(() => {
  584. const saved = localStorage.getItem('queue.historySortAsc');
  585. return saved !== null ? saved === 'true' : false;
  586. });
  587. const [pendingSortBy, setPendingSortBy] = useState<'position' | 'name' | 'printer' | 'time'>(() => {
  588. const saved = localStorage.getItem('queue.pendingSortBy');
  589. return (saved as 'position' | 'name' | 'printer' | 'time') || 'position';
  590. });
  591. const [pendingSortAsc, setPendingSortAsc] = useState(() => {
  592. const saved = localStorage.getItem('queue.pendingSortAsc');
  593. return saved !== null ? saved === 'true' : true;
  594. });
  595. // Persist sort settings to localStorage
  596. useEffect(() => {
  597. localStorage.setItem('queue.historySortBy', historySortBy);
  598. }, [historySortBy]);
  599. useEffect(() => {
  600. localStorage.setItem('queue.historySortAsc', String(historySortAsc));
  601. }, [historySortAsc]);
  602. useEffect(() => {
  603. localStorage.setItem('queue.pendingSortBy', pendingSortBy);
  604. }, [pendingSortBy]);
  605. useEffect(() => {
  606. localStorage.setItem('queue.pendingSortAsc', String(pendingSortAsc));
  607. }, [pendingSortAsc]);
  608. const sensors = useSensors(
  609. useSensor(PointerSensor, { activationConstraint: { distance: 8 } }),
  610. useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates })
  611. );
  612. const { data: settings } = useQuery({
  613. queryKey: ['settings'],
  614. queryFn: api.getSettings,
  615. });
  616. const timeFormat: TimeFormat = settings?.time_format || 'system';
  617. const { data: queue, isLoading } = useQuery({
  618. queryKey: ['queue', filterPrinter, filterStatus],
  619. queryFn: () => api.getQueue(filterPrinter || undefined, filterStatus || undefined),
  620. refetchInterval: 5000,
  621. });
  622. const { data: printers } = useQuery({
  623. queryKey: ['printers'],
  624. queryFn: () => api.getPrinters(),
  625. });
  626. const cancelMutation = useMutation({
  627. mutationFn: (id: number) => api.cancelQueueItem(id),
  628. onSuccess: () => {
  629. queryClient.invalidateQueries({ queryKey: ['queue'] });
  630. showToast(t('queue.toast.cancelled'));
  631. },
  632. onError: () => showToast(t('queue.toast.cancelFailed'), 'error'),
  633. });
  634. const removeMutation = useMutation({
  635. mutationFn: (id: number) => api.removeFromQueue(id),
  636. onSuccess: () => {
  637. queryClient.invalidateQueries({ queryKey: ['queue'] });
  638. showToast(t('queue.toast.removed'));
  639. },
  640. onError: () => showToast(t('queue.toast.removeFailed'), 'error'),
  641. });
  642. const stopMutation = useMutation({
  643. mutationFn: (id: number) => api.stopQueueItem(id),
  644. onSuccess: () => {
  645. queryClient.invalidateQueries({ queryKey: ['queue'] });
  646. showToast(t('queue.toast.stopped'));
  647. },
  648. onError: () => showToast(t('queue.toast.stopFailed'), 'error'),
  649. });
  650. const startMutation = useMutation({
  651. mutationFn: (id: number) => api.startQueueItem(id),
  652. onSuccess: () => {
  653. queryClient.invalidateQueries({ queryKey: ['queue'] });
  654. showToast(t('queue.toast.released'));
  655. },
  656. onError: () => showToast(t('queue.toast.startFailed'), 'error'),
  657. });
  658. const reorderMutation = useMutation({
  659. mutationFn: (items: { id: number; position: number }[]) => api.reorderQueue(items),
  660. onSuccess: () => {
  661. queryClient.invalidateQueries({ queryKey: ['queue'] });
  662. },
  663. onError: () => showToast(t('queue.toast.reorderFailed'), 'error'),
  664. });
  665. const clearHistoryMutation = useMutation({
  666. mutationFn: async () => {
  667. const historyItems = queue?.filter(i =>
  668. ['completed', 'failed', 'skipped', 'cancelled'].includes(i.status)
  669. ) || [];
  670. for (const item of historyItems) {
  671. await api.removeFromQueue(item.id);
  672. }
  673. return historyItems.length;
  674. },
  675. onSuccess: (count) => {
  676. queryClient.invalidateQueries({ queryKey: ['queue'] });
  677. showToast(t('queue.toast.historyCleared', { count }));
  678. },
  679. onError: () => showToast(t('queue.toast.clearHistoryFailed'), 'error'),
  680. });
  681. const bulkUpdateMutation = useMutation({
  682. mutationFn: (data: PrintQueueBulkUpdate) => api.bulkUpdateQueue(data),
  683. onSuccess: (result) => {
  684. queryClient.invalidateQueries({ queryKey: ['queue'] });
  685. setSelectedItems([]);
  686. setShowBulkEditModal(false);
  687. showToast(result.message);
  688. },
  689. onError: () => showToast(t('queue.toast.updateFailed'), 'error'),
  690. });
  691. const bulkCancelMutation = useMutation({
  692. mutationFn: async (ids: number[]) => {
  693. for (const id of ids) {
  694. await api.cancelQueueItem(id);
  695. }
  696. return ids.length;
  697. },
  698. onSuccess: (count) => {
  699. queryClient.invalidateQueries({ queryKey: ['queue'] });
  700. setSelectedItems([]);
  701. showToast(t('queue.toast.bulkCancelled', { count }));
  702. },
  703. onError: () => showToast(t('queue.toast.bulkCancelFailed'), 'error'),
  704. });
  705. const handleToggleSelect = (id: number) => {
  706. setSelectedItems(prev =>
  707. prev.includes(id) ? prev.filter(x => x !== id) : [...prev, id]
  708. );
  709. };
  710. // Get unique locations from printers for the filter dropdown
  711. const uniqueLocations = useMemo(() => {
  712. const locations = new Set<string>();
  713. printers?.forEach(p => {
  714. if (p.location) locations.add(p.location);
  715. });
  716. // Also include locations from queue items (for model-based assignments)
  717. queue?.forEach(item => {
  718. if (item.target_location) locations.add(item.target_location);
  719. });
  720. return Array.from(locations).sort();
  721. }, [printers, queue]);
  722. // Helper to check if a queue item matches the location filter
  723. const matchesLocationFilter = useCallback((item: PrintQueueItem): boolean => {
  724. if (!filterLocation) return true;
  725. // For model-based assignments, check target_location
  726. if (item.target_location) return item.target_location === filterLocation;
  727. // For printer-based assignments, check the printer's location
  728. if (item.printer_id) {
  729. const printer = printers?.find(p => p.id === item.printer_id);
  730. return printer?.location === filterLocation;
  731. }
  732. return false;
  733. }, [filterLocation, printers]);
  734. const pendingItems = useMemo(() => {
  735. let items = queue?.filter(i => i.status === 'pending') || [];
  736. // Apply location filter
  737. if (filterLocation) {
  738. items = items.filter(matchesLocationFilter);
  739. }
  740. // Helper to get scheduled time as timestamp (ASAP/placeholder = 0 for earliest)
  741. const getScheduledTime = (item: PrintQueueItem): number => {
  742. if (!item.scheduled_time) return 0;
  743. const time = new Date(item.scheduled_time).getTime();
  744. // Placeholder dates (> 6 months out) are treated as ASAP
  745. const sixMonthsFromNow = Date.now() + (180 * 24 * 60 * 60 * 1000);
  746. return time > sixMonthsFromNow ? 0 : time;
  747. };
  748. return [...items].sort((a, b) => {
  749. let cmp: number;
  750. if (pendingSortBy === 'name') {
  751. const aName = a.archive_name || a.library_file_name || '';
  752. const bName = b.archive_name || b.library_file_name || '';
  753. cmp = aName.localeCompare(bName);
  754. } else if (pendingSortBy === 'printer') {
  755. cmp = (a.printer_name || '').localeCompare(b.printer_name || '');
  756. } else if (pendingSortBy === 'time') {
  757. // Sort by scheduled start time (when print will begin)
  758. cmp = getScheduledTime(a) - getScheduledTime(b);
  759. } else {
  760. cmp = a.position - b.position;
  761. }
  762. return pendingSortAsc ? cmp : -cmp;
  763. });
  764. }, [queue, pendingSortBy, pendingSortAsc, matchesLocationFilter, filterLocation]);
  765. const handleSelectAll = () => {
  766. const allPendingIds = pendingItems.map(i => i.id);
  767. if (selectedItems.length === allPendingIds.length) {
  768. setSelectedItems([]);
  769. } else {
  770. setSelectedItems(allPendingIds);
  771. }
  772. };
  773. const activeItems = useMemo(() => {
  774. let items = queue?.filter(i => i.status === 'printing') || [];
  775. if (filterLocation) {
  776. items = items.filter(matchesLocationFilter);
  777. }
  778. return items;
  779. }, [queue, filterLocation, matchesLocationFilter]);
  780. // Get unique printer IDs from active items to fetch their statuses
  781. const activePrinterIds = useMemo(() => {
  782. const ids = new Set<number>();
  783. activeItems.forEach(item => {
  784. if (item.printer_id) ids.add(item.printer_id);
  785. });
  786. return Array.from(ids);
  787. }, [activeItems]);
  788. // Fetch printer statuses for printers with active jobs
  789. const printerStatusQueries = useQueries({
  790. queries: activePrinterIds.map(printerId => ({
  791. queryKey: ['printerStatus', printerId],
  792. queryFn: () => api.getPrinterStatus(printerId),
  793. refetchInterval: 5000,
  794. })),
  795. });
  796. // Build a map of printer_id -> state for quick lookup
  797. const printerStateMap = useMemo(() => {
  798. const map: Record<number, string | null> = {};
  799. activePrinterIds.forEach((printerId, index) => {
  800. const result = printerStatusQueries[index];
  801. if (result?.data?.state) {
  802. map[printerId] = result.data.state;
  803. }
  804. });
  805. return map;
  806. }, [activePrinterIds, printerStatusQueries]);
  807. const historyItems = useMemo(() => {
  808. let items = queue?.filter(i => ['completed', 'failed', 'skipped', 'cancelled'].includes(i.status)) || [];
  809. if (filterLocation) {
  810. items = items.filter(matchesLocationFilter);
  811. }
  812. return [...items].sort((a, b) => {
  813. let cmp: number;
  814. if (historySortBy === 'name') {
  815. const aName = a.archive_name || a.library_file_name || '';
  816. const bName = b.archive_name || b.library_file_name || '';
  817. cmp = aName.localeCompare(bName);
  818. } else if (historySortBy === 'printer') {
  819. cmp = (a.printer_name || '').localeCompare(b.printer_name || '');
  820. } else {
  821. // Default: by date - most recent first (desc) is the natural order
  822. cmp = new Date(b.completed_at || b.created_at).getTime() - new Date(a.completed_at || a.created_at).getTime();
  823. }
  824. return historySortAsc ? -cmp : cmp;
  825. });
  826. }, [queue, historySortBy, historySortAsc, matchesLocationFilter, filterLocation]);
  827. // Calculate total queue time
  828. const totalQueueTime = useMemo(() => {
  829. return pendingItems.reduce((acc, item) => acc + (item.print_time_seconds || 0), 0);
  830. }, [pendingItems]);
  831. const handleDragEnd = (event: DragEndEvent) => {
  832. const { active, over } = event;
  833. if (!over || active.id === over.id) return;
  834. const oldIndex = pendingItems.findIndex(i => i.id === active.id);
  835. const newIndex = pendingItems.findIndex(i => i.id === over.id);
  836. if (oldIndex !== -1 && newIndex !== -1) {
  837. const reordered = arrayMove(pendingItems, oldIndex, newIndex);
  838. const updates = reordered.map((item, index) => ({
  839. id: item.id,
  840. position: index + 1,
  841. }));
  842. reorderMutation.mutate(updates);
  843. }
  844. };
  845. return (
  846. <div className="p-4 md:p-8">
  847. {/* Header */}
  848. <div className="flex items-center justify-between mb-8">
  849. <div>
  850. <h1 className="text-2xl font-bold text-white flex items-center gap-3">
  851. <ListOrdered className="w-7 h-7 text-bambu-green" />
  852. {t('queue.title')}
  853. </h1>
  854. <p className="text-bambu-gray mt-1">{t('queue.subtitle')}</p>
  855. </div>
  856. </div>
  857. {/* Summary Cards */}
  858. <div className="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
  859. <Card className="bg-gradient-to-br from-blue-500/10 to-transparent border-blue-500/20">
  860. <CardContent className="p-4">
  861. <div className="flex items-center gap-3">
  862. <div className="w-10 h-10 rounded-lg bg-blue-500/20 flex items-center justify-center">
  863. <Play className="w-5 h-5 text-blue-400" />
  864. </div>
  865. <div>
  866. <p className="text-2xl font-bold text-white">{activeItems.length}</p>
  867. <p className="text-sm text-bambu-gray">{t('queue.summary.printing')}</p>
  868. </div>
  869. </div>
  870. </CardContent>
  871. </Card>
  872. <Card className="bg-gradient-to-br from-yellow-500/10 to-transparent border-yellow-500/20">
  873. <CardContent className="p-4">
  874. <div className="flex items-center gap-3">
  875. <div className="w-10 h-10 rounded-lg bg-yellow-500/20 flex items-center justify-center">
  876. <Clock className="w-5 h-5 text-yellow-400" />
  877. </div>
  878. <div>
  879. <p className="text-2xl font-bold text-white">{pendingItems.length}</p>
  880. <p className="text-sm text-bambu-gray">{t('queue.summary.queued')}</p>
  881. </div>
  882. </div>
  883. </CardContent>
  884. </Card>
  885. <Card className="bg-gradient-to-br from-bambu-green/10 to-transparent border-bambu-green/20">
  886. <CardContent className="p-4">
  887. <div className="flex items-center gap-3">
  888. <div className="w-10 h-10 rounded-lg bg-bambu-green/20 flex items-center justify-center">
  889. <Timer className="w-5 h-5 text-bambu-green" />
  890. </div>
  891. <div>
  892. <p className="text-2xl font-bold text-white">{formatDuration(totalQueueTime)}</p>
  893. <p className="text-sm text-bambu-gray">{t('queue.summary.totalTime')}</p>
  894. </div>
  895. </div>
  896. </CardContent>
  897. </Card>
  898. <Card className="bg-gradient-to-br from-gray-500/10 to-transparent border-gray-500/20">
  899. <CardContent className="p-4">
  900. <div className="flex items-center gap-3">
  901. <div className="w-10 h-10 rounded-lg bg-gray-500/20 flex items-center justify-center">
  902. <CheckCircle className="w-5 h-5 text-gray-400" />
  903. </div>
  904. <div>
  905. <p className="text-2xl font-bold text-white">{historyItems.length}</p>
  906. <p className="text-sm text-bambu-gray">{t('queue.summary.history')}</p>
  907. </div>
  908. </div>
  909. </CardContent>
  910. </Card>
  911. </div>
  912. {/* Filters */}
  913. <div className="flex items-center gap-4 mb-6">
  914. <select
  915. className="px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  916. value={filterPrinter === -1 ? 'unassigned' : (filterPrinter || '')}
  917. onChange={(e) => {
  918. const val = e.target.value;
  919. if (val === 'unassigned') setFilterPrinter(-1);
  920. else if (val === '') setFilterPrinter(null);
  921. else setFilterPrinter(Number(val));
  922. }}
  923. >
  924. <option value="">{t('queue.filter.allPrinters')}</option>
  925. <option value="unassigned">{t('queue.filter.unassigned')}</option>
  926. {printers?.map((p) => (
  927. <option key={p.id} value={p.id}>{p.name}</option>
  928. ))}
  929. </select>
  930. <select
  931. className="px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  932. value={filterStatus}
  933. onChange={(e) => setFilterStatus(e.target.value)}
  934. >
  935. <option value="">{t('queue.filter.allStatus')}</option>
  936. <option value="pending">{t('queue.status.pending')}</option>
  937. <option value="printing">{t('queue.status.printing')}</option>
  938. <option value="completed">{t('queue.status.completed')}</option>
  939. <option value="failed">{t('queue.status.failed')}</option>
  940. <option value="skipped">{t('queue.status.skipped')}</option>
  941. <option value="cancelled">{t('queue.status.cancelled')}</option>
  942. </select>
  943. {uniqueLocations.length > 0 && (
  944. <select
  945. className="px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  946. value={filterLocation}
  947. onChange={(e) => setFilterLocation(e.target.value)}
  948. >
  949. <option value="">{t('queue.filter.allLocations')}</option>
  950. {uniqueLocations.map((loc) => (
  951. <option key={loc} value={loc}>{loc}</option>
  952. ))}
  953. </select>
  954. )}
  955. <div className="flex-1" />
  956. {historyItems.length > 0 && (
  957. <Button
  958. variant="secondary"
  959. size="sm"
  960. onClick={() => setShowClearHistoryConfirm(true)}
  961. disabled={!hasPermission('queue:delete_all')}
  962. title={!hasPermission('queue:delete_all') ? t('queue.permissions.noClearHistory') : undefined}
  963. >
  964. <Trash2 className="w-4 h-4" />
  965. {t('queue.clearHistory')}
  966. </Button>
  967. )}
  968. </div>
  969. {isLoading ? (
  970. <div className="text-center py-12 text-bambu-gray">{t('common.loading')}</div>
  971. ) : queue?.length === 0 ? (
  972. <Card className="p-12 text-center border-dashed">
  973. <Calendar className="w-16 h-16 text-bambu-gray mx-auto mb-4 opacity-50" />
  974. <h3 className="text-xl font-medium text-white mb-2">{t('queue.empty.title')}</h3>
  975. <p className="text-bambu-gray max-w-md mx-auto">
  976. {t('queue.empty.description')}
  977. </p>
  978. </Card>
  979. ) : (
  980. <div className="space-y-8">
  981. {/* Active Prints */}
  982. {activeItems.length > 0 && (
  983. <div>
  984. <h2 className="text-lg font-semibold text-white mb-4 flex items-center gap-2">
  985. <div className="w-2 h-2 rounded-full bg-blue-400 animate-pulse" />
  986. {t('queue.sections.currentlyPrinting')}
  987. </h2>
  988. <div className="space-y-3">
  989. {activeItems.map((item) => (
  990. <SortableQueueItem
  991. key={item.id}
  992. item={item}
  993. onEdit={() => {}}
  994. onCancel={() => {}}
  995. onRemove={() => {}}
  996. onStop={() => setConfirmAction({ type: 'stop', item })}
  997. onRequeue={() => {}}
  998. onStart={() => {}}
  999. timeFormat={timeFormat}
  1000. hasPermission={hasPermission}
  1001. canModify={canModify}
  1002. printerState={item.printer_id ? printerStateMap[item.printer_id] : null}
  1003. t={t}
  1004. />
  1005. ))}
  1006. </div>
  1007. </div>
  1008. )}
  1009. {/* Pending Queue */}
  1010. {pendingItems.length > 0 && (
  1011. <div>
  1012. <div className="flex items-center justify-between mb-4">
  1013. <h2 className="text-lg font-semibold text-white flex items-center gap-2">
  1014. <Clock className="w-5 h-5 text-yellow-400" />
  1015. {t('queue.sections.queued')}
  1016. <span className="text-sm font-normal text-bambu-gray">
  1017. ({t('queue.itemCount', { count: pendingItems.length })})
  1018. </span>
  1019. <span className="text-xs text-bambu-gray ml-2" title={t('queue.reorderHint')}>
  1020. {t('queue.dragToReorder')}
  1021. </span>
  1022. </h2>
  1023. <div className="flex items-center gap-2">
  1024. <select
  1025. className="px-3 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  1026. value={pendingSortBy}
  1027. onChange={(e) => setPendingSortBy(e.target.value as 'position' | 'name' | 'printer' | 'time')}
  1028. >
  1029. <option value="position">{t('queue.sort.byPosition')}</option>
  1030. <option value="name">{t('queue.sort.byName')}</option>
  1031. <option value="printer">{t('queue.sort.byPrinter')}</option>
  1032. <option value="time">{t('queue.sort.bySchedule')}</option>
  1033. </select>
  1034. <Button
  1035. variant="ghost"
  1036. size="sm"
  1037. onClick={() => setPendingSortAsc(!pendingSortAsc)}
  1038. title={pendingSortAsc ? t('common.ascending') : t('common.descending')}
  1039. className="px-2"
  1040. >
  1041. {pendingSortAsc ? <ArrowUp className="w-4 h-4" /> : <ArrowDown className="w-4 h-4" />}
  1042. </Button>
  1043. </div>
  1044. </div>
  1045. {/* Bulk action toolbar */}
  1046. <div className="flex items-center gap-3 mb-4 p-3 bg-bambu-dark rounded-lg">
  1047. <Button
  1048. variant="ghost"
  1049. size="sm"
  1050. onClick={handleSelectAll}
  1051. className="flex items-center gap-2"
  1052. >
  1053. {selectedItems.length === pendingItems.length && pendingItems.length > 0 ? (
  1054. <CheckSquare className="w-4 h-4 text-bambu-green" />
  1055. ) : (
  1056. <Square className="w-4 h-4" />
  1057. )}
  1058. {selectedItems.length === pendingItems.length && pendingItems.length > 0 ? t('queue.bulkEdit.deselectAll') : t('queue.bulkEdit.selectAll')}
  1059. </Button>
  1060. {selectedItems.length > 0 && (
  1061. <>
  1062. <span className="text-sm text-bambu-gray">
  1063. {t('queue.bulkEdit.selected', { count: selectedItems.length })}
  1064. </span>
  1065. <div className="h-4 w-px bg-bambu-dark-tertiary" />
  1066. <Button
  1067. variant="ghost"
  1068. size="sm"
  1069. onClick={() => setShowBulkEditModal(true)}
  1070. className="flex items-center gap-2 text-bambu-green hover:text-bambu-green-light"
  1071. disabled={!hasAnyPermission('queue:update_own', 'queue:update_all')}
  1072. title={!hasAnyPermission('queue:update_own', 'queue:update_all') ? t('queue.permissions.noEditItems') : undefined}
  1073. >
  1074. <Pencil className="w-4 h-4" />
  1075. {t('queue.bulkEdit.editSelected')}
  1076. </Button>
  1077. <Button
  1078. variant="ghost"
  1079. size="sm"
  1080. onClick={() => bulkCancelMutation.mutate(selectedItems)}
  1081. className="flex items-center gap-2 text-red-400 hover:text-red-300"
  1082. disabled={bulkCancelMutation.isPending || !hasAnyPermission('queue:delete_own', 'queue:delete_all')}
  1083. title={!hasAnyPermission('queue:delete_own', 'queue:delete_all') ? t('queue.permissions.noCancelItems') : undefined}
  1084. >
  1085. <X className="w-4 h-4" />
  1086. {t('queue.bulkEdit.cancelSelected')}
  1087. </Button>
  1088. </>
  1089. )}
  1090. </div>
  1091. <DndContext
  1092. sensors={sensors}
  1093. collisionDetection={closestCenter}
  1094. onDragEnd={handleDragEnd}
  1095. >
  1096. <SortableContext
  1097. items={pendingItems.map(i => i.id)}
  1098. strategy={verticalListSortingStrategy}
  1099. >
  1100. <div className="space-y-3">
  1101. {pendingItems.map((item, index) => (
  1102. <SortableQueueItem
  1103. key={item.id}
  1104. item={item}
  1105. position={index + 1}
  1106. onEdit={() => setEditItem(item)}
  1107. onCancel={() => setConfirmAction({ type: 'cancel', item })}
  1108. onRemove={() => {}}
  1109. onStop={() => {}}
  1110. onRequeue={() => {}}
  1111. onStart={() => startMutation.mutate(item.id)}
  1112. timeFormat={timeFormat}
  1113. isSelected={selectedItems.includes(item.id)}
  1114. onToggleSelect={() => handleToggleSelect(item.id)}
  1115. hasPermission={hasPermission}
  1116. canModify={canModify}
  1117. t={t}
  1118. />
  1119. ))}
  1120. </div>
  1121. </SortableContext>
  1122. </DndContext>
  1123. </div>
  1124. )}
  1125. {/* History */}
  1126. {historyItems.length > 0 && (
  1127. <div>
  1128. <div className="flex items-center justify-between mb-4">
  1129. <h2 className="text-lg font-semibold text-white flex items-center gap-2">
  1130. <CheckCircle className="w-5 h-5 text-bambu-gray" />
  1131. {t('queue.sections.history')}
  1132. <span className="text-sm font-normal text-bambu-gray">
  1133. ({t('queue.itemCount', { count: historyItems.length })})
  1134. </span>
  1135. </h2>
  1136. <div className="flex items-center gap-2">
  1137. <select
  1138. className="px-3 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  1139. value={historySortBy}
  1140. onChange={(e) => setHistorySortBy(e.target.value as 'date' | 'name' | 'printer')}
  1141. >
  1142. <option value="date">{t('queue.sort.byDate')}</option>
  1143. <option value="name">{t('queue.sort.byName')}</option>
  1144. <option value="printer">{t('queue.sort.byPrinter')}</option>
  1145. </select>
  1146. <Button
  1147. variant="ghost"
  1148. size="sm"
  1149. onClick={() => setHistorySortAsc(!historySortAsc)}
  1150. title={historySortAsc ? t('queue.sort.ascendingOldest') : t('queue.sort.descendingNewest')}
  1151. className="px-2"
  1152. >
  1153. {historySortAsc ? <ArrowUp className="w-4 h-4" /> : <ArrowDown className="w-4 h-4" />}
  1154. </Button>
  1155. </div>
  1156. </div>
  1157. <div className="space-y-3">
  1158. {historyItems.slice(0, 20).map((item, index) => (
  1159. <SortableQueueItem
  1160. key={item.id}
  1161. item={item}
  1162. position={index + 1}
  1163. onEdit={() => {}}
  1164. onCancel={() => {}}
  1165. onRemove={() => setConfirmAction({ type: 'remove', item })}
  1166. onStop={() => {}}
  1167. onRequeue={() => setRequeueItem(item)}
  1168. onStart={() => {}}
  1169. timeFormat={timeFormat}
  1170. hasPermission={hasPermission}
  1171. canModify={canModify}
  1172. t={t}
  1173. />
  1174. ))}
  1175. </div>
  1176. </div>
  1177. )}
  1178. </div>
  1179. )}
  1180. {/* Edit Modal */}
  1181. {editItem && (
  1182. <PrintModal
  1183. mode="edit-queue-item"
  1184. archiveId={editItem.archive_id ?? undefined}
  1185. libraryFileId={editItem.library_file_id ?? undefined}
  1186. archiveName={editItem.archive_name || editItem.library_file_name || `File #${editItem.archive_id || editItem.library_file_id}`}
  1187. queueItem={editItem}
  1188. onClose={() => setEditItem(null)}
  1189. />
  1190. )}
  1191. {/* Re-queue Modal */}
  1192. {requeueItem && (
  1193. <PrintModal
  1194. mode="add-to-queue"
  1195. archiveId={requeueItem.archive_id ?? undefined}
  1196. libraryFileId={requeueItem.library_file_id ?? undefined}
  1197. archiveName={requeueItem.archive_name || requeueItem.library_file_name || `File #${requeueItem.archive_id || requeueItem.library_file_id}`}
  1198. onClose={() => setRequeueItem(null)}
  1199. />
  1200. )}
  1201. {/* Confirm Action Modal */}
  1202. {confirmAction && (
  1203. <ConfirmModal
  1204. title={
  1205. confirmAction.type === 'cancel' ? t('queue.confirm.cancelTitle') :
  1206. confirmAction.type === 'stop' ? t('queue.confirm.stopTitle') :
  1207. t('queue.confirm.removeTitle')
  1208. }
  1209. message={
  1210. confirmAction.type === 'cancel'
  1211. ? t('queue.confirm.cancelMessage', { name: confirmAction.item.archive_name || confirmAction.item.library_file_name || t('queue.confirm.thisPrint') })
  1212. : confirmAction.type === 'stop'
  1213. ? t('queue.confirm.stopMessage', { name: confirmAction.item.archive_name || confirmAction.item.library_file_name || t('queue.confirm.thisPrint') })
  1214. : t('queue.confirm.removeMessage', { name: confirmAction.item.archive_name || confirmAction.item.library_file_name || t('queue.confirm.thisItem') })
  1215. }
  1216. confirmText={
  1217. confirmAction.type === 'cancel' ? t('queue.confirm.cancelButton') :
  1218. confirmAction.type === 'stop' ? t('queue.confirm.stopButton') :
  1219. t('common.remove')
  1220. }
  1221. variant="danger"
  1222. onConfirm={() => {
  1223. if (confirmAction.type === 'cancel') {
  1224. cancelMutation.mutate(confirmAction.item.id);
  1225. } else if (confirmAction.type === 'stop') {
  1226. stopMutation.mutate(confirmAction.item.id);
  1227. } else {
  1228. removeMutation.mutate(confirmAction.item.id);
  1229. }
  1230. setConfirmAction(null);
  1231. }}
  1232. onCancel={() => setConfirmAction(null)}
  1233. />
  1234. )}
  1235. {/* Clear History Confirm Modal */}
  1236. {showClearHistoryConfirm && (
  1237. <ConfirmModal
  1238. title={t('queue.confirm.clearHistoryTitle')}
  1239. message={t('queue.confirm.clearHistoryMessage', { count: historyItems.length })}
  1240. confirmText={t('queue.clearHistory')}
  1241. variant="danger"
  1242. onConfirm={() => {
  1243. clearHistoryMutation.mutate();
  1244. setShowClearHistoryConfirm(false);
  1245. }}
  1246. onCancel={() => setShowClearHistoryConfirm(false)}
  1247. />
  1248. )}
  1249. {/* Bulk Edit Modal */}
  1250. {showBulkEditModal && (
  1251. <BulkEditModal
  1252. selectedCount={selectedItems.length}
  1253. printers={printers?.map(p => ({ id: p.id, name: p.name })) || []}
  1254. onSave={(data) => {
  1255. if (Object.keys(data).length > 0) {
  1256. bulkUpdateMutation.mutate({ item_ids: selectedItems, ...data });
  1257. }
  1258. }}
  1259. onClose={() => setShowBulkEditModal(false)}
  1260. isSaving={bulkUpdateMutation.isPending}
  1261. t={t}
  1262. />
  1263. )}
  1264. </div>
  1265. );
  1266. }