CompactHistoryRow.tsx 7.6 KB

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