"use client";
import { useState } from "react";
import { RIBBON_DEVICE_INFO } from "./constants";
import { DeviceGlyph, MAX_TIER_DEVICES, RibbonArt } from "./RibbonArt";
import type { Device, DeviceType, RibbonDesign, RibbonLine, Stripe } from "./types";
// ─── Preview ────────────────────────────────────────────────────────
/**
* Design-time ribbon preview: the same `RibbonArt` used on the live site, so
* authors see exactly what grantees get (devices included, auto-laid-out).
* Pass `tier` to preview one fixed tier. With `allowTierPreview`, a small
* selector simulates tiers 1..MAX+1: tier 1 renders the authored device set,
* each higher tier derives its cluster count (N - 1 devices), matching the
* service-star convention applied at render time.
*/
export function RibbonPreview({
design,
tier,
allowTierPreview,
}: {
design: RibbonDesign;
tier?: number | null;
allowTierPreview?: boolean;
}) {
const [simulatedTier, setSimulatedTier] = useState(1);
const effectiveTier = allowTierPreview ? simulatedTier : tier;
return (
{allowTierPreview && (
)}
);
}
// ─── Stripe Editor ──────────────────────────────────────────────────
export function StripeEditor({
stripes,
onChange,
}: {
stripes: Stripe[];
onChange: (s: Stripe[]) => void;
}) {
const totalWeight = stripes.reduce((sum, s) => sum + s.width, 0);
const addStripe = () => onChange([...stripes, { color: "#f5f5f4", width: 10 }]);
const removeStripe = (idx: number) => onChange(stripes.filter((_, i) => i !== idx));
const updateStripe = (idx: number, patch: Partial) =>
onChange(stripes.map((s, i) => (i === idx ? { ...s, ...patch } : s)));
return (
Stripes
{stripes.map((stripe, idx) => (
))}
);
}
// ─── Accent line editor ─────────────────────────────────────────────
/** The overlay-line cap enforced by ribbonValidation; keeps the add button honest. */
export const MAX_DESIGN_LINES = 5;
/**
* Edit the horizontal accent lines drawn over the stripe pattern (independent
* of stripe orientation). Each row picks a color, a vertical center position,
* and a thickness, all as percentages of ribbon height so the preview stays
* faithful at any scale.
*/
export function LineEditor({ lines, onChange }: { lines: RibbonLine[]; onChange: (l: RibbonLine[]) => void }) {
const addLine = () =>
onChange([...lines, { id: crypto.randomUUID(), color: "#ca8a04", positionY: 50, thickness: 6 }]);
const removeLine = (id: string) => onChange(lines.filter((line) => line.id !== id));
const updateLine = (idx: number, patch: Partial) =>
onChange(lines.map((l, i) => (i === idx ? { ...l, ...patch } : l)));
return (
Horizontal lines
Thin overlay bars across the stripes, drawn under devices. Up to {MAX_DESIGN_LINES}.
{lines.map((line, idx) => (
))}
);
}
// ─── 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,
}: {
devices: Device[];
onChange: (d: Device[]) => void;
}) {
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 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];
return (
);
})}
{devices.length > 0 && (
{devices.map((device, idx) => {
const info = RIBBON_DEVICE_INFO[device.type];
return (
{idx + 1}
{info.label}
);
})}
)}
);
}