FilamentSlotCircle.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * FilamentSlotCircle renders a small color circle with the 1-based slot
  3. * number centered inside, matching the style used on AMS cards in PrintersPage.
  4. *
  5. * Props:
  6. * trayColor - 6-char hex color string WITHOUT leading '#' (e.g. "FF0000").
  7. * Pass undefined / empty string when the slot is empty.
  8. * trayType - Filament material string (e.g. "PLA"). Used to decide the
  9. * fallback background when there is no color but a type is known.
  10. * isEmpty - Whether the slot contains no filament.
  11. * emptyKind - Optional refinement of the empty state used to render the
  12. * slot border (#1322 follow-up): "physical" for firmware-
  13. * confirmed no spool (state 9/10), "reset" for slots where
  14. * the user cleared the assignment but the firmware hasn't
  15. * positively confirmed emptiness. Ignored when isEmpty is false.
  16. * slotNumber - 1-based slot number to display inside the circle.
  17. */
  18. interface FilamentSlotCircleProps {
  19. trayColor?: string | null;
  20. trayType?: string | null;
  21. isEmpty: boolean;
  22. emptyKind?: 'physical' | 'reset' | null;
  23. slotNumber: number;
  24. }
  25. function isLightFilamentColor(hex: string): boolean {
  26. if (!hex || hex.length < 6) return false;
  27. const r = parseInt(hex.slice(0, 2), 16);
  28. const g = parseInt(hex.slice(2, 4), 16);
  29. const b = parseInt(hex.slice(4, 6), 16);
  30. return (0.299 * r + 0.587 * g + 0.114 * b) / 255 > 0.6;
  31. }
  32. export function FilamentSlotCircle({ trayColor, trayType, isEmpty, emptyKind, slotNumber }: FilamentSlotCircleProps) {
  33. // Reset slots get a quieter border than physical-empty so they read as
  34. // "cleared but possibly still has a spool the firmware hasn't confirmed
  35. // gone" rather than "definitely no spool".
  36. const emptyBorderColor = emptyKind === 'reset' ? '#3d3d3d' : '#666';
  37. return (
  38. <div
  39. className="w-3.5 h-3.5 rounded-full mx-auto mb-0.5 border-2 flex items-center justify-center"
  40. style={{
  41. backgroundColor: trayColor ? `#${trayColor}` : (trayType ? '#333' : 'transparent'),
  42. borderColor: isEmpty ? emptyBorderColor : 'rgba(255,255,255,0.1)',
  43. borderStyle: isEmpty ? 'dashed' : 'solid',
  44. }}
  45. >
  46. <span
  47. className="text-[6px] font-bold leading-none select-none"
  48. style={{ color: trayColor && isLightFilamentColor(trayColor) ? '#000' : '#fff' }}
  49. >
  50. {slotNumber}
  51. </span>
  52. </div>
  53. );
  54. }