1
0
Fork 0
polaris-task-force/src/components/frontend/account/MedalViewer.tsx

214 lines
5.9 KiB
TypeScript

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<MetalFinish, { colors: string[] }> = {
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 (
<g>
<defs>
<radialGradient id={gradId} cx="40%" cy="35%">
<stop offset="0%" stopColor={finishColors[0]} />
<stop offset="60%" stopColor={finishColors[1]} />
<stop offset="100%" stopColor={finishColors[2]} />
</radialGradient>
</defs>
{shape === "circle" && (
<circle
cx={cx}
cy={cy}
r={r}
fill={`url(#${gradId})`}
stroke={finishColors[2]}
strokeWidth={1.5}
/>
)}
{shape === "star" && (
<polygon
points={Array.from({ length: 10 }, (_, i) => {
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" && (
<path
d={`M${cx - 12},${cy - r} L${cx + 12},${cy - r} L${cx + 12},${cy - 12}
L${cx + r},${cy - 12} L${cx + r},${cy + 12} L${cx + 12},${cy + 12}
L${cx + 12},${cy + r} L${cx - 12},${cy + r} L${cx - 12},${cy + 12}
L${cx - r},${cy + 12} L${cx - r},${cy - 12} L${cx - 12},${cy - 12}Z`}
fill={`url(#${gradId})`}
stroke={finishColors[2]}
strokeWidth={1.5}
/>
)}
{shape === "shield" && (
<path
d={`M${cx},${cy - r}
Q${cx + r},${cy - r * 0.6} ${cx + r * 0.9},${cy - r * 0.1}
L${cx},${cy + r}
L${cx - r * 0.9},${cy - r * 0.1}
Q${cx - r},${cy - r * 0.6} ${cx},${cy - r}Z`}
fill={`url(#${gradId})`}
stroke={finishColors[2]}
strokeWidth={1.5}
/>
)}
{engraving && (
<text
x={cx}
y={cy + 2}
textAnchor="middle"
dominantBaseline="central"
fill={finishColors[2]}
fontSize={engraving.length > 10 ? 6 : 8}
fontFamily="serif"
fontWeight="bold"
letterSpacing={0.5}
>
{engraving.toUpperCase()}
</text>
)}
</g>
);
}
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 (
<svg
viewBox={`0 0 ${viewWidth} 175`}
className={className ?? "w-full h-auto rounded border border-zinc-700 bg-zinc-950"}
style={{ maxHeight: 220, ...style }}
>
{(() => {
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 (
<polygon
key={i}
points={`${tlx},${ribbonTop} ${trx},${ribbonTop} ${brx},${ribbonBottom} ${blx},${ribbonBottom}`}
fill={stripe.color}
/>
);
});
})()}
{/* Small connector tab between ribbon and medal */}
<rect
x={medalCx - 4}
y={ribbonBottom}
width={8}
height={8}
rx={2}
fill="#9ca3af"
/>
{/* Medal body */}
<MedalBody
shape={shape}
finish={finish}
engraving={engraving}
cx={medalCx}
cy={medalCy}
/>
</svg>
);
}