1
0
Fork 0
polaris-task-force/src/components/awards/ribbonDesign/parts.tsx
Z8MB1E e92580bbe4 feat(ribbon): add stripe direction toggle and line editor to builders
Shared LineEditor (color, position, thickness sliders) and a vertical /
horizontal segmented control in RibbonStudio, plus an equivalent
orientation button and the same editor in the admin RibbonDesigner.
Review-queue previews pick both features up through RibbonArt.
2026-12-01 09:29:14 -05:00

313 lines
13 KiB
TypeScript

"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 (
<div className="flex flex-col gap-2">
<RibbonArt
design={design}
tier={effectiveTier}
className="w-full h-auto rounded border border-zinc-700 bg-zinc-950"
maxHeight={140}
/>
{allowTierPreview && (
<label className="flex items-center gap-2 text-[11px] text-zinc-400">
<span className="uppercase tracking-wider">Preview tier</span>
<input
type="range"
min={1}
max={MAX_TIER_DEVICES + 1}
value={simulatedTier}
onChange={(e) => setSimulatedTier(Number(e.target.value))}
className="flex-1 h-1 accent-amber-500"
/>
<span className="font-mono w-16 text-right">
{simulatedTier === 1
? "as authored"
: `${Math.min(simulatedTier - 1, MAX_TIER_DEVICES)} devices`}
</span>
</label>
)}
</div>
);
}
// ─── 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<Stripe>) =>
onChange(stripes.map((s, i) => (i === idx ? { ...s, ...patch } : s)));
return (
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<span className="text-xs font-medium text-zinc-400 uppercase tracking-wider">Stripes</span>
<button
type="button"
onClick={addStripe}
className="text-[11px] px-2 py-0.5 rounded bg-emerald-950 border border-emerald-700 text-emerald-200 hover:bg-emerald-900 transition-colors cursor-pointer"
>
+ Add Stripe
</button>
</div>
{stripes.map((stripe, idx) => (
<div key={idx} className="flex flex-wrap items-center gap-2 bg-zinc-900 rounded px-2 py-1.5">
<input
type="color"
value={stripe.color}
onChange={(e) => updateStripe(idx, { color: e.target.value })}
className="size-7 rounded cursor-pointer bg-transparent border-0 p-0"
/>
<input
type="text"
value={stripe.color}
onChange={(e) => updateStripe(idx, { color: e.target.value })}
className="text-[11px] font-mono bg-zinc-800 border border-zinc-700 rounded px-2 py-1 w-20 text-zinc-300"
/>
<div className="flex items-center gap-1.5 flex-1">
<label className="text-[10px] text-zinc-500 shrink-0">%</label>
<input
type="range"
min={1}
max={100}
value={stripe.width}
onChange={(e) => updateStripe(idx, { width: Number(e.target.value) })}
className="flex-1 h-1 accent-zinc-400"
/>
<span className="text-[10px] text-zinc-500 font-mono w-8 text-right">
{totalWeight > 0 ? Math.round((stripe.width / totalWeight) * 100) : 0}%
</span>
</div>
<div
className="size-5 rounded shrink-0 border border-zinc-700"
style={{ backgroundColor: stripe.color }}
/>
<button
type="button"
onClick={() => removeStripe(idx)}
className="py-1 text-zinc-500 hover:text-red-400 text-sm cursor-pointer"
>
{"\u00D7"}
</button>
</div>
))}
</div>
);
}
// ─── 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<RibbonLine>) =>
onChange(lines.map((l, i) => (i === idx ? { ...l, ...patch } : l)));
return (
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<span className="text-xs font-medium text-zinc-400 uppercase tracking-wider">Horizontal lines</span>
<button
type="button"
onClick={addLine}
disabled={lines.length >= MAX_DESIGN_LINES}
className="text-[11px] px-2 py-0.5 rounded bg-emerald-950 border border-emerald-700 text-emerald-200 hover:bg-emerald-900 transition-colors cursor-pointer disabled:opacity-40 disabled:cursor-not-allowed"
>
+ Add line
</button>
</div>
<p className="text-[10px] text-zinc-500">
Thin overlay bars across the stripes, drawn under devices. Up to {MAX_DESIGN_LINES}.
</p>
{lines.map((line, idx) => (
<div key={line.id} className="flex flex-wrap items-center gap-2 bg-zinc-800/60 rounded px-2 py-1.5">
<input
type="color"
value={line.color}
onChange={(e) => updateLine(idx, { color: e.target.value })}
className="h-6 w-8 cursor-pointer rounded border border-zinc-700 bg-transparent"
aria-label={`Line ${idx + 1} color`}
/>
<div className="flex min-w-0 flex-1 items-center gap-1.5">
<label className="text-[10px] text-zinc-500 uppercase" htmlFor={`line-pos-${line.id}`}>
Pos
</label>
<input
id={`line-pos-${line.id}`}
type="range"
min={0}
max={100}
value={line.positionY}
onChange={(e) => updateLine(idx, { positionY: Number(e.target.value) })}
className="h-1 flex-1 accent-amber-500"
/>
<span className="w-8 text-right font-mono text-[10px] text-zinc-400">{line.positionY}%</span>
</div>
<div className="flex min-w-0 flex-1 items-center gap-1.5">
<label className="text-[10px] text-zinc-500 uppercase" htmlFor={`line-size-${line.id}`}>
Size
</label>
<input
id={`line-size-${line.id}`}
type="range"
min={1}
max={100}
value={line.thickness}
onChange={(e) => updateLine(idx, { thickness: Number(e.target.value) })}
className="h-1 flex-1 accent-amber-500"
/>
<span className="w-8 text-right font-mono text-[10px] text-zinc-400">{line.thickness}%</span>
</div>
<button
type="button"
onClick={() => removeLine(line.id)}
className="px-1 py-1 text-zinc-500 hover:text-red-400 text-sm cursor-pointer"
aria-label={`Remove line ${idx + 1}`}
>
{"\u00D7"}
</button>
</div>
))}
</div>
);
}
// ─── 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 (
<div className="flex flex-col gap-2">
<span className="text-xs font-medium text-zinc-400 uppercase tracking-wider">Devices</span>
<p className="text-[10px] text-zinc-500">
Devices arrange themselves centered on the ribbon. For awards in an
advancement series, the tier sets how many show (tier N displays N&nbsp;-&nbsp;1).
</p>
<div className="flex flex-wrap gap-1.5">
{(Object.keys(RIBBON_DEVICE_INFO) as DeviceType[]).map((type) => {
const info = RIBBON_DEVICE_INFO[type];
return (
<button
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 inline-flex items-center gap-1.5 text-yellow-400 [&_span]:text-zinc-300"
>
<DeviceGlyph type={type} px={12} /> <span>{info.label}</span>
</button>
);
})}
</div>
{devices.length > 0 && (
<div className="flex flex-col gap-1.5">
{devices.map((device, idx) => {
const info = RIBBON_DEVICE_INFO[device.type];
return (
<div
key={device.id}
className="flex items-center gap-2 bg-zinc-900 rounded px-2 py-1.5 text-yellow-400"
>
<span className="font-mono text-[10px] text-zinc-500 w-4">{idx + 1}</span>
<DeviceGlyph type={device.type} px={14} />
<span className="text-[11px] text-zinc-400">{info.label}</span>
<div className="flex-1" />
<button
type="button"
aria-label={`Move ${info.label} earlier`}
onClick={() => moveDevice(idx, -1)}
disabled={idx === 0}
className="px-1 text-zinc-500 hover:text-zinc-200 disabled:opacity-30 cursor-pointer"
>
↑
</button>
<button
type="button"
aria-label={`Move ${info.label} later`}
onClick={() => moveDevice(idx, 1)}
disabled={idx === devices.length - 1}
className="px-1 text-zinc-500 hover:text-zinc-200 disabled:opacity-30 cursor-pointer"
>
↓
</button>
<button
type="button"
aria-label={`Remove ${info.label}`}
onClick={() => removeDevice(device.id)}
className="px-1 py-1 text-zinc-500 hover:text-red-400 text-sm cursor-pointer"
>
{"\u00D7"}
</button>
</div>
);
})}
</div>
)}
</div>
);
}