accesscodes/src/components/donate-podium.tsx
anonymousratwastaken ebfa25d1e8 Rework donate flow into guided steps with gift options
- Split the donate page into step components and reorder the flow: intent, rank or amount, recipient, gift, upgrade target, then confirm
- Let a gift go to a player on the other edition, and allow anonymous gifts (donor is left blank in metadata)
- Look up the recipient's current rank for upgrades and disable ranks they already hold
- Validate custom amounts on the server as integers between CUSTOM_MIN and CUSTOM_MAX
- Show friendly messages for checkout errors and send users back to the step that needs fixing
- Build the Stripe appearance from the page's glow accent colour, and tint the glow to match a custom amount's unlocked tier
2026-10-24 16:40:08 +01:00

164 lines
7.1 KiB
TypeScript

"use client";
import { useRef, useState } from "react";
import { motion, useReducedMotion } from "motion/react";
import { Check } from "lucide-react";
import { TIERS } from "@/lib/donations";
import { useIsWide } from "@/components/donate-hand";
import { cn } from "@/lib/utils";
// The "which rank?" step as a podium: the top rank stands tallest in the
// middle, the others step down on either side. Blocks grow up from the floor
// when the step opens, rise a little under the pointer, and the perks of the
// rank being looked at are written underneath.
export type Rank = "iron" | "gold" | "diamond";
type Step = {
id: Rank;
// Block height in rem on narrow and wide screens.
narrow: number;
wide: number;
// Light and deep tone, matching the page glow this rank selects.
light: string;
deep: string;
};
// Left to right, so keyboard order matches what is on screen.
const PODIUM: Step[] = [
{ id: "gold", narrow: 11, wide: 12.5, light: "#fbbf24", deep: "#f59e0b" },
{ id: "diamond", narrow: 13, wide: 15, light: "#22d3ee", deep: "#06b6d4" },
{ id: "iron", narrow: 9, wide: 10.5, light: "#cbd5e1", deep: "#64748b" },
];
// Centre first, then outwards.
const ENTRANCE = [1, 0, 2];
const SPRING = { type: "spring", stiffness: 320, damping: 30, mass: 0.8 } as const;
export function DonatePodium({ value, onSelect, onPreview, ariaLabel = "Which rank?", disabled = [], price, note }: {
value: string;
onSelect: (id: Rank) => void;
onPreview: (id: Rank | null) => void;
ariaLabel?: string;
// Ranks that stand on the podium but cannot be picked.
disabled?: Rank[];
// Overrides for the line under a rank's name (null hides it) and a small
// caption below that. The default is the rank's full price.
price?: (id: Rank) => string | null;
note?: (id: Rank) => string | undefined;
}) {
const reduced = useReducedMotion() ?? false;
const wide = useIsWide();
const [hovered, setHovered] = useState<number | null>(null);
const hoveredRef = useRef<number | null>(null);
const buttons = useRef<(HTMLButtonElement | null)[]>([]);
const selected = PODIUM.findIndex((p) => p.id === value);
const off = (i: number) => disabled.includes(PODIUM[i].id);
const firstOn = PODIUM.findIndex((_, i) => !off(i));
const tabStop = selected >= 0 && !off(selected) ? selected : firstOn;
const shown = hovered ?? (selected >= 0 ? selected : null);
// One owner for the lifted block and the glow preview. A block's blur only
// clears it when that block holds it, so pressing another block (which moves
// focus) cannot flash the old colour.
function hover(i: number | null) {
hoveredRef.current = i;
setHovered(i);
onPreview(i === null ? null : PODIUM[i].id);
}
// Move focus to the next pickable block in a direction, wrapping round.
function focusStep(from: number, dir: 1 | -1) {
for (let n = 1; n <= PODIUM.length; n++) {
const i = (from + dir * n + PODIUM.length * n) % PODIUM.length;
if (!off(i)) { buttons.current[i]?.focus(); return; }
}
}
function onKeyDown(e: React.KeyboardEvent, i: number) {
if (e.key === "ArrowRight" || e.key === "ArrowDown") { e.preventDefault(); focusStep(i, 1); }
else if (e.key === "ArrowLeft" || e.key === "ArrowUp") { e.preventDefault(); focusStep(i, -1); }
}
return (
<div className="flex flex-col gap-5">
<div
role="radiogroup"
aria-label={ariaLabel}
className="mx-auto flex w-full max-w-md items-end justify-center gap-1.5 pt-6 md:gap-2"
onPointerLeave={() => hover(null)}
>
{PODIUM.map((step, i) => {
const tier = TIERS[step.id];
const unavailable = off(i);
const checked = i === selected && !unavailable;
const lifted = hovered === i;
const faded = selected >= 0 && !checked && !lifted && !unavailable;
const priceText = price ? price(step.id) : `£${(tier.price / 100).toFixed(2)}`;
const noteText = note?.(step.id);
const bump = checked ? 0.75 : lifted ? 0.5 : 0;
return (
<motion.button
key={step.id}
ref={(el) => { buttons.current[i] = el; }}
type="button"
role="radio"
aria-checked={checked}
aria-disabled={unavailable || undefined}
tabIndex={i === tabStop ? 0 : -1}
onClick={() => { if (!unavailable) onSelect(step.id); }}
onKeyDown={(e) => onKeyDown(e, i)}
onPointerEnter={(e) => { if (e.pointerType !== "touch" && !unavailable) hover(i); }}
onFocus={(e) => { if (e.currentTarget.matches(":focus-visible")) hover(i); }}
onBlur={() => { if (hoveredRef.current === i) hover(null); }}
initial={reduced ? false : { height: 0 }}
animate={{
height: ((wide ? step.wide : step.narrow) + bump) * 16,
opacity: unavailable ? 0.45 : faded ? 0.8 : 1,
}}
transition={reduced ? { duration: 0 } : { ...SPRING, delay: ENTRANCE[i] * 0.07 }}
style={{
"--c": step.light,
"--c2": step.deep,
} as React.CSSProperties}
className={cn(
"relative flex min-w-0 flex-1 flex-col overflow-hidden rounded-t-2xl border border-b-0 p-3 text-start select-none md:p-4",
"bg-[color-mix(in_oklab,var(--c2)_20%,oklch(0.19_0.006_286))]",
"shadow-[inset_0_1px_0_color-mix(in_oklab,var(--c)_45%,transparent),0_-14px_24px_-16px_rgb(0_0_0/0.6)]",
"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-4 focus-visible:outline-(--c)",
checked ? "border-(--c)" : "border-[color-mix(in_oklab,var(--c)_20%,transparent)]",
unavailable ? "cursor-not-allowed" : "cursor-pointer",
"transition-[border-color] duration-200"
)}
>
<span
aria-hidden="true"
className={cn(
"flex size-4 shrink-0 items-center justify-center rounded-full border transition-colors duration-200",
checked ? "border-(--c) bg-(--c) text-black/80" : "border-white/25"
)}
>
{checked && <Check className="size-2.5" strokeWidth={3.5} />}
</span>
<span className="mt-3 text-base/tight font-semibold tracking-tight text-white md:text-xl/tight">{tier.label}</span>
{priceText !== null && <span className="mt-1 text-sm font-semibold tabular-nums text-[color-mix(in_oklab,var(--c)_70%,white)]">{priceText}</span>}
{noteText && <span className="mt-0.5 text-xs/tight text-white/70">{noteText}</span>}
</motion.button>
);
})}
</div>
{/* The floor the podium stands on. */}
<div aria-hidden="true" className="mx-auto -mt-5 h-px w-full max-w-md bg-white/15" />
<p aria-live="polite" className="mx-auto min-h-10 max-w-sm text-center text-xs/relaxed text-pretty text-muted-foreground">
{shown === null
? "Pick a rank to see what's included."
: TIERS[PODIUM[shown].id].perks.join(" · ")}
</p>
</div>
);
}