FilamentHoverCard.tsx 16 KB

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