import type { CSSProperties } from "react"; /** * Frontend-safe SVG renderer for a medal's art. Mirrors the admin * `MedalDesigner` preview so the award listing page can show medal art * without pulling in Payload admin UI dependencies. Self-contained — the * shape/finish constants intentionally duplicate the admin module so this * file stays client- and server-safe. */ export type MedalShape = "circle" | "star" | "cross" | "shield"; export type MetalFinish = "gold" | "silver" | "bronze" | "copper" | "darkSteel"; export interface MedalDesign { ribbonStripes: { color: string; width: number }[]; shape: MedalShape; finish: MetalFinish; engraving?: string; } const FINISH_INFO: Record = { gold: { colors: ["#fbbf24", "#ca8a04", "#92400e"] }, silver: { colors: ["#e5e7eb", "#9ca3af", "#6b7280"] }, bronze: { colors: ["#d97706", "#92400e", "#78350f"] }, copper: { colors: ["#f97316", "#c2410c", "#7c2d12"] }, darkSteel: { colors: ["#6b7280", "#4b5563", "#374151"] }, }; const RIBBON_WIDTH = 366; function isMedalDesign(value: unknown): value is MedalDesign { return ( !!value && typeof value === "object" && Array.isArray((value as MedalDesign).ribbonStripes) && ((value as MedalDesign).ribbonStripes as unknown[]).length > 0 ); } function MedalBody({ shape, finish, engraving, cx, cy, }: { shape: MedalShape; finish: MetalFinish; engraving: string; cx: number; cy: number; }) { const finishColors = FINISH_INFO[finish].colors; const gradId = `medal-grad-${finish}`; const r = 40; return ( {shape === "circle" && ( )} {shape === "star" && ( { const angle = Math.PI / 2 + (i * Math.PI) / 5; const radius = i % 2 === 0 ? r : r * 0.45; return `${cx + radius * Math.cos(angle)},${cy - radius * Math.sin(angle)}`; }).join(" ")} fill={`url(#${gradId})`} stroke={finishColors[2]} strokeWidth={1.5} /> )} {shape === "cross" && ( )} {shape === "shield" && ( )} {engraving && ( 10 ? 6 : 8} fontFamily="serif" fontWeight="bold" letterSpacing={0.5} > {engraving.toUpperCase()} )} ); } export function MedalViewer({ medalDesign, className, style, }: { medalDesign: MedalDesign | unknown; className?: string; style?: CSSProperties; }) { const design = isMedalDesign(medalDesign) ? medalDesign : null; if (!design) return null; const { ribbonStripes, shape, finish, engraving = "" } = design; const totalWeight = ribbonStripes.reduce((s, stripe) => s + stripe.width, 0); const viewWidth = RIBBON_WIDTH; const ribbonTop = 14; const ribbonBottom = 72; const convergeRatio = 0.35; const medalCx = viewWidth / 2; const medalCy = 120; const ribbonViewWidth = RIBBON_WIDTH; const offset = 0; return ( {(() => { const topStartX = offset; const bottomCenter = medalCx; const topWidth = ribbonViewWidth; const bottomWidth = topWidth * convergeRatio; let topX = topStartX; return ribbonStripes.map((stripe, i) => { const stripeW = (stripe.width / totalWeight) * ribbonViewWidth; const fracStart = (topX - topStartX) / topWidth; const fracEnd = (topX + stripeW - topStartX) / topWidth; const tlx = topX; const trx = topX + stripeW; const blx = bottomCenter - bottomWidth / 2 + fracStart * bottomWidth; const brx = bottomCenter - bottomWidth / 2 + fracEnd * bottomWidth; topX += stripeW; return ( ); }); })()} {/* Small connector tab between ribbon and medal */} {/* Medal body */} ); }