From 7430251adf757196313455a296e625c572d454cb Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Mon, 26 Oct 2026 22:21:10 -0400 Subject: [PATCH] feat(awards): render ribbon devices as vectors with tier-driven auto-layout Introduce RibbonArt, a single shared SVG renderer used by both the production viewer and admin/studio preview so they can no longer diverge. Devices are now vector glyphs (star/oak leaf/campaign clasp/arrow up) instead of Unicode text, laid out as one centered cluster that shrinks to fit. Device count derives from advancement tier using the service-star convention (tier N shows N-1, cap 6; bare ribbon at tier 1 or for non-tiered awards). Legacy per-device x/y positions are stored but ignored, so no schema change. Designers add/remove/reorder device types and preview tiers. --- .../admin/awards/RibbonDesigner.tsx | 2 +- .../awards/ribbonDesign/RibbonArt.tsx | 211 ++++++++++++++++++ src/components/awards/ribbonDesign/parts.tsx | 184 +++++++-------- src/components/frontend/account/AwardCard.tsx | 1 + .../frontend/account/AwardListingCard.tsx | 1 + .../frontend/account/AwardPickerDialog.tsx | 6 +- .../frontend/account/RibbonRack.tsx | 2 +- .../frontend/account/RibbonViewer.tsx | 77 ++----- .../frontend/awards/RibbonStudio.tsx | 2 +- tests/int/ribbon-devices.int.spec.ts | 147 ++++++++++++ 10 files changed, 485 insertions(+), 148 deletions(-) create mode 100644 src/components/awards/ribbonDesign/RibbonArt.tsx create mode 100644 tests/int/ribbon-devices.int.spec.ts diff --git a/src/components/admin/awards/RibbonDesigner.tsx b/src/components/admin/awards/RibbonDesigner.tsx index 86bac4ff..d44c101e 100644 --- a/src/components/admin/awards/RibbonDesigner.tsx +++ b/src/components/admin/awards/RibbonDesigner.tsx @@ -52,7 +52,7 @@ export default function RibbonDesigner() { Configure the ribbon colors, stripes, and device attachments for this award.

- + ))} @@ -133,6 +131,12 @@ export function StripeEditor({ // ─── Device Editor ────────────────────────────────────────────────── +/** + * Devices are added/removed as an ordered set; their placement on the ribbon + * is computed automatically (centered cluster, tier-driven count when the + * award belongs to an advancement series). The legacy per-device X/Y sliders + * are gone because layout is derived, and stored positions are ignored. + */ export function DeviceEditor({ devices, onChange, @@ -143,12 +147,21 @@ export function DeviceEditor({ const addDevice = (type: DeviceType) => onChange([...devices, { id: crypto.randomUUID(), type, position: 50, positionY: 50 }]); const removeDevice = (id: string) => onChange(devices.filter((d) => d.id !== id)); - const updateDevice = (id: string, patch: Partial) => - onChange(devices.map((d) => (d.id === id ? { ...d, ...patch } : d))); + const moveDevice = (idx: number, delta: number) => { + const next = [...devices]; + const target = idx + delta; + if (target < 0 || target >= next.length) return; + [next[idx], next[target]] = [next[target], next[idx]]; + onChange(next); + }; return (
Devices +

+ Devices arrange themselves centered on the ribbon. For awards in an + advancement series, the tier sets how many show (tier N displays N - 1). +

{(Object.keys(RIBBON_DEVICE_INFO) as DeviceType[]).map((type) => { const info = RIBBON_DEVICE_INFO[type]; @@ -157,58 +170,51 @@ export function DeviceEditor({ key={type} type="button" onClick={() => addDevice(type)} - className="text-[11px] px-2 py-1 rounded bg-zinc-800 border border-zinc-700 text-zinc-300 hover:bg-zinc-700 transition-colors cursor-pointer" + className="text-[11px] px-2 py-1 rounded bg-zinc-800 border border-zinc-700 text-zinc-300 hover:bg-zinc-700 transition-colors cursor-pointer inline-flex items-center gap-1.5 text-yellow-400 [&_span]:text-zinc-300" > - {info.symbol} {info.label} + {info.label} ); })}
{devices.length > 0 && (
- {devices.map((device) => { + {devices.map((device, idx) => { const info = RIBBON_DEVICE_INFO[device.type]; return (
- {info.symbol} + {idx + 1} + {info.label} -
- - updateDevice(device.id, { position: Number(e.target.value) })} - className="flex-1 h-1 accent-zinc-400" - /> - - {device.position} - -
-
- - updateDevice(device.id, { positionY: Number(e.target.value) })} - className="flex-1 h-1 accent-zinc-400" - /> - - {device.positionY} - -
+
+ +
); diff --git a/src/components/frontend/account/AwardCard.tsx b/src/components/frontend/account/AwardCard.tsx index d4f007f0..168c9367 100644 --- a/src/components/frontend/account/AwardCard.tsx +++ b/src/components/frontend/account/AwardCard.tsx @@ -71,6 +71,7 @@ export default function AwardCard(props: { )}
diff --git a/src/components/frontend/account/AwardListingCard.tsx b/src/components/frontend/account/AwardListingCard.tsx index 31d4f8f2..215892a3 100644 --- a/src/components/frontend/account/AwardListingCard.tsx +++ b/src/components/frontend/account/AwardListingCard.tsx @@ -87,6 +87,7 @@ export function AwardListingCard({
) : awardImage?.url ? ( diff --git a/src/components/frontend/account/AwardPickerDialog.tsx b/src/components/frontend/account/AwardPickerDialog.tsx index e8781700..38236f1d 100644 --- a/src/components/frontend/account/AwardPickerDialog.tsx +++ b/src/components/frontend/account/AwardPickerDialog.tsx @@ -75,7 +75,11 @@ export function AwardPickerDialog({ {award.type === "ribbon" && award.ribbonDesign ? (
- +
) : ( diff --git a/src/components/frontend/account/RibbonRack.tsx b/src/components/frontend/account/RibbonRack.tsx index 4ab9946c..fb7b32b8 100644 --- a/src/components/frontend/account/RibbonRack.tsx +++ b/src/components/frontend/account/RibbonRack.tsx @@ -63,7 +63,7 @@ export const RibbonRack = ({ awards }: { awards: Award[] }) => { return ( - +
diff --git a/src/components/frontend/account/RibbonViewer.tsx b/src/components/frontend/account/RibbonViewer.tsx index ace27988..5f9f781f 100644 --- a/src/components/frontend/account/RibbonViewer.tsx +++ b/src/components/frontend/account/RibbonViewer.tsx @@ -1,15 +1,26 @@ "use client"; -import { - RIBBON_DEVICE_INFO, - RIBBON_CONSTANTS, - DEFAULT_RIBBON, -} from "@/components/awards/ribbonDesign/constants"; +import { DEFAULT_RIBBON } from "@/components/awards/ribbonDesign/constants"; +import { RibbonArt } from "@/components/awards/ribbonDesign/RibbonArt"; import type { RibbonDesign } from "@/components/awards/ribbonDesign/types"; import { useState } from "react"; import { cn } from "@/lib/utils"; -export const RibbonViewer = ({ ribbonDesign, showRibbonOnly }: { ribbonDesign: RibbonDesign, showRibbonOnly?: boolean }) => { +/** + * Production ribbon renderer. Thin wrapper around the shared `RibbonArt` so + * the live site and the admin/studio previews cannot drift apart. Pass an + * award's `advancement.tier` to let the device cluster derive its count from + * the tier (service-star convention); omit it to show the authored devices. + */ +export const RibbonViewer = ({ + ribbonDesign, + showRibbonOnly, + tier, +}: { + ribbonDesign: RibbonDesign; + showRibbonOnly?: boolean; + tier?: number | null; +}) => { const initial: RibbonDesign = ribbonDesign && typeof ribbonDesign === "object" && Array.isArray((ribbonDesign as RibbonDesign).stripes) ? (ribbonDesign as RibbonDesign) @@ -17,57 +28,13 @@ export const RibbonViewer = ({ ribbonDesign, showRibbonOnly }: { ribbonDesign: R const [design] = useState(initial); - const { stripes, devices, mirrored } = design; - if (stripes.length === 0) return null; - - const displayStripes = mirrored ? [...stripes, ...stripes.slice(0, -1).reverse()] : stripes; - - const totalWeight = displayStripes.reduce((sum, s) => sum + s.width, 0); - const viewWidth = RIBBON_CONSTANTS.RIBBON_WIDTH; - const viewHeight = RIBBON_CONSTANTS.PREVIEW_HEIGHT; - return ( - - - {(() => { - let x = 0; - return displayStripes.map((stripe, i) => { - const w = (stripe.width / totalWeight) * viewWidth; - const el = ; - x += w; - return el; - }); - })()} - - - - {devices.map((device) => { - const info = RIBBON_DEVICE_INFO[device.type]; - const cx = (device.position / 100) * viewWidth; - const cy = (device.positionY / 100) * viewHeight; - const fontSize = Math.min(viewHeight * 0.45, 24); - return ( - - {info.symbol} - - ); + "w-full rounded border border-zinc-700 bg-zinc-950": !showRibbonOnly, })} - + /> ); }; diff --git a/src/components/frontend/awards/RibbonStudio.tsx b/src/components/frontend/awards/RibbonStudio.tsx index d9dddd91..58a6be62 100644 --- a/src/components/frontend/awards/RibbonStudio.tsx +++ b/src/components/frontend/awards/RibbonStudio.tsx @@ -114,7 +114,7 @@ export function RibbonStudio() {
- +
({ + stripes: [ + { color: "#b91c1c", width: 50 }, + { color: "#1e3a5f", width: 50 }, + ], + devices: deviceTypes.map((type, i) => ({ + id: `dev-${i}`, + // Cast to satisfy the union; tests only use valid types or assert fallback. + type: type as RibbonDesign["devices"][number]["type"], + position: 17 + i * 11, + positionY: 83, + })), + mirrored: false, +}); + +describe("resolveDisplayedDeviceTypes (tier-driven count)", () => { + const authored = ["star", "oakLeaf"] as const; + + it("shows exactly the authored set for base, missing, and invalid tiers", () => { + for (const tier of [undefined, null, 1, 0, -2, Number.NaN]) { + expect(resolveDisplayedDeviceTypes(design([...authored]), tier)).toEqual(["star", "oakLeaf"]); + } + }); + + it("maps tier N to N-1 devices using the service-star convention", () => { + const d = design([...authored]); + expect(resolveDisplayedDeviceTypes(d, 2)).toEqual(["star"]); + expect(resolveDisplayedDeviceTypes(d, 3)).toEqual(["star", "oakLeaf"]); + expect(resolveDisplayedDeviceTypes(d, 4)).toEqual(["star", "oakLeaf", "star"]); + }); + + it("caps the derived cluster at MAX_TIER_DEVICES", () => { + const result = resolveDisplayedDeviceTypes(design([...authored]), 99); + expect(result).toHaveLength(MAX_TIER_DEVICES); + }); + + it("defaults to stars when a tiered series authored no devices", () => { + const result = resolveDisplayedDeviceTypes(design([]), 3); + expect(result).toEqual(["star", "star"]); + }); +}); + +describe("computeDeviceSlots (centered auto-layout)", () => { + const W = 366; + const H = 80; + + it("returns no slots for zero/negative counts", () => { + expect(computeDeviceSlots(0, W, H)).toEqual([]); + expect(computeDeviceSlots(-3, W, H)).toEqual([]); + }); + + it("centers a single device on the ribbon face", () => { + const [slot] = computeDeviceSlots(1, W, H); + expect(slot.cx).toBeCloseTo(W / 2, 6); + expect(slot.cy).toBeCloseTo(H / 2, 6); + expect(slot.size).toBeGreaterThan(0); + }); + + it("places N devices as an evenly spaced, centered cluster", () => { + const slots = computeDeviceSlots(4, W, H); + expect(slots).toHaveLength(4); + + const spacings = slots.slice(1).map((s, i) => s.cx - slots[i].cx); + spacings.forEach((gap) => expect(gap).toBeCloseTo(spacings[0], 6)); + + // Symmetric about the centerline. + const meanCx = slots.reduce((sum, s) => sum + s.cx, 0) / slots.length; + expect(meanCx).toBeCloseTo(W / 2, 6); + + // All vertically centered. + for (const slot of slots) expect(slot.cy).toBeCloseTo(H / 2, 6); + }); + + it("shrinks and keeps the cluster inside the face when crowded", () => { + // A tall, narrow canvas makes ideal sizing overflow at high counts, which + // exercises the uniform shrink path (compare like-for-like canvas). + const NARROW_W = 100; + const TALL_H = 200; + const few = computeDeviceSlots(1, NARROW_W, TALL_H); + const many = computeDeviceSlots(MAX_TIER_DEVICES, NARROW_W, TALL_H); + expect(many[many.length - 1].size).toBeLessThan(few[0].size); + + const span = + many[many.length - 1].cx + many[many.length - 1].size / 2 - (many[0].cx - many[0].size / 2); + expect(span).toBeLessThanOrEqual(NARROW_W * 0.8 + 1e-6); + }); +}); + +describe("RibbonArt rendering", () => { + it("renders vector device groups (no Unicode text elements) with tier counts", () => { + const { container } = render(h(RibbonArt, { design: design(["star", "oakLeaf"]), tier: 3 })); + const devices = container.querySelectorAll("[data-device]"); + expect(devices).toHaveLength(2); + expect(Array.from(devices).map((d) => d.getAttribute("data-device"))).toEqual([ + "star", + "oakLeaf", + ]); + // Glyphs are inline paths now; no nodes and no emoji fallbacks. + expect(container.querySelectorAll("text")).toHaveLength(0); + expect(container.querySelectorAll("path").length).toBeGreaterThan(0); + }); + + it("keeps preview identical to production by drawing the fabric overlay once", () => { + const { container } = render(h(RibbonArt, { design: design([]), tier: 1 })); + expect(container.querySelectorAll("image[href='/images/ribbon-overlay.png']")).toHaveLength(1); + }); + + it("drops the overlay when showOverlay is false", () => { + const { container } = render(h(RibbonArt, { design: design([]), showOverlay: false })); + expect(container.querySelector("image")).toBeNull(); + }); + + it("renders mirrored designs with stripes doubled", () => { + const mirrored: RibbonDesign = { ...design(["star"]), mirrored: true }; + const { container } = render(h(RibbonArt, { design: mirrored })); + // mirrored doubles every stripe band; two colors -> four rects over base bg + const rects = container.querySelectorAll("rect"); + expect(rects.length).toBeGreaterThanOrEqual(4); + }); + + it("returns null for an explicitly stripe-less design and falls back for an absent one", () => { + // Matches legacy RibbonViewer/RibbonPreview behavior: a valid object with + // no stripes draws nothing... + const { container: empty } = render( + h(RibbonArt, { design: { stripes: [], devices: [], mirrored: false } }), + ); + expect(empty.querySelector("svg")).toBeNull(); + + // ...while an unusable value falls back to DEFAULT_RIBBON and still renders. + const { container: fallback } = render(h(RibbonArt, { design: undefined })); + expect(fallback.querySelector("svg")).not.toBeNull(); + }); +});