FilamentHoverCard.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. import { useState, useRef, useEffect, type ReactNode } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { Droplets, Link2, Copy, Check, Settings2, ExternalLink, Package, Unlink } from 'lucide-react';
  4. interface FilamentData {
  5. vendor: 'Bambu Lab' | 'Generic';
  6. profile: string;
  7. colorName: string;
  8. colorHex: string | null;
  9. kFactor: string;
  10. fillLevel: number | null; // null = unknown
  11. trayUuid?: string | null; // Bambu Lab spool UUID for Spoolman linking
  12. fillSource?: 'ams' | 'spoolman' | 'inventory'; // Source of fill level data
  13. }
  14. interface SpoolmanConfig {
  15. enabled: boolean;
  16. onLinkSpool?: (trayUuid: string) => void;
  17. hasUnlinkedSpools?: boolean; // Whether there are spools available to link
  18. linkedSpoolId?: number | null; // Spoolman spool ID if this tray is already linked
  19. spoolmanUrl?: string | null; // Base URL for Spoolman (for "Open in Spoolman" link)
  20. }
  21. interface InventoryConfig {
  22. onAssignSpool?: () => void;
  23. onUnassignSpool?: () => void;
  24. assignedSpool?: { id: number; material: string; brand: string | null; color_name: string | null; remainingWeightGrams?: number | null } | null;
  25. }
  26. interface ConfigureSlotConfig {
  27. enabled: boolean;
  28. onConfigure?: () => void;
  29. }
  30. interface FilamentHoverCardProps {
  31. data: FilamentData;
  32. children: ReactNode;
  33. disabled?: boolean;
  34. className?: string;
  35. spoolman?: SpoolmanConfig;
  36. inventory?: InventoryConfig;
  37. configureSlot?: ConfigureSlotConfig;
  38. }
  39. /**
  40. * A hover card that displays filament details when hovering over AMS slots.
  41. * Replaces the basic browser tooltip with a styled popover.
  42. */
  43. export function FilamentHoverCard({ data, children, disabled, className = '', spoolman, inventory, configureSlot }: FilamentHoverCardProps) {
  44. const { t } = useTranslation();
  45. const [isVisible, setIsVisible] = useState(false);
  46. const [position, setPosition] = useState<'top' | 'bottom'>('top');
  47. const [copied, setCopied] = useState(false);
  48. const triggerRef = useRef<HTMLDivElement>(null);
  49. const cardRef = useRef<HTMLDivElement>(null);
  50. const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  51. const handleCopyUuid = () => {
  52. const uuid = data.trayUuid;
  53. if (!uuid) return;
  54. // Try modern clipboard API first, fallback to execCommand
  55. if (navigator.clipboard && window.isSecureContext) {
  56. navigator.clipboard.writeText(uuid).then(() => {
  57. setCopied(true);
  58. setTimeout(() => setCopied(false), 2000);
  59. }).catch(() => {
  60. // Fallback on error
  61. fallbackCopy(uuid);
  62. });
  63. } else {
  64. fallbackCopy(uuid);
  65. }
  66. };
  67. const fallbackCopy = (text: string) => {
  68. const textarea = document.createElement('textarea');
  69. textarea.value = text;
  70. textarea.style.position = 'fixed';
  71. textarea.style.opacity = '0';
  72. document.body.appendChild(textarea);
  73. textarea.select();
  74. try {
  75. document.execCommand('copy');
  76. setCopied(true);
  77. setTimeout(() => setCopied(false), 2000);
  78. } catch {
  79. console.error('Failed to copy to clipboard');
  80. }
  81. document.body.removeChild(textarea);
  82. };
  83. // Calculate position when showing
  84. useEffect(() => {
  85. if (isVisible && triggerRef.current && cardRef.current) {
  86. const triggerRect = triggerRef.current.getBoundingClientRect();
  87. const cardHeight = cardRef.current.offsetHeight;
  88. // Account for fixed header (56px) - space above should exclude header area
  89. const headerHeight = 56;
  90. const spaceAbove = triggerRect.top - headerHeight;
  91. const spaceBelow = window.innerHeight - triggerRect.bottom;
  92. // Prefer top, but flip to bottom if not enough space (accounting for header)
  93. if (spaceAbove < cardHeight + 12 && spaceBelow > spaceAbove) {
  94. setPosition('bottom');
  95. } else {
  96. setPosition('top');
  97. }
  98. }
  99. }, [isVisible]);
  100. const handleMouseEnter = () => {
  101. if (disabled) return;
  102. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  103. // Small delay to prevent flicker on quick mouse movements
  104. timeoutRef.current = setTimeout(() => setIsVisible(true), 80);
  105. };
  106. const handleMouseLeave = () => {
  107. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  108. timeoutRef.current = setTimeout(() => setIsVisible(false), 100);
  109. };
  110. // Cleanup timeout on unmount
  111. useEffect(() => {
  112. return () => {
  113. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  114. };
  115. }, []);
  116. // Get fill bar color based on percentage
  117. const getFillColor = (fill: number): string => {
  118. if (fill <= 15) return '#ef4444'; // red
  119. if (fill <= 30) return '#f97316'; // orange
  120. if (fill <= 50) return '#eab308'; // yellow
  121. return '#22c55e'; // green
  122. };
  123. // Determine if color is light (for text contrast on swatch)
  124. const isLightColor = (hex: string | null): boolean => {
  125. if (!hex) return false;
  126. const cleanHex = hex.replace('#', '');
  127. const r = parseInt(cleanHex.slice(0, 2), 16);
  128. const g = parseInt(cleanHex.slice(2, 4), 16);
  129. const b = parseInt(cleanHex.slice(4, 6), 16);
  130. const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
  131. return luminance > 0.6;
  132. };
  133. const colorHex = data.colorHex ? `#${data.colorHex.replace('#', '')}` : null;
  134. const assignedRemainingWeight = inventory?.assignedSpool?.remainingWeightGrams ?? null;
  135. return (
  136. <div
  137. ref={triggerRef}
  138. className={`relative ${className}`}
  139. onMouseEnter={handleMouseEnter}
  140. onMouseLeave={handleMouseLeave}
  141. >
  142. {children}
  143. {/* Hover Card */}
  144. {isVisible && (
  145. <div
  146. ref={cardRef}
  147. className={`
  148. absolute left-1/2 -translate-x-1/2 z-50
  149. ${position === 'top' ? 'bottom-full mb-2' : 'top-full mt-2'}
  150. animate-in fade-in-0 zoom-in-95 duration-150
  151. `}
  152. style={{
  153. // Ensure card doesn't go off-screen horizontally
  154. maxWidth: 'calc(100vw - 24px)',
  155. }}
  156. >
  157. {/* Card container */}
  158. <div className="
  159. w-52 bg-bambu-dark-secondary border border-bambu-dark-tertiary
  160. rounded-lg shadow-xl overflow-hidden
  161. backdrop-blur-sm
  162. ">
  163. {/* Color swatch header - the hero element */}
  164. <div
  165. className="h-12 relative overflow-hidden"
  166. style={{
  167. backgroundColor: colorHex || '#3d3d3d',
  168. }}
  169. >
  170. {/* Subtle gradient overlay for depth */}
  171. <div className="absolute inset-0 bg-gradient-to-b from-white/10 to-transparent" />
  172. {/* Color name on swatch */}
  173. <div className={`
  174. absolute inset-0 flex items-center justify-center
  175. font-semibold text-sm tracking-wide
  176. ${isLightColor(colorHex) ? 'text-black/80' : 'text-white/90'}
  177. `}>
  178. {data.colorName}
  179. </div>
  180. {/* Vendor badge - solid background for visibility on any color */}
  181. <div className={`
  182. absolute top-1.5 right-1.5 px-1.5 py-0.5 rounded text-[9px] font-bold uppercase tracking-wider
  183. ${data.vendor === 'Bambu Lab'
  184. ? 'bg-black/60 text-white'
  185. : 'bg-black/50 text-white/90'}
  186. `}>
  187. {data.vendor === 'Bambu Lab' ? 'BBL' : 'GEN'}
  188. </div>
  189. </div>
  190. {/* Details section */}
  191. <div className="p-3 space-y-2.5">
  192. {/* Profile name */}
  193. <div className="flex items-center justify-between">
  194. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  195. {t('ams.profile')}
  196. </span>
  197. <span className="text-xs text-white font-semibold truncate max-w-[120px]">
  198. {data.profile}
  199. </span>
  200. </div>
  201. {/* K Factor */}
  202. <div className="flex items-center justify-between">
  203. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  204. {t('ams.kFactor')}
  205. </span>
  206. <span className="text-xs text-bambu-green font-mono font-bold">
  207. {data.kFactor}
  208. </span>
  209. </div>
  210. {/* Fill Level */}
  211. <div className="space-y-1">
  212. <div className="flex items-center justify-between">
  213. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium flex items-center gap-1">
  214. <Droplets className="w-3 h-3" />
  215. {t('ams.fill')}
  216. </span>
  217. <span className="text-xs text-white font-semibold flex items-center gap-1">
  218. <span>{data.fillLevel !== null ? `${data.fillLevel}%` : '—'}</span>
  219. {assignedRemainingWeight !== null && data.fillLevel !== null && (
  220. <span className="text-[9px] text-bambu-gray font-normal">• {assignedRemainingWeight}g</span>
  221. )}
  222. {data.fillSource === 'spoolman' && data.fillLevel !== null && (
  223. <span className="text-[9px] text-bambu-gray font-normal">{t('spoolman.fillSourceLabel')}</span>
  224. )}
  225. {data.fillSource === 'inventory' && data.fillLevel !== null && (
  226. <span className="text-[9px] text-bambu-gray font-normal">{t('inventory.fillSourceLabel')}</span>
  227. )}
  228. </span>
  229. </div>
  230. {/* Fill bar */}
  231. <div className="h-1.5 bg-black/40 rounded-full overflow-hidden">
  232. {data.fillLevel !== null ? (
  233. <div
  234. className="h-full rounded-full transition-all duration-300"
  235. style={{
  236. width: `${data.fillLevel}%`,
  237. backgroundColor: getFillColor(data.fillLevel),
  238. }}
  239. />
  240. ) : (
  241. <div className="h-full w-full bg-bambu-gray/30 rounded-full" />
  242. )}
  243. </div>
  244. </div>
  245. {/* Spoolman section - only show if enabled */}
  246. {spoolman?.enabled && data.trayUuid && (
  247. <div className="pt-2 mt-2 border-t border-bambu-dark-tertiary space-y-2">
  248. {/* Tray UUID with copy button */}
  249. <div className="flex items-center justify-between">
  250. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  251. {t('spoolman.spoolId')}
  252. </span>
  253. <button
  254. onClick={(e) => {
  255. e.stopPropagation();
  256. handleCopyUuid();
  257. }}
  258. className="flex items-center gap-1 text-xs text-bambu-gray hover:text-white transition-colors"
  259. title="Copy spool UUID"
  260. >
  261. <span className="font-mono text-[10px] truncate max-w-[80px]">
  262. {data.trayUuid.slice(0, 8)}...
  263. </span>
  264. {copied ? (
  265. <Check className="w-3 h-3 text-bambu-green" />
  266. ) : (
  267. <Copy className="w-3 h-3" />
  268. )}
  269. </button>
  270. </div>
  271. {/* Open in Spoolman button (when already linked) */}
  272. {spoolman.linkedSpoolId && spoolman.spoolmanUrl && (
  273. <a
  274. href={`${spoolman.spoolmanUrl.replace(/\/$/, '')}/spool/show/${spoolman.linkedSpoolId}`}
  275. target="_blank"
  276. rel="noopener noreferrer"
  277. onClick={(e) => e.stopPropagation()}
  278. className="w-full flex items-center justify-center gap-1.5 px-2 py-1.5 text-xs font-medium rounded transition-colors bg-bambu-green/20 hover:bg-bambu-green/30 text-bambu-green"
  279. title={t('spoolman.openInSpoolman')}
  280. >
  281. <ExternalLink className="w-3.5 h-3.5" />
  282. {t('spoolman.openInSpoolman')}
  283. </a>
  284. )}
  285. {/* Link Spool button (when not linked) */}
  286. {!spoolman.linkedSpoolId && spoolman.onLinkSpool && (
  287. <button
  288. onClick={(e) => {
  289. e.stopPropagation();
  290. if (spoolman.hasUnlinkedSpools !== false) {
  291. spoolman.onLinkSpool?.(data.trayUuid!);
  292. }
  293. }}
  294. disabled={spoolman.hasUnlinkedSpools === false}
  295. className={`w-full flex items-center justify-center gap-1.5 px-2 py-1.5 text-xs font-medium rounded transition-colors ${
  296. spoolman.hasUnlinkedSpools === false
  297. ? 'bg-bambu-gray/10 text-bambu-gray cursor-not-allowed'
  298. : 'bg-bambu-green/20 hover:bg-bambu-green/30 text-bambu-green'
  299. }`}
  300. title={spoolman.hasUnlinkedSpools === false ? t('spoolman.noUnlinkedSpools') : t('spoolman.linkToSpoolman')}
  301. >
  302. <Link2 className="w-3.5 h-3.5" />
  303. {t('spoolman.linkToSpoolman')}
  304. </button>
  305. )}
  306. </div>
  307. )}
  308. {/* Inventory section - only for non-Bambu spools */}
  309. {inventory && data.vendor !== 'Bambu Lab' && (
  310. <div className="pt-2 mt-2 border-t border-bambu-dark-tertiary space-y-2">
  311. {inventory.assignedSpool ? (
  312. <>
  313. <div className="flex items-center gap-1.5">
  314. <Package className="w-3 h-3 text-bambu-green" />
  315. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  316. {t('inventory.assigned')}
  317. </span>
  318. </div>
  319. <p className="text-xs text-white truncate">
  320. {inventory.assignedSpool.brand ? `${inventory.assignedSpool.brand} ` : ''}
  321. {inventory.assignedSpool.material}
  322. {inventory.assignedSpool.color_name ? ` - ${inventory.assignedSpool.color_name}` : ''}
  323. </p>
  324. {inventory.onUnassignSpool && (
  325. <button
  326. onClick={(e) => {
  327. e.stopPropagation();
  328. inventory.onUnassignSpool?.();
  329. }}
  330. className="w-full flex items-center justify-center gap-1.5 px-2 py-1.5 text-xs font-medium rounded transition-colors bg-red-500/20 hover:bg-red-500/30 text-red-400"
  331. >
  332. <Unlink className="w-3.5 h-3.5" />
  333. {t('inventory.unassignSpool')}
  334. </button>
  335. )}
  336. </>
  337. ) : inventory.onAssignSpool ? (
  338. <button
  339. onClick={(e) => {
  340. e.stopPropagation();
  341. inventory.onAssignSpool?.();
  342. }}
  343. className="w-full flex items-center justify-center gap-1.5 px-2 py-1.5 text-xs font-medium rounded transition-colors bg-bambu-blue/20 hover:bg-bambu-blue/30 text-bambu-blue"
  344. >
  345. <Package className="w-3.5 h-3.5" />
  346. {t('inventory.assignSpool')}
  347. </button>
  348. ) : null}
  349. </div>
  350. )}
  351. {/* Configure slot section - always show if enabled */}
  352. {configureSlot?.enabled && (
  353. <div className={`${spoolman?.enabled && data.trayUuid ? '' : 'pt-2 mt-2 border-t border-bambu-dark-tertiary'}`}>
  354. <button
  355. onClick={(e) => {
  356. e.stopPropagation();
  357. configureSlot.onConfigure?.();
  358. }}
  359. className="w-full flex items-center justify-center gap-1.5 px-2 py-1.5 text-xs font-medium rounded transition-colors bg-bambu-blue/20 hover:bg-bambu-blue/30 text-bambu-blue"
  360. title={t('ams.configureSlot')}
  361. >
  362. <Settings2 className="w-3.5 h-3.5" />
  363. {t('ams.configure')}
  364. </button>
  365. </div>
  366. )}
  367. </div>
  368. </div>
  369. {/* Arrow pointer */}
  370. <div
  371. className={`
  372. absolute left-1/2 -translate-x-1/2 w-0 h-0
  373. border-l-[6px] border-l-transparent
  374. border-r-[6px] border-r-transparent
  375. ${position === 'top'
  376. ? 'top-full border-t-[6px] border-t-bambu-dark-tertiary'
  377. : 'bottom-full border-b-[6px] border-b-bambu-dark-tertiary'}
  378. `}
  379. />
  380. </div>
  381. )}
  382. </div>
  383. );
  384. }
  385. interface EmptySlotHoverCardProps {
  386. children: ReactNode;
  387. className?: string;
  388. configureSlot?: ConfigureSlotConfig;
  389. }
  390. /**
  391. * Wrapper for empty slots - shows "Empty" on hover with optional configure button
  392. */
  393. export function EmptySlotHoverCard({ children, className = '', configureSlot }: EmptySlotHoverCardProps) {
  394. const { t } = useTranslation();
  395. const [isVisible, setIsVisible] = useState(false);
  396. const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  397. const handleMouseEnter = () => {
  398. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  399. timeoutRef.current = setTimeout(() => setIsVisible(true), 80);
  400. };
  401. const handleMouseLeave = () => {
  402. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  403. timeoutRef.current = setTimeout(() => setIsVisible(false), 100);
  404. };
  405. useEffect(() => {
  406. return () => {
  407. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  408. };
  409. }, []);
  410. return (
  411. <div
  412. className={`relative ${className}`}
  413. onMouseEnter={handleMouseEnter}
  414. onMouseLeave={handleMouseLeave}
  415. >
  416. {children}
  417. {isVisible && (
  418. <div className="
  419. absolute left-1/2 -translate-x-1/2 bottom-full mb-2 z-50
  420. animate-in fade-in-0 zoom-in-95 duration-150
  421. ">
  422. <div className="
  423. bg-bambu-dark-secondary border border-bambu-dark-tertiary
  424. rounded-md shadow-lg overflow-hidden
  425. ">
  426. <div className="px-3 py-1.5 text-xs text-bambu-gray whitespace-nowrap">
  427. {t('ams.emptySlot')}
  428. </div>
  429. {/* Configure slot button */}
  430. {configureSlot?.enabled && (
  431. <div className="px-2 pb-2">
  432. <button
  433. onClick={(e) => {
  434. e.stopPropagation();
  435. configureSlot.onConfigure?.();
  436. }}
  437. className="w-full flex items-center justify-center gap-1.5 px-2 py-1.5 text-xs font-medium rounded transition-colors bg-bambu-blue/20 hover:bg-bambu-blue/30 text-bambu-blue"
  438. title={t('ams.configureSlot')}
  439. >
  440. <Settings2 className="w-3.5 h-3.5" />
  441. {t('ams.configure')}
  442. </button>
  443. </div>
  444. )}
  445. </div>
  446. <div className="
  447. absolute left-1/2 -translate-x-1/2 top-full w-0 h-0
  448. border-l-[5px] border-l-transparent
  449. border-r-[5px] border-r-transparent
  450. border-t-[5px] border-t-bambu-dark-tertiary
  451. " />
  452. </div>
  453. )}
  454. </div>
  455. );
  456. }