PrinterQueueWidget.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 { parseUTCDate } from '../utils/date';
  9. interface PrinterQueueWidgetProps {
  10. printerId: number;
  11. printerState?: string | null;
  12. plateCleared?: boolean;
  13. }
  14. function formatRelativeTime(dateString: string | null): string {
  15. if (!dateString) return 'ASAP';
  16. const date = parseUTCDate(dateString);
  17. if (!date) return 'ASAP';
  18. const now = new Date();
  19. const diff = date.getTime() - now.getTime();
  20. if (diff < 0) return 'Now';
  21. if (diff < 60000) return 'In <1 min';
  22. if (diff < 3600000) return `In ${Math.round(diff / 60000)} min`;
  23. if (diff < 86400000) return `In ${Math.round(diff / 3600000)}h`;
  24. return date.toLocaleDateString();
  25. }
  26. export function PrinterQueueWidget({ printerId, printerState, plateCleared }: PrinterQueueWidgetProps) {
  27. const { t } = useTranslation();
  28. const queryClient = useQueryClient();
  29. const { showToast } = useToast();
  30. const { hasPermission } = useAuth();
  31. const { data: queue } = useQuery({
  32. queryKey: ['queue', printerId, 'pending'],
  33. queryFn: () => api.getQueue(printerId, 'pending'),
  34. refetchInterval: 30000,
  35. });
  36. const clearPlateMutation = useMutation({
  37. mutationFn: () => api.clearPlate(printerId),
  38. onSuccess: () => {
  39. queryClient.invalidateQueries({ queryKey: ['queue', printerId] });
  40. queryClient.invalidateQueries({ queryKey: ['printerStatus', printerId] });
  41. showToast(t('queue.clearPlateSuccess'), 'success');
  42. },
  43. onError: (err: Error) => {
  44. showToast(err.message, 'error');
  45. },
  46. });
  47. const nextItem = queue?.[0];
  48. const totalPending = queue?.length || 0;
  49. if (totalPending === 0) {
  50. return null;
  51. }
  52. const needsClearPlate = (printerState === 'FINISH' || printerState === 'FAILED') && !plateCleared;
  53. if (needsClearPlate) {
  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. {nextItem?.archive_name || nextItem?.library_file_name || `File #${nextItem?.archive_id || nextItem?.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:control')}
  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. {formatRelativeTime(nextItem?.scheduled_time || null)}
  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. }