PrinterQueueWidget.tsx 5.1 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 { parseUTCDate } from '../utils/date';
  9. interface PrinterQueueWidgetProps {
  10. printerId: number;
  11. printerState?: string | null;
  12. }
  13. function formatRelativeTime(dateString: string | null): string {
  14. if (!dateString) return 'ASAP';
  15. const date = parseUTCDate(dateString);
  16. if (!date) return 'ASAP';
  17. const now = new Date();
  18. const diff = date.getTime() - now.getTime();
  19. if (diff < 0) return 'Now';
  20. if (diff < 60000) return 'In <1 min';
  21. if (diff < 3600000) return `In ${Math.round(diff / 60000)} min`;
  22. if (diff < 86400000) return `In ${Math.round(diff / 3600000)}h`;
  23. return date.toLocaleDateString();
  24. }
  25. export function PrinterQueueWidget({ printerId, printerState }: PrinterQueueWidgetProps) {
  26. const { t } = useTranslation();
  27. const queryClient = useQueryClient();
  28. const { showToast } = useToast();
  29. const { hasPermission } = useAuth();
  30. const { data: queue } = useQuery({
  31. queryKey: ['queue', printerId, 'pending'],
  32. queryFn: () => api.getQueue(printerId, 'pending'),
  33. refetchInterval: 30000,
  34. });
  35. const clearPlateMutation = useMutation({
  36. mutationFn: () => api.clearPlate(printerId),
  37. onSuccess: () => {
  38. queryClient.invalidateQueries({ queryKey: ['queue', printerId] });
  39. queryClient.invalidateQueries({ queryKey: ['printerStatus', printerId] });
  40. showToast(t('queue.clearPlateSuccess'), 'success');
  41. },
  42. onError: (err: Error) => {
  43. showToast(err.message, 'error');
  44. },
  45. });
  46. const nextItem = queue?.[0];
  47. const totalPending = queue?.length || 0;
  48. if (totalPending === 0) {
  49. return null;
  50. }
  51. const needsClearPlate = printerState === 'FINISH' || printerState === 'FAILED';
  52. if (needsClearPlate) {
  53. return (
  54. <div className="mb-3 p-3 bg-bambu-dark rounded-lg border border-yellow-400/30">
  55. <div className="flex items-center gap-3 mb-2">
  56. <Calendar className="w-5 h-5 text-yellow-400 flex-shrink-0" />
  57. <div className="min-w-0 flex-1">
  58. <p className="text-xs text-bambu-gray">{t('queue.nextInQueue')}</p>
  59. <p className="text-sm text-white truncate">
  60. {nextItem?.archive_name || nextItem?.library_file_name || `File #${nextItem?.archive_id || nextItem?.library_file_id}`}
  61. </p>
  62. </div>
  63. {totalPending > 1 && (
  64. <span className="text-xs px-1.5 py-0.5 bg-yellow-400/20 text-yellow-400 rounded flex-shrink-0">
  65. +{totalPending - 1}
  66. </span>
  67. )}
  68. </div>
  69. {clearPlateMutation.isSuccess ? (
  70. <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">
  71. <CircleCheck className="w-4 h-4" />
  72. {t('queue.plateReady')}
  73. </div>
  74. ) : (
  75. <button
  76. onClick={() => clearPlateMutation.mutate()}
  77. disabled={clearPlateMutation.isPending || !hasPermission('printers:control')}
  78. 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"
  79. >
  80. {clearPlateMutation.isPending ? (
  81. <Loader2 className="w-4 h-4 animate-spin" />
  82. ) : (
  83. <CircleCheck className="w-4 h-4" />
  84. )}
  85. {t('queue.clearPlate')}
  86. </button>
  87. )}
  88. </div>
  89. );
  90. }
  91. return (
  92. <Link
  93. to="/queue"
  94. className="block mb-3 p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors"
  95. >
  96. <div className="flex items-center justify-between gap-3">
  97. <div className="flex items-center gap-3 min-w-0 flex-1">
  98. <Calendar className="w-5 h-5 text-yellow-400 flex-shrink-0" />
  99. <div className="min-w-0 flex-1">
  100. <p className="text-xs text-bambu-gray">{t('queue.nextInQueue')}</p>
  101. <p className="text-sm text-white truncate">
  102. {nextItem?.archive_name || nextItem?.library_file_name || `File #${nextItem?.archive_id || nextItem?.library_file_id}`}
  103. </p>
  104. </div>
  105. </div>
  106. <div className="flex items-center gap-2 flex-shrink-0">
  107. <span className="text-xs text-bambu-gray flex items-center gap-1">
  108. <Clock className="w-3 h-3" />
  109. {formatRelativeTime(nextItem?.scheduled_time || null)}
  110. </span>
  111. {totalPending > 1 && (
  112. <span className="text-xs px-1.5 py-0.5 bg-yellow-400/20 text-yellow-400 rounded">
  113. +{totalPending - 1}
  114. </span>
  115. )}
  116. <ChevronRight className="w-4 h-4 text-bambu-gray" />
  117. </div>
  118. </div>
  119. </Link>
  120. );
  121. }