FilamentHoverCard.tsx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. import { useState, useRef, useEffect, useLayoutEffect, type ReactNode } from 'react';
  2. import { createPortal } from 'react-dom';
  3. import { useNavigate } from 'react-router-dom';
  4. import { useTranslation } from 'react-i18next';
  5. import { Droplets, Copy, Check, Settings2, Package, Unlink } from 'lucide-react';
  6. import { isLightColor } from '../utils/colors';
  7. interface FilamentData {
  8. vendor: 'Bambu Lab' | 'Generic';
  9. profile: string;
  10. colorName: string;
  11. colorHex: string | null;
  12. kFactor: string;
  13. fillLevel: number | null; // null = unknown
  14. trayUuid?: string | null; // Bambu Lab spool UUID for Spoolman linking
  15. tagUid?: string | null; // Generic NFC tag UID fallback for linking
  16. fillSource?: 'ams' | 'spoolman' | 'inventory'; // Source of fill level data
  17. }
  18. interface SpoolmanConfig {
  19. enabled: boolean;
  20. onLinkSpool?: () => void;
  21. onUnlinkSpool?: () => void;
  22. linkedSpoolId?: number | null; // Spoolman spool ID if this tray is already linked
  23. spoolmanUrl?: string | null; // Base URL for Spoolman (for "Open in Spoolman" link)
  24. syncMode?: string | null; // If auto-sync is enabled, we may want to hide the unlink option for Bambu spools
  25. }
  26. interface InventoryConfig {
  27. onAssignSpool?: () => void;
  28. onUnassignSpool?: () => void;
  29. assignedSpool?: { id: number; material: string; brand: string | null; color_name: string | null; remainingWeightGrams?: number | null } | null;
  30. isAssigned?: boolean;
  31. }
  32. interface ConfigureSlotConfig {
  33. enabled: boolean;
  34. onConfigure?: () => void;
  35. }
  36. interface FilamentHoverCardProps {
  37. data: FilamentData;
  38. children: ReactNode;
  39. disabled?: boolean;
  40. className?: string;
  41. spoolman?: SpoolmanConfig;
  42. inventory?: InventoryConfig;
  43. configureSlot?: ConfigureSlotConfig;
  44. }
  45. /**
  46. * A hover card that displays filament details when hovering over AMS slots.
  47. * Replaces the basic browser tooltip with a styled popover.
  48. */
  49. export function FilamentHoverCard({ data, children, disabled, className = '', spoolman, inventory, configureSlot }: FilamentHoverCardProps) {
  50. const { t } = useTranslation();
  51. const navigate = useNavigate();
  52. const [isVisible, setIsVisible] = useState(false);
  53. const [position, setPosition] = useState<'top' | 'bottom'>('top');
  54. // Screen-space coordinates for the portaled card (#1336 follow-up). Using
  55. // a portal + position:fixed lets the popover escape sibling printer cards
  56. // that create their own stacking contexts on the dashboard — without this,
  57. // a card later in DOM order draws over the hover popover regardless of
  58. // z-index because z-index doesn't cross stacking-context boundaries.
  59. const [coords, setCoords] = useState<{ top: number; left: number } | null>(null);
  60. const [copied, setCopied] = useState(false);
  61. const [showUnlinkConfirm, setShowUnlinkConfirm] = useState(false);
  62. const triggerRef = useRef<HTMLDivElement>(null);
  63. const cardRef = useRef<HTMLDivElement>(null);
  64. const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  65. const handleCopyUuid = () => {
  66. const uuid = data.trayUuid;
  67. if (!uuid) return;
  68. // Try modern clipboard API first, fallback to execCommand
  69. if (navigator.clipboard && window.isSecureContext) {
  70. navigator.clipboard.writeText(uuid).then(() => {
  71. setCopied(true);
  72. setTimeout(() => setCopied(false), 2000);
  73. }).catch(() => {
  74. // Fallback on error
  75. fallbackCopy(uuid);
  76. });
  77. } else {
  78. fallbackCopy(uuid);
  79. }
  80. };
  81. const fallbackCopy = (text: string) => {
  82. const textarea = document.createElement('textarea');
  83. textarea.value = text;
  84. textarea.style.position = 'fixed';
  85. textarea.style.opacity = '0';
  86. document.body.appendChild(textarea);
  87. textarea.select();
  88. try {
  89. document.execCommand('copy');
  90. setCopied(true);
  91. setTimeout(() => setCopied(false), 2000);
  92. } catch {
  93. console.error('Failed to copy to clipboard');
  94. }
  95. document.body.removeChild(textarea);
  96. };
  97. // Compute placement (top/bottom) + screen coordinates for the portaled
  98. // card. Runs on visibility change, scroll, and resize so the popover
  99. // tracks the trigger when the viewport moves. useLayoutEffect rather
  100. // than useEffect so the first paint already has the correct coords —
  101. // avoids a one-frame flicker at (0, 0).
  102. useLayoutEffect(() => {
  103. if (!isVisible) {
  104. setCoords(null);
  105. return;
  106. }
  107. const compute = () => {
  108. if (!triggerRef.current || !cardRef.current) return;
  109. const triggerRect = triggerRef.current.getBoundingClientRect();
  110. const cardHeight = cardRef.current.offsetHeight;
  111. const cardWidth = cardRef.current.offsetWidth;
  112. const headerHeight = 56;
  113. const spaceAbove = triggerRect.top - headerHeight;
  114. const spaceBelow = window.innerHeight - triggerRect.bottom;
  115. const placement: 'top' | 'bottom' =
  116. spaceAbove < cardHeight + 12 && spaceBelow > spaceAbove ? 'bottom' : 'top';
  117. const centerX = triggerRect.left + triggerRect.width / 2;
  118. const left = Math.max(8, Math.min(centerX - cardWidth / 2, window.innerWidth - cardWidth - 8));
  119. const top = placement === 'top' ? triggerRect.top - cardHeight - 8 : triggerRect.bottom + 8;
  120. setPosition(placement);
  121. setCoords({ top, left });
  122. };
  123. // First compute is synchronous from the layout effect; a follow-up rAF
  124. // re-measures after the card actually has its rendered dimensions.
  125. compute();
  126. const rafId = requestAnimationFrame(compute);
  127. window.addEventListener('scroll', compute, true);
  128. window.addEventListener('resize', compute);
  129. return () => {
  130. cancelAnimationFrame(rafId);
  131. window.removeEventListener('scroll', compute, true);
  132. window.removeEventListener('resize', compute);
  133. };
  134. }, [isVisible]);
  135. const handleMouseEnter = () => {
  136. if (disabled) return;
  137. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  138. // Small delay to prevent flicker on quick mouse movements
  139. timeoutRef.current = setTimeout(() => setIsVisible(true), 80);
  140. };
  141. const handleMouseLeave = () => {
  142. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  143. timeoutRef.current = setTimeout(() => setIsVisible(false), 100);
  144. };
  145. // Cleanup timeout on unmount
  146. useEffect(() => {
  147. return () => {
  148. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  149. };
  150. }, []);
  151. // Get fill bar color based on percentage
  152. const getFillColor = (fill: number): string => {
  153. if (fill <= 15) return '#ef4444'; // red
  154. if (fill <= 30) return '#f97316'; // orange
  155. if (fill <= 50) return '#eab308'; // yellow
  156. return '#22c55e'; // green
  157. };
  158. const colorHex = data.colorHex ? `#${data.colorHex.replace('#', '')}` : null;
  159. const assignedRemainingWeight = inventory?.assignedSpool?.remainingWeightGrams ?? null;
  160. return (
  161. <div
  162. ref={triggerRef}
  163. className={`relative ${className}`}
  164. onMouseEnter={handleMouseEnter}
  165. onMouseLeave={handleMouseLeave}
  166. >
  167. {children}
  168. {/* Portaled hover card — rendered into document.body so it escapes
  169. any ancestor stacking context. Sibling printer cards on the
  170. dashboard create their own stacking contexts; without the portal
  171. the popover gets covered by the next card even at z-[60]
  172. (#1336 follow-up). */}
  173. {isVisible && createPortal(
  174. <div
  175. ref={cardRef}
  176. className="fixed z-[60] animate-in fade-in-0 zoom-in-95 duration-150"
  177. style={{
  178. top: coords?.top ?? -9999,
  179. left: coords?.left ?? -9999,
  180. maxWidth: 'calc(100vw - 24px)',
  181. // Hide until coords are computed to avoid a (-9999,-9999) flash.
  182. visibility: coords ? 'visible' : 'hidden',
  183. }}
  184. onMouseEnter={handleMouseEnter}
  185. onMouseLeave={handleMouseLeave}
  186. >
  187. {/* Card container */}
  188. <div className="
  189. w-52 bg-bambu-dark-secondary border border-bambu-dark-tertiary
  190. rounded-lg shadow-xl overflow-hidden
  191. backdrop-blur-sm
  192. ">
  193. {/* Color swatch header - the hero element */}
  194. <div
  195. className="h-12 relative overflow-hidden"
  196. style={{
  197. backgroundColor: colorHex || '#3d3d3d',
  198. }}
  199. >
  200. {/* Subtle gradient overlay for depth */}
  201. <div className="absolute inset-0 bg-gradient-to-b from-white/10 to-transparent" />
  202. {/* Color name on swatch */}
  203. <div className={`
  204. absolute inset-0 flex items-center justify-center
  205. font-semibold text-sm tracking-wide
  206. ${isLightColor(colorHex) ? 'text-black/80' : 'text-white/90'}
  207. `}>
  208. {data.colorName}
  209. </div>
  210. {/* Vendor badge - solid background for visibility on any color */}
  211. <div className={`
  212. absolute top-1.5 right-1.5 px-1.5 py-0.5 rounded text-[9px] font-bold uppercase tracking-wider
  213. ${data.vendor === 'Bambu Lab'
  214. ? 'bg-black/60 text-white'
  215. : 'bg-black/50 text-white/90'}
  216. `}>
  217. {data.vendor === 'Bambu Lab' ? 'BBL' : 'GEN'}
  218. </div>
  219. </div>
  220. {/* Details section */}
  221. <div className="p-3 space-y-2.5">
  222. {/* Profile name */}
  223. <div className="flex items-center justify-between">
  224. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  225. {t('ams.profile')}
  226. </span>
  227. <span className="text-xs text-white font-semibold truncate max-w-[120px]">
  228. {data.profile}
  229. </span>
  230. </div>
  231. {/* K Factor */}
  232. <div className="flex items-center justify-between">
  233. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  234. {t('ams.kFactor')}
  235. </span>
  236. <span className="text-xs text-bambu-green font-mono font-bold">
  237. {data.kFactor}
  238. </span>
  239. </div>
  240. {/* Fill Level */}
  241. <div className="space-y-1">
  242. <div className="flex items-center justify-between">
  243. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium flex items-center gap-1">
  244. <Droplets className="w-3 h-3" />
  245. {t('ams.fill')}
  246. </span>
  247. <span className="text-xs text-white font-semibold flex items-center gap-1">
  248. <span>{data.fillLevel !== null ? `${data.fillLevel}%` : '—'}</span>
  249. {assignedRemainingWeight !== null && data.fillLevel !== null && (
  250. <span className="text-[9px] text-bambu-gray font-normal">• {assignedRemainingWeight}g</span>
  251. )}
  252. </span>
  253. </div>
  254. {/* Fill bar */}
  255. <div className="h-1.5 bg-black/40 rounded-full overflow-hidden">
  256. {data.fillLevel !== null ? (
  257. <div
  258. className="h-full rounded-full transition-all duration-300"
  259. style={{
  260. width: `${data.fillLevel}%`,
  261. backgroundColor: getFillColor(data.fillLevel),
  262. }}
  263. />
  264. ) : (
  265. <div className="h-full w-full bg-bambu-gray/30 rounded-full" />
  266. )}
  267. </div>
  268. </div>
  269. {/* Spoolman section - only show if enabled */}
  270. {spoolman?.enabled && (
  271. <div className="pt-2 mt-2 border-t border-bambu-dark-tertiary space-y-2">
  272. {/* Tray UUID with copy button */}
  273. <div className="flex items-center justify-between">
  274. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  275. {t('spoolman.spoolId')}
  276. </span>
  277. {data.trayUuid ? (
  278. <button
  279. onClick={(e) => {
  280. e.stopPropagation();
  281. handleCopyUuid();
  282. }}
  283. className="flex items-center gap-1 text-xs text-bambu-gray hover:text-white transition-colors"
  284. title="Copy spool UUID"
  285. >
  286. <span className="font-mono text-[10px] truncate max-w-[80px]">
  287. {data.trayUuid.slice(0, 8)}...
  288. </span>
  289. {copied ? (
  290. <Check className="w-3 h-3 text-bambu-green" />
  291. ) : (
  292. <Copy className="w-3 h-3" />
  293. )}
  294. </button>
  295. ) : (
  296. <span className="text-[10px] text-bambu-gray">—</span>
  297. )}
  298. </div>
  299. {/* Open in inventory button (when already linked to a Spoolman spool) */}
  300. {spoolman.linkedSpoolId && (
  301. <>
  302. <button
  303. onClick={(e) => {
  304. e.stopPropagation();
  305. navigate(`/inventory?spool=${spoolman.linkedSpoolId}`);
  306. }}
  307. 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"
  308. title={t('inventory.openInInventory')}
  309. >
  310. <Package className="w-3.5 h-3.5" />
  311. {t('inventory.openInInventory')}
  312. </button>
  313. </>
  314. )}
  315. {/* Link/Unlink action buttons intentionally NOT rendered
  316. here. The inventory section below already provides
  317. Assign/Unassign for slot-binding (the primary user
  318. flow in Spoolman mode). Showing the spoolman tag-link
  319. buttons in addition surfaced two red Unlink-icon
  320. buttons for what users perceive as the same action,
  321. regardless of whether the labels said "Unlink Spool"
  322. vs "Unassign Spool". Tag-linking remains available
  323. via dedicated UI (LinkSpoolModal can be opened from
  324. Spoolman settings / inventory page). */}
  325. </div>
  326. )}
  327. {/* Inventory section — shown for every vendor including
  328. Bambu Lab (#1133). The earlier "non-Bambu only" gate
  329. prevented users from manually assigning a Bambu spool
  330. in inventory to an AMS slot when they didn't want to
  331. re-scan via SpoolBuddy NFC. */}
  332. {inventory && (
  333. <div className="pt-2 mt-2 border-t border-bambu-dark-tertiary space-y-2">
  334. {inventory.assignedSpool ? (
  335. <>
  336. <div className="flex items-center gap-1.5">
  337. <Package className="w-3 h-3 text-bambu-green" />
  338. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  339. {t('inventory.assigned')}
  340. </span>
  341. </div>
  342. <div className="flex items-baseline gap-1.5 min-w-0 mb-1">
  343. <p className="text-xs text-white truncate">
  344. {inventory.assignedSpool.brand ? `${inventory.assignedSpool.brand} ` : ''}
  345. {inventory.assignedSpool.material}
  346. {inventory.assignedSpool.color_name ? ` - ${inventory.assignedSpool.color_name}` : ''}
  347. </p>
  348. <span className="text-[10px] font-mono text-bambu-gray shrink-0">#{inventory.assignedSpool.id}</span>
  349. </div>
  350. {(!spoolman?.linkedSpoolId || inventory.assignedSpool!.id !== spoolman.linkedSpoolId) && (
  351. <button
  352. onClick={(e) => {
  353. e.stopPropagation();
  354. navigate(`/inventory?spool=${inventory.assignedSpool!.id}`);
  355. }}
  356. 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"
  357. title={t('inventory.openInInventory')}
  358. >
  359. <Package className="w-3.5 h-3.5" />
  360. {t('inventory.openInInventory')}
  361. </button>
  362. )}
  363. {inventory.onUnassignSpool && (
  364. <button
  365. onClick={(e) => {
  366. e.stopPropagation();
  367. inventory.onUnassignSpool?.();
  368. }}
  369. 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"
  370. >
  371. <Unlink className="w-3.5 h-3.5" />
  372. {t('inventory.unassignSpool')}
  373. </button>
  374. )}
  375. </>
  376. ) : inventory.onAssignSpool ? (
  377. <button
  378. onClick={inventory.isAssigned ? undefined : (e) => {
  379. e.stopPropagation();
  380. inventory.onAssignSpool?.();
  381. }}
  382. disabled={!!inventory.isAssigned}
  383. 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 text-bambu-blue ${
  384. inventory.isAssigned ? 'opacity-50 cursor-not-allowed' : 'hover:bg-bambu-blue/30'
  385. }`}
  386. >
  387. <Package className="w-3.5 h-3.5" />
  388. {t('inventory.assignSpool')}
  389. </button>
  390. ) : null}
  391. </div>
  392. )}
  393. {/* Configure slot section - always show if enabled */}
  394. {configureSlot?.enabled && (
  395. <div className={`${spoolman?.enabled && data.trayUuid ? '' : 'pt-2 mt-2 border-t border-bambu-dark-tertiary'}`}>
  396. <button
  397. onClick={(e) => {
  398. e.stopPropagation();
  399. configureSlot.onConfigure?.();
  400. }}
  401. 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"
  402. title={t('ams.configureSlot')}
  403. >
  404. <Settings2 className="w-3.5 h-3.5" />
  405. {t('ams.configure')}
  406. </button>
  407. </div>
  408. )}
  409. </div>
  410. </div>
  411. {/* Arrow pointer */}
  412. <div
  413. className={`
  414. absolute left-1/2 -translate-x-1/2 w-0 h-0
  415. border-l-[6px] border-l-transparent
  416. border-r-[6px] border-r-transparent
  417. ${position === 'top'
  418. ? 'top-full border-t-[6px] border-t-bambu-dark-tertiary'
  419. : 'bottom-full border-b-[6px] border-b-bambu-dark-tertiary'}
  420. `}
  421. />
  422. </div>,
  423. document.body,
  424. )}
  425. {/* Unlink Confirmation Dialog */}
  426. {showUnlinkConfirm && (
  427. <div className="fixed inset-0 z-[100] flex items-center justify-center" onClick={() => setShowUnlinkConfirm(false)}>
  428. <div className="absolute inset-0 bg-black/60 backdrop-blur-sm" />
  429. <div
  430. className="relative bg-bambu-dark-secondary rounded-lg shadow-xl w-full max-w-sm mx-4 border border-bambu-dark-tertiary"
  431. onClick={(e) => e.stopPropagation()}
  432. >
  433. <div className="p-4 space-y-4">
  434. <div className="space-y-2">
  435. <h3 className="text-base font-semibold text-white">
  436. {t('spoolman.unlinkConfirmTitle')}
  437. </h3>
  438. <p className="text-sm text-bambu-gray">
  439. {t('spoolman.unlinkConfirmMessage')}
  440. </p>
  441. </div>
  442. <div className="flex gap-2">
  443. <button
  444. onClick={() => setShowUnlinkConfirm(false)}
  445. className="flex-1 px-3 py-2 text-sm font-medium rounded transition-colors bg-bambu-dark hover:bg-bambu-dark-tertiary text-white"
  446. >
  447. {t('common.cancel')}
  448. </button>
  449. <button
  450. onClick={() => {
  451. spoolman?.onUnlinkSpool?.();
  452. setShowUnlinkConfirm(false);
  453. }}
  454. className="flex-1 px-3 py-2 text-sm font-medium rounded transition-colors bg-red-500/20 hover:bg-red-500/30 text-red-400"
  455. >
  456. {t('inventory.unassignSpool')}
  457. </button>
  458. </div>
  459. </div>
  460. </div>
  461. </div>
  462. )}
  463. </div>
  464. );
  465. }
  466. interface EmptySlotHoverCardProps {
  467. children: ReactNode;
  468. className?: string;
  469. configureSlot?: ConfigureSlotConfig;
  470. onAssignSpool?: () => void;
  471. // #1322 follow-up: distinguish firmware-confirmed empty (state 9/10) from
  472. // a user reset where the firmware still has a spool registered. "reset"
  473. // surfaces the user-cleared label; undefined / "physical" keeps the
  474. // historical "Empty slot" wording.
  475. kind?: 'physical' | 'reset';
  476. }
  477. export function EmptySlotHoverCard({ children, className = '', configureSlot, onAssignSpool, kind }: EmptySlotHoverCardProps) {
  478. const { t } = useTranslation();
  479. const [isVisible, setIsVisible] = useState(false);
  480. // Screen-space coords for the portaled card — same pattern as
  481. // FilamentHoverCard, see comment there (#1336 follow-up).
  482. const [coords, setCoords] = useState<{ top: number; left: number } | null>(null);
  483. const triggerRef = useRef<HTMLDivElement>(null);
  484. const cardRef = useRef<HTMLDivElement>(null);
  485. const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  486. const handleMouseEnter = () => {
  487. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  488. timeoutRef.current = setTimeout(() => setIsVisible(true), 80);
  489. };
  490. const handleMouseLeave = () => {
  491. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  492. timeoutRef.current = setTimeout(() => setIsVisible(false), 100);
  493. };
  494. useEffect(() => {
  495. return () => {
  496. if (timeoutRef.current) clearTimeout(timeoutRef.current);
  497. };
  498. }, []);
  499. useLayoutEffect(() => {
  500. if (!isVisible) {
  501. setCoords(null);
  502. return;
  503. }
  504. const compute = () => {
  505. if (!triggerRef.current || !cardRef.current) return;
  506. const triggerRect = triggerRef.current.getBoundingClientRect();
  507. const cardHeight = cardRef.current.offsetHeight;
  508. const cardWidth = cardRef.current.offsetWidth;
  509. const centerX = triggerRect.left + triggerRect.width / 2;
  510. const left = Math.max(8, Math.min(centerX - cardWidth / 2, window.innerWidth - cardWidth - 8));
  511. const top = triggerRect.top - cardHeight - 8;
  512. setCoords({ top, left });
  513. };
  514. compute();
  515. const rafId = requestAnimationFrame(compute);
  516. window.addEventListener('scroll', compute, true);
  517. window.addEventListener('resize', compute);
  518. return () => {
  519. cancelAnimationFrame(rafId);
  520. window.removeEventListener('scroll', compute, true);
  521. window.removeEventListener('resize', compute);
  522. };
  523. }, [isVisible]);
  524. return (
  525. <div
  526. ref={triggerRef}
  527. className={`relative ${className}`}
  528. onMouseEnter={handleMouseEnter}
  529. onMouseLeave={handleMouseLeave}
  530. >
  531. {children}
  532. {isVisible && createPortal(
  533. <div
  534. ref={cardRef}
  535. className="fixed z-[60] animate-in fade-in-0 zoom-in-95 duration-150"
  536. style={{
  537. top: coords?.top ?? -9999,
  538. left: coords?.left ?? -9999,
  539. visibility: coords ? 'visible' : 'hidden',
  540. }}
  541. onMouseEnter={handleMouseEnter}
  542. onMouseLeave={handleMouseLeave}
  543. >
  544. <div className="
  545. bg-bambu-dark-secondary border border-bambu-dark-tertiary
  546. rounded-md shadow-lg overflow-hidden
  547. ">
  548. <div className="px-3 py-1.5 text-xs text-bambu-gray whitespace-nowrap">
  549. {kind === 'reset' ? t('ams.emptySlotReset') : t('ams.emptySlot')}
  550. </div>
  551. {/* Configure slot button */}
  552. {(configureSlot?.enabled || onAssignSpool) && (
  553. <div className="px-2 pb-2 space-y-1">
  554. {configureSlot?.enabled && (
  555. <button
  556. onClick={(e) => {
  557. e.stopPropagation();
  558. configureSlot.onConfigure?.();
  559. }}
  560. 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"
  561. title={t('ams.configureSlot')}
  562. >
  563. <Settings2 className="w-3.5 h-3.5" />
  564. {t('ams.configure')}
  565. </button>
  566. )}
  567. {onAssignSpool && (
  568. <button
  569. onClick={(e) => { e.stopPropagation(); onAssignSpool(); }}
  570. 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"
  571. >
  572. <Package className="w-3.5 h-3.5" />
  573. {t('inventory.assignSpool')}
  574. </button>
  575. )}
  576. </div>
  577. )}
  578. </div>
  579. <div className="
  580. absolute left-1/2 -translate-x-1/2 top-full w-0 h-0
  581. border-l-[5px] border-l-transparent
  582. border-r-[5px] border-r-transparent
  583. border-t-[5px] border-t-bambu-dark-tertiary
  584. " />
  585. </div>,
  586. document.body,
  587. )}
  588. </div>
  589. );
  590. }