CompactHistoryRow.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {
  2. CheckCircle,
  3. XCircle,
  4. SkipForward,
  5. Ban,
  6. RefreshCw,
  7. Trash2,
  8. Printer,
  9. Timer,
  10. Layers,
  11. User,
  12. AlertCircle,
  13. Weight,
  14. } from 'lucide-react';
  15. import { queueItemThumbnailUrl } from '../utils/queueItemThumbnail';
  16. import { type TimeFormat, formatDuration, formatRelativeTime } from '../utils/date';
  17. import type { PrintQueueItem, Permission } from '../api/client';
  18. import { Button } from './Button';
  19. import { queueItemDisplayName } from '../utils/queueItemName';
  20. const STATUS_CONFIG = {
  21. completed: { icon: CheckCircle, color: 'text-emerald-600 dark:text-emerald-400', border: 'border-l-emerald-500' },
  22. failed: { icon: XCircle, color: 'text-red-600 dark:text-red-400', border: 'border-l-red-500' },
  23. skipped: { icon: SkipForward, color: 'text-orange-600 dark:text-orange-400', border: 'border-l-gray-500' },
  24. cancelled: { icon: Ban, color: 'text-gray-400', border: 'border-l-gray-500' },
  25. } as const;
  26. /** Bambu encodes "no filament" as transparent/zeroed RGBA. The slicer's
  27. * filament_color is a hex string like "RRGGBBAA" or "RRGGBB"; treat all-zero
  28. * / unparsable as no swatch. */
  29. function normalizeFilamentColor(raw: string | null | undefined): string | null {
  30. if (!raw) return null;
  31. const clean = raw.startsWith('#') ? raw.slice(1) : raw;
  32. if (/^0{6,8}$/.test(clean)) return null;
  33. if (!/^[0-9a-fA-F]{6,8}$/.test(clean)) return null;
  34. return `#${clean.slice(0, 6)}`;
  35. }
  36. export function CompactHistoryRow({
  37. item,
  38. onRequeue,
  39. onRemove,
  40. timeFormat = 'system',
  41. hasPermission,
  42. canModify,
  43. t,
  44. }: {
  45. item: PrintQueueItem;
  46. onRequeue: () => void;
  47. onRemove: () => void;
  48. timeFormat?: TimeFormat;
  49. hasPermission: (permission: Permission) => boolean;
  50. canModify: (resource: 'queue' | 'archives' | 'library', action: 'update' | 'delete' | 'reprint', createdById: number | null | undefined) => boolean;
  51. t: (key: string, options?: Record<string, unknown>) => string;
  52. }) {
  53. const config = STATUS_CONFIG[item.status as keyof typeof STATUS_CONFIG] || STATUS_CONFIG.cancelled;
  54. const StatusIcon = config.icon;
  55. const displayName = queueItemDisplayName(item);
  56. const thumbnailUrl = queueItemThumbnailUrl(item);
  57. const completedTime = item.completed_at || item.created_at;
  58. const filamentColor = normalizeFilamentColor(item.filament_color);
  59. const filamentMass = item.filament_used_grams ? Math.round(item.filament_used_grams) : null;
  60. // Failed and skipped prints carry the diagnostic in error_message; surface
  61. // it inline so the user doesn't have to reopen the row to see why.
  62. const showErrorMessage = !!item.error_message
  63. && (item.status === 'failed' || item.status === 'skipped');
  64. return (
  65. <div className={`px-3 py-2 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary border-l-[3px] ${config.border}`}>
  66. {/* Top row — status / thumb / name / time / actions */}
  67. <div className="flex items-center gap-2 sm:gap-3">
  68. <StatusIcon className={`w-4 h-4 shrink-0 ${config.color}`} />
  69. <div className="relative shrink-0 history-thumb-hover">
  70. <div className="w-8 h-8 bg-bambu-dark rounded overflow-hidden">
  71. {thumbnailUrl ? (
  72. <img src={thumbnailUrl} alt="" className="w-full h-full object-cover" />
  73. ) : (
  74. <div className="w-full h-full flex items-center justify-center text-bambu-gray">
  75. <Layers className="w-4 h-4" />
  76. </div>
  77. )}
  78. </div>
  79. {/* Hover preview — desktop only via CSS @media. Positioned to the
  80. right of the thumbnail; the parent card has no overflow:hidden
  81. so the popup escapes its rounded border. pointer-events-none so
  82. it doesn't interfere with clicks on rows below. */}
  83. {thumbnailUrl && (
  84. <div className="history-thumb-preview absolute z-[60] left-full top-0 ml-2 w-48 h-48 pointer-events-none opacity-0 transition-opacity">
  85. <img
  86. src={thumbnailUrl}
  87. alt=""
  88. className="w-full h-full object-cover rounded-lg shadow-2xl border-2 border-bambu-dark-tertiary bg-bambu-dark"
  89. />
  90. </div>
  91. )}
  92. </div>
  93. <span className="text-sm text-white font-medium truncate min-w-0 flex-1">
  94. {displayName}
  95. </span>
  96. <span
  97. className="text-xs text-bambu-gray shrink-0"
  98. title={completedTime ?? undefined}
  99. >
  100. {formatRelativeTime(completedTime, timeFormat, t)}
  101. </span>
  102. <div className="flex items-center gap-0.5 shrink-0">
  103. <Button
  104. variant="ghost"
  105. size="sm"
  106. onClick={onRequeue}
  107. disabled={!hasPermission('queue:create')}
  108. title={!hasPermission('queue:create') ? t('queue.permissions.noRequeue') : t('queue.actions.requeue')}
  109. className="text-bambu-green hover:text-bambu-green/80 hover:bg-bambu-green/10 p-1.5"
  110. >
  111. <RefreshCw className="w-3.5 h-3.5" />
  112. </Button>
  113. <Button
  114. variant="ghost"
  115. size="sm"
  116. onClick={onRemove}
  117. disabled={!canModify('queue', 'delete', item.created_by_id)}
  118. title={!canModify('queue', 'delete', item.created_by_id) ? t('queue.permissions.noRemove') : t('common.remove')}
  119. className="p-1.5"
  120. >
  121. <Trash2 className="w-3.5 h-3.5" />
  122. </Button>
  123. </div>
  124. </div>
  125. {/* Meta row — printer / filament / duration / user. Indented under the
  126. thumbnail so it lines up with the name. */}
  127. <div className="mt-1 ml-[3.25rem] flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-bambu-gray">
  128. {item.printer_name && (
  129. <span className="flex items-center gap-1 shrink-0">
  130. <Printer className="w-3 h-3" />
  131. <span className="truncate max-w-[100px] sm:max-w-[140px]">{item.printer_name}</span>
  132. </span>
  133. )}
  134. {(filamentColor || filamentMass) && (
  135. <span className="flex items-center gap-1 shrink-0">
  136. {filamentColor ? (
  137. <span
  138. className="inline-block w-2.5 h-2.5 rounded-full border border-white/15"
  139. style={{ backgroundColor: filamentColor }}
  140. aria-hidden
  141. />
  142. ) : (
  143. <Weight className="w-3 h-3" />
  144. )}
  145. <span className="truncate">
  146. {filamentMass ? `${filamentMass}g` : null}
  147. {filamentMass && item.filament_type ? ` ${item.filament_type}` : null}
  148. {!filamentMass && item.filament_type ? item.filament_type : null}
  149. </span>
  150. </span>
  151. )}
  152. {item.print_time_seconds && (
  153. <span className="flex items-center gap-1 shrink-0">
  154. <Timer className="w-3 h-3" />
  155. {formatDuration(item.print_time_seconds)}
  156. </span>
  157. )}
  158. {item.created_by_username && (
  159. <span
  160. className="flex items-center gap-1 shrink-0"
  161. title={t('queue.addedBy', { name: item.created_by_username })}
  162. >
  163. <User className="w-3 h-3" />
  164. <span className="truncate max-w-[120px]">{item.created_by_username}</span>
  165. </span>
  166. )}
  167. </div>
  168. {/* Error message — only rendered on failed/skipped rows. */}
  169. {showErrorMessage && (
  170. <div className="mt-1 ml-[3.25rem] flex items-start gap-1 text-xs text-red-700 dark:text-red-400">
  171. <AlertCircle className="w-3 h-3 mt-0.5 shrink-0" />
  172. <span className="break-words">{item.error_message}</span>
  173. </div>
  174. )}
  175. </div>
  176. );
  177. }