PrinterQueueWidget.tsx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { useQuery } from '@tanstack/react-query';
  2. import { Clock, Calendar, ChevronRight } from 'lucide-react';
  3. import { Link } from 'react-router-dom';
  4. import { useTranslation } from 'react-i18next';
  5. import { api } from '../api/client';
  6. import { formatRelativeTime } from '../utils/date';
  7. import { filterCompatibleQueueItems } from '../utils/printer';
  8. import { queueItemDisplayName } from '../utils/queueItemName';
  9. interface PrinterQueueWidgetProps {
  10. printerId: number;
  11. printerModel?: string | null;
  12. loadedFilamentTypes?: Set<string>;
  13. loadedFilaments?: Set<string>; // "TYPE:rrggbb" pairs for filament override color matching
  14. loadedVariants?: Set<string>; // "TYPE:rrggbb:idx" triples for PLA sub-variant matching (#2650)
  15. variant?: 'card' | 'panelExtension';
  16. }
  17. export function PrinterQueueWidget({ printerId, printerModel, loadedFilamentTypes, loadedFilaments, loadedVariants, variant = 'card' }: PrinterQueueWidgetProps) {
  18. const { t } = useTranslation();
  19. const { data: queue } = useQuery({
  20. queryKey: ['queue', printerId, 'pending', printerModel],
  21. queryFn: () => api.getQueue(printerId, 'pending', printerModel || undefined),
  22. refetchInterval: 30000,
  23. });
  24. // Filter queue to items this printer can actually print (filament type + color check)
  25. const compatibleQueue = queue ? filterCompatibleQueueItems(queue, loadedFilamentTypes, loadedFilaments, loadedVariants) : undefined;
  26. const totalPending = compatibleQueue?.length || 0;
  27. if (totalPending === 0) {
  28. return null;
  29. }
  30. const nextItem = compatibleQueue?.[0];
  31. // Passive next-in-queue preview. Plate-clear acknowledgment is handled by the
  32. // card-level "Mark plate as cleared" button (PrintersPage.tsx). Having a
  33. // second button in this widget caused the two controls to overlap whenever
  34. // the plate-clear gate was up with auto-dispatch items queued — both POSTed
  35. // to the same /clear-plate endpoint, so the widget button was pure noise.
  36. const linkClassName = variant === 'panelExtension'
  37. ? 'block mt-2 border-t border-bambu-dark-tertiary pt-2 pl-1 hover:opacity-90 transition-opacity'
  38. : 'block mb-3 p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors';
  39. return (
  40. <Link
  41. to="/queue"
  42. className={linkClassName}
  43. >
  44. <div className="flex items-center justify-between gap-3">
  45. <div className="flex items-center gap-3 min-w-0 flex-1">
  46. <Calendar className="w-5 h-5 text-yellow-600 dark:text-yellow-400 flex-shrink-0" />
  47. <div className="min-w-0 flex-1">
  48. <p className="text-xs text-bambu-gray">{t('queue.nextInQueue')}</p>
  49. <p className="text-sm text-white truncate">
  50. {nextItem ? queueItemDisplayName(nextItem) : ''}
  51. </p>
  52. </div>
  53. </div>
  54. <div className="flex items-center gap-2 flex-shrink-0">
  55. <span className="text-xs text-bambu-gray flex items-center gap-1">
  56. <Clock className="w-3 h-3" />
  57. {nextItem?.scheduled_time ? formatRelativeTime(nextItem.scheduled_time, 'system', t) : t('time.waiting')}
  58. </span>
  59. {totalPending > 1 && (
  60. <span className="text-xs px-1.5 py-0.5 bg-yellow-100 dark:bg-yellow-400/20 text-yellow-700 dark:text-yellow-400 rounded">
  61. +{totalPending - 1}
  62. </span>
  63. )}
  64. <ChevronRight className="w-4 h-4 text-bambu-gray" />
  65. </div>
  66. </div>
  67. </Link>
  68. );
  69. }