accesscodes/src/lib/player-lookup.ts
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

44 lines
1.7 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
export type LookupStatus = "idle" | "checking" | "found" | "missing" | "error";
// Looks a Minecraft name up once typing pauses. "missing" only means the
// upstream said the player does not exist; a rate limit or an outage is
// "error", which must never stop someone from donating.
export function usePlayerLookup(platform: "java" | "bedrock", raw: string, enabled = true): { status: LookupStatus; name: string } {
const name = raw.trim();
const [result, setResult] = useState<{ key: string; status: LookupStatus; name: string } | null>(null);
const key = `${platform}:${name.toLowerCase()}`;
const active = enabled && name.length >= 3;
useEffect(() => {
if (!active) return;
let stale = false;
const t = setTimeout(async () => {
let status: LookupStatus = "error";
let resolved = name;
try {
const r = await fetch("/api/resolve-player", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ type: platform, username: name }),
});
const j = await r.json().catch(() => null);
if (r.ok && j?.data) {
status = "found";
resolved = j.data.username ?? name;
} else if (/not found|error: 400/i.test(String(j?.error ?? ""))) {
status = "missing";
}
} catch {}
if (!stale) setResult({ key, status, name: resolved });
}, 500);
return () => { stale = true; clearTimeout(t); };
}, [active, key, platform, name]);
if (!active) return { status: "idle", name };
if (result?.key !== key) return { status: "checking", name };
return { status: result.status, name: result.name };
}