PrinterQueueWidget.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // The printer's model is resolved on the server, as GET /queue/ does when
  22. // no target_model is given; the key keeps it so a model change refetches.
  23. queryFn: () => api.getPrinterQueue(printerId, 'pending'),
  24. refetchInterval: 30000,
  25. });
  26. // Filter queue to items this printer can actually print (filament type + color check)
  27. const compatibleQueue = queue ? filterCompatibleQueueItems(queue, loadedFilamentTypes, loadedFilaments, loadedVariants) : undefined;
  28. const totalPending = compatibleQueue?.length || 0;
  29. if (totalPending === 0) {
  30. return null;
  31. }
  32. const nextItem = compatibleQueue?.[0];
  33. // Passive next-in-queue preview. Plate-clear acknowledgment is handled by the
  34. // card-level "Mark plate as cleared" button (PrintersPage.tsx). Having a
  35. // second button in this widget caused the two controls to overlap whenever
  36. // the plate-clear gate was up with auto-dispatch items queued — both POSTed
  37. // to the same /clear-plate endpoint, so the widget button was pure noise.
  38. const linkClassName = variant === 'panelExtension'
  39. ? 'block mt-2 border-t border-bambu-dark-tertiary pt-2 pl-1 hover:opacity-90 transition-opacity'
  40. : 'block mb-3 p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors';
  41. return (
  42. <Link
  43. to="/queue"
  44. className={linkClassName}
  45. >
  46. <div className="flex items-center justify-between gap-3">
  47. <div className="flex items-center gap-3 min-w-0 flex-1">
  48. <Calendar className="w-5 h-5 text-yellow-600 dark:text-yellow-400 flex-shrink-0" />
  49. <div className="min-w-0 flex-1">
  50. <p className="text-xs text-bambu-gray">{t('queue.nextInQueue')}</p>
  51. <p className="text-sm text-white truncate">
  52. {nextItem ? queueItemDisplayName(nextItem) : ''}
  53. </p>
  54. </div>
  55. </div>
  56. <div className="flex items-center gap-2 flex-shrink-0">
  57. <span className="text-xs text-bambu-gray flex items-center gap-1">
  58. <Clock className="w-3 h-3" />
  59. {nextItem?.scheduled_time ? formatRelativeTime(nextItem.scheduled_time, 'system', t) : t('time.waiting')}
  60. </span>
  61. {totalPending > 1 && (
  62. <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">
  63. +{totalPending - 1}
  64. </span>
  65. )}
  66. <ChevronRight className="w-4 h-4 text-bambu-gray" />
  67. </div>
  68. </div>
  69. </Link>
  70. );
  71. }