PrinterQueueWidget.tsx 3.4 KB

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