|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import Link from "next/link"; |
| 4 | +import { usePathname, useSearchParams } from "next/navigation"; |
| 5 | +import { createContext, useCallback, useContext, useEffect, useState } from "react"; |
| 6 | +import { AccountApiError } from "../../../lib/account/api"; |
| 7 | +import { |
| 8 | + adminErrorMessage, |
| 9 | + coreProtectMessage, |
| 10 | + getAdminMe, |
| 11 | + getPlayer, |
| 12 | + type AdminMe, |
| 13 | + type PlayerProfile as Profile, |
| 14 | +} from "../../../lib/admin/api"; |
| 15 | +import { formatAgo, formatEpoch } from "../../../lib/admin/time"; |
| 16 | +import AdminColumn from "./AdminColumn"; |
| 17 | +import { StaffGateMessage, gateKind, type GateKind } from "./StaffGate"; |
| 18 | +import PlayerTabs, { profileTab } from "./PlayerTabs"; |
| 19 | + |
| 20 | +type Load = { kind: "loading" } | { kind: GateKind } | { kind: "failed"; message: string } | { kind: "ready"; profile: Profile }; |
| 21 | + |
| 22 | +export type PlayerData = { |
| 23 | + profile: Profile; |
| 24 | + me: AdminMe | null; |
| 25 | + /** Admins and the owner; mods see the profile without it. */ |
| 26 | + movement: boolean; |
| 27 | + reload: () => void; |
| 28 | +}; |
| 29 | + |
| 30 | +const PlayerContext = createContext<PlayerData | null>(null); |
| 31 | + |
| 32 | +/** The player the frame has loaded; null outside one. */ |
| 33 | +export function usePlayer(): PlayerData | null { |
| 34 | + return useContext(PlayerContext); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * The player's name, details and tabs, shared by the profile and the movement |
| 39 | + * page so they stay put from tab to tab. Its pages render once the player has loaded, |
| 40 | + * so nothing arrives above them later and pushes them down. |
| 41 | + */ |
| 42 | +export default function PlayerFrame({ uuid, children }: { uuid: string; children: React.ReactNode }) { |
| 43 | + const [load, setLoad] = useState<Load>({ kind: "loading" }); |
| 44 | + const [me, setMe] = useState<AdminMe | null>(null); |
| 45 | + const pathname = usePathname(); |
| 46 | + const search = useSearchParams(); |
| 47 | + const current = pathname.endsWith("/movement") ? "movement" : profileTab(search.get("tab")); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + let live = true; |
| 51 | + getAdminMe() |
| 52 | + .then((value) => live && setMe(value)) |
| 53 | + .catch(() => undefined); |
| 54 | + return () => { |
| 55 | + live = false; |
| 56 | + }; |
| 57 | + }, []); |
| 58 | + |
| 59 | + const loadProfile = useCallback(() => { |
| 60 | + let live = true; |
| 61 | + getPlayer(uuid) |
| 62 | + .then((profile) => live && setLoad({ kind: "ready", profile })) |
| 63 | + .catch((err) => { |
| 64 | + if (!live) return; |
| 65 | + const status = err instanceof AccountApiError ? err.status : 0; |
| 66 | + setLoad(status === 400 || status === 404 ? { kind: "failed", message: adminErrorMessage(err) } : { kind: gateKind(err) }); |
| 67 | + }); |
| 68 | + return () => { |
| 69 | + live = false; |
| 70 | + }; |
| 71 | + }, [uuid]); |
| 72 | + |
| 73 | + useEffect(() => loadProfile(), [loadProfile]); |
| 74 | + |
| 75 | + const back = ( |
| 76 | + <Link href="/admin/players" className="text-sm text-[var(--tfmc-stone)] hover:text-[var(--tfmc-cream)]"> |
| 77 | + ← All players |
| 78 | + </Link> |
| 79 | + ); |
| 80 | + if (load.kind !== "ready") { |
| 81 | + return ( |
| 82 | + <AdminColumn> |
| 83 | + <p className="mt-4">{back}</p> |
| 84 | + {load.kind === "loading" ? ( |
| 85 | + <p className="mt-6 text-[var(--tfmc-mist)]">Loading…</p> |
| 86 | + ) : load.kind === "failed" ? ( |
| 87 | + <p className="mt-6 text-[var(--tfmc-mist)]" role="alert"> |
| 88 | + {load.message} |
| 89 | + </p> |
| 90 | + ) : ( |
| 91 | + <StaffGateMessage kind={load.kind} /> |
| 92 | + )} |
| 93 | + </AdminColumn> |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + const { profile } = load; |
| 98 | + const movement = me?.capabilities.includes("view_player_movement") ?? false; |
| 99 | + const notice = coreProtectMessage(profile.coreprotect); |
| 100 | + const label = profile.coreprotect.server_label; |
| 101 | + const seen = [ |
| 102 | + profile.online ? "Seen just now" : `Last seen ${formatAgo(profile.last_seen).toLowerCase()}`, |
| 103 | + profile.first_seen ? `first seen ${formatEpoch(profile.first_seen)}` : null, |
| 104 | + label, |
| 105 | + profile.past_names.length ? `previously ${profile.past_names.map((n) => n.name).join(", ")}` : null, |
| 106 | + ].filter(Boolean); |
| 107 | + |
| 108 | + return ( |
| 109 | + <> |
| 110 | + <AdminColumn> |
| 111 | + {/* Two lines on every tab, so the tabs under them never move and the map keeps its height. */} |
| 112 | + <header className="mt-4"> |
| 113 | + <div className="flex flex-wrap items-baseline gap-x-3"> |
| 114 | + {back} |
| 115 | + <h2 className="font-[family-name:var(--font-fraunces)] text-2xl text-[var(--tfmc-cream)]"> |
| 116 | + {profile.minecraft_name ?? "Unknown name"} |
| 117 | + </h2> |
| 118 | + <span className="font-mono text-xs text-[var(--tfmc-stone)]">{profile.uuid}</span> |
| 119 | + </div> |
| 120 | + <p className="mt-1 text-sm text-[var(--tfmc-mist)]"> |
| 121 | + {seen.join(" · ")} |
| 122 | + {" · "} |
| 123 | + <Link href={`/admin/ranks/players/${profile.uuid}`} className="text-[var(--tfmc-accent)] underline-offset-2 hover:underline"> |
| 124 | + In-game ranks and permissions → |
| 125 | + </Link> |
| 126 | + </p> |
| 127 | + {notice ? ( |
| 128 | + <p className="mt-2 text-sm text-[#e8c48a]" role="status"> |
| 129 | + {notice} |
| 130 | + </p> |
| 131 | + ) : null} |
| 132 | + </header> |
| 133 | + <PlayerTabs uuid={profile.uuid} current={current} movement={movement} /> |
| 134 | + </AdminColumn> |
| 135 | + <PlayerContext.Provider value={{ profile, me, movement, reload: loadProfile }}>{children}</PlayerContext.Provider> |
| 136 | + </> |
| 137 | + ); |
| 138 | +} |
0 commit comments