CompactHistoryRow.tsx 7.6 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 { 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. 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 = item.archive_thumbnail
  57. ? api.getArchiveThumbnail(item.archive_id!)
  58. : item.library_file_thumbnail
  59. ? api.getLibraryFileThumbnailUrl(item.library_file_id!)
  60. : null;
  61. const completedTime = item.completed_at || item.created_at;
  62. const filamentColor = normalizeFilamentColor(item.filament_color);
  63. const filamentMass = item.filament_used_grams ? Math.round(item.filament_used_grams) : null;
  64. // Failed and skipped prints carry the diagnostic in error_message; surface
  65. // it inline so the user doesn't have to reopen the row to see why.
  66. const showErrorMessage = !!item.error_message
  67. && (item.status === 'failed' || item.status === 'skipped');
  68. return (
  69. <div className={`px-3 py-2 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary border-l-[3px] ${config.border}`}>
  70. {/* Top row — status / thumb / name / time / actions */}
  71. <div className="flex items-center gap-2 sm:gap-3">
  72. <StatusIcon className={`w-4 h-4 shrink-0 ${config.color}`} />
  73. <div className="relative shrink-0 history-thumb-hover">
  74. <div className="w-8 h-8 bg-bambu-dark rounded overflow-hidden">
  75. {thumbnailUrl ? (
  76. <img src={thumbnailUrl} alt="" className="w-full h-full object-cover" />
  77. ) : (
  78. <div className="w-full h-full flex items-center justify-center text-bambu-gray">
  79. <Layers className="w-4 h-4" />
  80. </div>
  81. )}
  82. </div>
  83. {/* Hover preview — desktop only via CSS @media. Positioned to the
  84. right of the thumbnail; the parent card has no overflow:hidden
  85. so the popup escapes its rounded border. pointer-events-none so
  86. it doesn't interfere with clicks on rows below. */}
  87. {thumbnailUrl && (
  88. <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">
  89. <img
  90. src={thumbnailUrl}
  91. alt=""
  92. className="w-full h-full object-cover rounded-lg shadow-2xl border-2 border-bambu-dark-tertiary bg-bambu-dark"
  93. />
  94. </div>
  95. )}
  96. </div>
  97. <span className="text-sm text-white font-medium truncate min-w-0 flex-1">
  98. {displayName}
  99. </span>
  100. <span
  101. className="text-xs text-bambu-gray shrink-0"
  102. title={completedTime ?? undefined}
  103. >
  104. {formatRelativeTime(completedTime, timeFormat, t)}
  105. </span>
  106. <div className="flex items-center gap-0.5 shrink-0">
  107. <Button
  108. variant="ghost"
  109. size="sm"
  110. onClick={onRequeue}
  111. disabled={!hasPermission('queue:create')}
  112. title={!hasPermission('queue:create') ? t('queue.permissions.noRequeue') : t('queue.actions.requeue')}
  113. className="text-bambu-green hover:text-bambu-green/80 hover:bg-bambu-green/10 p-1.5"
  114. >
  115. <RefreshCw className="w-3.5 h-3.5" />
  116. </Button>
  117. <Button
  118. variant="ghost"
  119. size="sm"
  120. onClick={onRemove}
  121. disabled={!canModify('queue', 'delete', item.created_by_id)}
  122. title={!canModify('queue', 'delete', item.created_by_id) ? t('queue.permissions.noRemove') : t('common.remove')}
  123. className="p-1.5"
  124. >
  125. <Trash2 className="w-3.5 h-3.5" />
  126. </Button>
  127. </div>
  128. </div>
  129. {/* Meta row — printer / filament / duration / user. Indented under the
  130. thumbnail so it lines up with the name. */}
  131. <div className="mt-1 ml-[3.25rem] flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-bambu-gray">
  132. {item.printer_name && (
  133. <span className="flex items-center gap-1 shrink-0">
  134. <Printer className="w-3 h-3" />
  135. <span className="truncate max-w-[100px] sm:max-w-[140px]">{item.printer_name}</span>
  136. </span>
  137. )}
  138. {(filamentColor || filamentMass) && (
  139. <span className="flex items-center gap-1 shrink-0">
  140. {filamentColor ? (
  141. <span
  142. className="inline-block w-2.5 h-2.5 rounded-full border border-white/15"
  143. style={{ backgroundColor: filamentColor }}
  144. aria-hidden
  145. />
  146. ) : (
  147. <Weight className="w-3 h-3" />
  148. )}
  149. <span className="truncate">
  150. {filamentMass ? `${filamentMass}g` : null}
  151. {filamentMass && item.filament_type ? ` ${item.filament_type}` : null}
  152. {!filamentMass && item.filament_type ? item.filament_type : null}
  153. </span>
  154. </span>
  155. )}
  156. {item.print_time_seconds && (
  157. <span className="flex items-center gap-1 shrink-0">
  158. <Timer className="w-3 h-3" />
  159. {formatDuration(item.print_time_seconds)}
  160. </span>
  161. )}
  162. {item.created_by_username && (
  163. <span
  164. className="flex items-center gap-1 shrink-0"
  165. title={t('queue.addedBy', { name: item.created_by_username })}
  166. >
  167. <User className="w-3 h-3" />
  168. <span className="truncate max-w-[120px]">{item.created_by_username}</span>
  169. </span>
  170. )}
  171. </div>
  172. {/* Error message — only rendered on failed/skipped rows. */}
  173. {showErrorMessage && (
  174. <div className="mt-1 ml-[3.25rem] flex items-start gap-1 text-xs text-red-700 dark:text-red-400">
  175. <AlertCircle className="w-3 h-3 mt-0.5 shrink-0" />
  176. <span className="break-words">{item.error_message}</span>
  177. </div>
  178. )}
  179. </div>
  180. );
  181. }