PrinterQueueWidget.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  2. import { Clock, Calendar, ChevronRight, Loader2, CircleCheck } from 'lucide-react';
  3. import { Link } from 'react-router-dom';
  4. import { useTranslation } from 'react-i18next';
  5. import { api } from '../api/client';
  6. import { useAuth } from '../contexts/AuthContext';
  7. import { useToast } from '../contexts/ToastContext';
  8. import { formatRelativeTime } from '../utils/date';
  9. import { filterCompatibleQueueItems } from '../utils/printer';
  10. interface PrinterQueueWidgetProps {
  11. printerId: number;
  12. printerModel?: string | null;
  13. printerState?: string | null;
  14. plateCleared?: boolean;
  15. requirePlateClear?: boolean;
  16. loadedFilamentTypes?: Set<string>;
  17. loadedFilaments?: Set<string>; // "TYPE:rrggbb" pairs for filament override color matching
  18. }
  19. export function PrinterQueueWidget({ printerId, printerModel, printerState, plateCleared, requirePlateClear = true, loadedFilamentTypes, loadedFilaments }: PrinterQueueWidgetProps) {
  20. const { t } = useTranslation();
  21. const queryClient = useQueryClient();
  22. const { showToast } = useToast();
  23. const { hasPermission } = useAuth();
  24. const { data: queue } = useQuery({
  25. queryKey: ['queue', printerId, 'pending', printerModel],
  26. queryFn: () => api.getQueue(printerId, 'pending', printerModel || undefined),
  27. refetchInterval: 30000,
  28. });
  29. const clearPlateMutation = useMutation({
  30. mutationFn: () => api.clearPlate(printerId),
  31. onSuccess: () => {
  32. queryClient.invalidateQueries({ queryKey: ['queue', printerId] });
  33. queryClient.invalidateQueries({ queryKey: ['printerStatus', printerId] });
  34. showToast(t('queue.clearPlateSuccess'), 'success');
  35. },
  36. onError: (err: Error) => {
  37. showToast(err.message, 'error');
  38. },
  39. });
  40. // Filter queue to items this printer can actually print (filament type + color check)
  41. const compatibleQueue = queue ? filterCompatibleQueueItems(queue, loadedFilamentTypes, loadedFilaments) : undefined;
  42. // Split into auto-dispatchable vs staged (manual_start) items
  43. const autoDispatchQueue = compatibleQueue?.filter(item => !item.manual_start) ?? [];
  44. const totalPending = compatibleQueue?.length || 0;
  45. if (totalPending === 0) {
  46. return null;
  47. }
  48. const nextAutoItem = autoDispatchQueue[0];
  49. const nextItem = compatibleQueue?.[0];
  50. // Only prompt "Clear Plate & Start Next" when there are auto-dispatchable items
  51. const needsClearPlate = requirePlateClear && (printerState === 'FINISH' || printerState === 'FAILED') && !plateCleared && autoDispatchQueue.length > 0;
  52. if (needsClearPlate) {
  53. const displayItem = nextAutoItem || nextItem;
  54. return (
  55. <div className="mb-3 p-3 bg-bambu-dark rounded-lg border border-yellow-400/30">
  56. <div className="flex items-center gap-3 mb-2">
  57. <Calendar className="w-5 h-5 text-yellow-400 flex-shrink-0" />
  58. <div className="min-w-0 flex-1">
  59. <p className="text-xs text-bambu-gray">{t('queue.nextInQueue')}</p>
  60. <p className="text-sm text-white truncate">
  61. {displayItem?.archive_name || displayItem?.library_file_name || `File #${displayItem?.archive_id || displayItem?.library_file_id}`}
  62. </p>
  63. </div>
  64. {totalPending > 1 && (
  65. <span className="text-xs px-1.5 py-0.5 bg-yellow-400/20 text-yellow-400 rounded flex-shrink-0">
  66. +{totalPending - 1}
  67. </span>
  68. )}
  69. </div>
  70. {clearPlateMutation.isSuccess ? (
  71. <div className="w-full py-2 px-3 rounded-lg bg-bambu-green/10 border border-bambu-green/20 text-bambu-green text-sm flex items-center justify-center gap-2">
  72. <CircleCheck className="w-4 h-4" />
  73. {t('queue.plateReady')}
  74. </div>
  75. ) : (
  76. <button
  77. onClick={() => clearPlateMutation.mutate()}
  78. disabled={clearPlateMutation.isPending || !hasPermission('printers:clear_plate')}
  79. className="w-full py-2 px-3 rounded-lg bg-bambu-green/20 border border-bambu-green/40 text-bambu-green hover:bg-bambu-green/30 transition-colors text-sm font-medium flex items-center justify-center gap-2 disabled:opacity-50"
  80. >
  81. {clearPlateMutation.isPending ? (
  82. <Loader2 className="w-4 h-4 animate-spin" />
  83. ) : (
  84. <CircleCheck className="w-4 h-4" />
  85. )}
  86. {t('queue.clearPlate')}
  87. </button>
  88. )}
  89. </div>
  90. );
  91. }
  92. return (
  93. <Link
  94. to="/queue"
  95. className="block mb-3 p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors"
  96. >
  97. <div className="flex items-center justify-between gap-3">
  98. <div className="flex items-center gap-3 min-w-0 flex-1">
  99. <Calendar className="w-5 h-5 text-yellow-400 flex-shrink-0" />
  100. <div className="min-w-0 flex-1">
  101. <p className="text-xs text-bambu-gray">{t('queue.nextInQueue')}</p>
  102. <p className="text-sm text-white truncate">
  103. {nextItem?.archive_name || nextItem?.library_file_name || `File #${nextItem?.archive_id || nextItem?.library_file_id}`}
  104. </p>
  105. </div>
  106. </div>
  107. <div className="flex items-center gap-2 flex-shrink-0">
  108. <span className="text-xs text-bambu-gray flex items-center gap-1">
  109. <Clock className="w-3 h-3" />
  110. {nextItem?.scheduled_time ? formatRelativeTime(nextItem.scheduled_time, 'system', t) : t('time.waiting')}
  111. </span>
  112. {totalPending > 1 && (
  113. <span className="text-xs px-1.5 py-0.5 bg-yellow-400/20 text-yellow-400 rounded">
  114. +{totalPending - 1}
  115. </span>
  116. )}
  117. <ChevronRight className="w-4 h-4 text-bambu-gray" />
  118. </div>
  119. </div>
  120. </Link>
  121. );
  122. }