PrinterQueueWidget.tsx 3.3 KB

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