CompactHistoryRow.tsx 7.8 KB

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