FilamentSlotCircle.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. Accepts
  17. * a string for non-numeric labels (e.g. "L" / "R" for the
  18. * dual-nozzle external trays, where carrying a separate
  19. * Ext-L/Ext-R caption underneath made the row taller).
  20. */
  21. interface FilamentSlotCircleProps {
  22. trayColor?: string | null;
  23. trayType?: string | null;
  24. isEmpty: boolean;
  25. emptyKind?: 'physical' | 'reset' | null;
  26. slotNumber: number | string;
  27. }
  28. function isLightFilamentColor(hex: string): boolean {
  29. if (!hex || hex.length < 6) return false;
  30. const r = parseInt(hex.slice(0, 2), 16);
  31. const g = parseInt(hex.slice(2, 4), 16);
  32. const b = parseInt(hex.slice(4, 6), 16);
  33. return (0.299 * r + 0.587 * g + 0.114 * b) / 255 > 0.6;
  34. }
  35. export function FilamentSlotCircle({ trayColor, trayType, isEmpty, emptyKind, slotNumber }: FilamentSlotCircleProps) {
  36. // Reset slots get a quieter border than physical-empty so they read as
  37. // "cleared but possibly still has a spool the firmware hasn't confirmed
  38. // gone" rather than "definitely no spool".
  39. const emptyBorderColor = emptyKind === 'reset' ? '#3d3d3d' : '#666';
  40. return (
  41. <div
  42. className="w-3.5 h-3.5 rounded-full mx-auto mb-0.5 border-2 flex items-center justify-center"
  43. style={{
  44. backgroundColor: trayColor ? `#${trayColor}` : (trayType ? '#333' : 'transparent'),
  45. borderColor: isEmpty ? emptyBorderColor : 'rgba(255,255,255,0.1)',
  46. borderStyle: isEmpty ? 'dashed' : 'solid',
  47. }}
  48. >
  49. <span
  50. className="text-[6px] font-bold leading-none select-none"
  51. style={{ color: trayColor && isLightFilamentColor(trayColor) ? '#000' : '#fff' }}
  52. >
  53. {slotNumber}
  54. </span>
  55. </div>
  56. );
  57. }