268 lines
8.2 KiB
TypeScript
268 lines
8.2 KiB
TypeScript
import { useEffect, useMemo, useRef, useState } from "react";
|
|
import type { Orbit, Snapshot } from "../../api";
|
|
import { easeOutCubic, useGalaxyTime } from "../../hooks/useGalaxyMotion";
|
|
|
|
const MAX_STARS = 20;
|
|
const MAX_MOONS = 6;
|
|
|
|
type StarLayout = {
|
|
orbit: Orbit;
|
|
rx: number;
|
|
ry: number;
|
|
speed: number;
|
|
phase: number;
|
|
size: number;
|
|
};
|
|
|
|
function statusColor(status: string): string {
|
|
if (status === "failed") return "#f87171";
|
|
if (status === "running" || status === "rehearsed") return "#f0c468";
|
|
if (status === "completed") return "#4ade9a";
|
|
return "#94a3d4";
|
|
}
|
|
|
|
function groupSnaps(snaps: Snapshot[]): Map<string, Snapshot[]> {
|
|
const m = new Map<string, Snapshot[]>();
|
|
for (const s of snaps) {
|
|
const list = m.get(s.orbit_name) ?? [];
|
|
list.push(s);
|
|
m.set(s.orbit_name, list);
|
|
}
|
|
for (const [k, v] of m) {
|
|
v.sort((a, b) => b.taken_at.localeCompare(a.taken_at));
|
|
m.set(k, v.slice(0, MAX_MOONS));
|
|
}
|
|
return m;
|
|
}
|
|
|
|
export function BlackholeGalaxy({
|
|
orbits,
|
|
snapshots,
|
|
flowing,
|
|
onToggleFlow,
|
|
onSelectSnapshot,
|
|
onSelectOrbit,
|
|
selectedSnapshotId,
|
|
}: {
|
|
orbits: Orbit[];
|
|
snapshots: Snapshot[];
|
|
flowing: boolean;
|
|
onToggleFlow: () => void;
|
|
onSelectSnapshot: (id: string) => void;
|
|
onSelectOrbit: (id: string) => void;
|
|
selectedSnapshotId?: string | null;
|
|
}) {
|
|
const t = useGalaxyTime(!flowing);
|
|
const [blend, setBlend] = useState(1);
|
|
const blendStart = useRef(0);
|
|
const wasFlowing = useRef(flowing);
|
|
|
|
useEffect(() => {
|
|
if (wasFlowing.current !== flowing) {
|
|
blendStart.current = performance.now();
|
|
wasFlowing.current = flowing;
|
|
}
|
|
let raf = 0;
|
|
const tick = () => {
|
|
const dt = (performance.now() - blendStart.current) / 600;
|
|
setBlend(flowing ? Math.min(1, dt) : Math.max(0, 1 - easeOutCubic(Math.min(1, dt))));
|
|
if (dt < 1) raf = requestAnimationFrame(tick);
|
|
};
|
|
raf = requestAnimationFrame(tick);
|
|
return () => cancelAnimationFrame(raf);
|
|
}, [flowing]);
|
|
|
|
const stars = useMemo((): StarLayout[] => {
|
|
const list = orbits.slice(0, MAX_STARS);
|
|
return list.map((orbit, i) => {
|
|
const n = Math.max(list.length, 1);
|
|
const tier = i % 3;
|
|
return {
|
|
orbit,
|
|
rx: 92 + tier * 22 + (i % 4) * 6,
|
|
ry: 56 + tier * 14 + (i % 3) * 8,
|
|
speed: 0.08 + (i % 7) * 0.012,
|
|
phase: (i / n) * Math.PI * 2,
|
|
size: 5 + (orbit.name.length % 3),
|
|
};
|
|
});
|
|
}, [orbits]);
|
|
|
|
const snapsByOrbit = useMemo(() => groupSnaps(snapshots), [snapshots]);
|
|
|
|
return (
|
|
<div className="w-full h-full flex items-center justify-center pb-24 pointer-events-auto">
|
|
<svg
|
|
viewBox="-250 -250 500 500"
|
|
className="w-full max-w-[min(100vmin,520px)] h-auto max-h-[70dvh]"
|
|
role="application"
|
|
aria-label="Galaxy vault viewport"
|
|
>
|
|
<defs>
|
|
<radialGradient id="bhCore" cx="50%" cy="50%" r="50%">
|
|
<stop offset="0%" stopColor="#000" />
|
|
<stop offset="45%" stopColor="#0a0614" />
|
|
<stop offset="100%" stopColor="transparent" />
|
|
</radialGradient>
|
|
<radialGradient id="bhDisk" cx="50%" cy="50%" r="50%">
|
|
<stop offset="0%" stopColor="#a78bfa" stopOpacity="0.7" />
|
|
<stop offset="40%" stopColor="#f0c468" stopOpacity="0.35" />
|
|
<stop offset="100%" stopColor="transparent" />
|
|
</radialGradient>
|
|
<filter id="glow">
|
|
<feGaussianBlur stdDeviation="2" result="b" />
|
|
<feMerge>
|
|
<feMergeNode in="b" />
|
|
<feMergeNode in="SourceGraphic" />
|
|
</feMerge>
|
|
</filter>
|
|
</defs>
|
|
|
|
{/* faint orbital guides */}
|
|
{stars.map((s, i) => (
|
|
<ellipse
|
|
key={`path-${s.orbit.id}`}
|
|
cx="0"
|
|
cy="0"
|
|
rx={s.rx}
|
|
ry={s.ry}
|
|
fill="none"
|
|
stroke="#2d3a6b"
|
|
strokeOpacity={flowing ? 0.12 : 0.22}
|
|
strokeWidth="0.6"
|
|
strokeDasharray="3 6"
|
|
transform={`rotate(${(i * 17) % 40 - 20})`}
|
|
/>
|
|
))}
|
|
|
|
<BlackHoleCore t={t} flowing={flowing} onToggle={onToggleFlow} />
|
|
|
|
{stars.map((s, i) => {
|
|
const n = stars.length;
|
|
const slotAngle = (i / n) * Math.PI * 2 - Math.PI / 2;
|
|
const flowAngle = s.phase + t * s.speed;
|
|
const angle = flowAngle * blend + slotAngle * (1 - blend);
|
|
const x = Math.cos(angle) * s.rx;
|
|
const y = Math.sin(angle) * s.ry;
|
|
const moons = snapsByOrbit.get(s.orbit.name) ?? [];
|
|
const color = statusColor(s.orbit.last_status);
|
|
|
|
return (
|
|
<g key={s.orbit.id} filter="url(#glow)">
|
|
<circle
|
|
cx={x}
|
|
cy={y}
|
|
r={s.size + 8}
|
|
fill="transparent"
|
|
className="cursor-pointer"
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
if (!flowing) onSelectOrbit(s.orbit.id);
|
|
}}
|
|
/>
|
|
<circle
|
|
cx={x}
|
|
cy={y}
|
|
r={s.size}
|
|
fill={color}
|
|
fillOpacity={0.85}
|
|
stroke="#eef1ff"
|
|
strokeOpacity={0.35}
|
|
strokeWidth="0.6"
|
|
className={!flowing ? "cursor-pointer" : "pointer-events-none"}
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
if (!flowing) onSelectOrbit(s.orbit.id);
|
|
}}
|
|
/>
|
|
{!flowing &&
|
|
moons.map((moon, mi) => {
|
|
const mAngle = (mi / Math.max(moons.length, 1)) * Math.PI * 2 + 0.3;
|
|
const mx = x + Math.cos(mAngle) * 14;
|
|
const my = y + Math.sin(mAngle) * 10;
|
|
const sel = moon.id === selectedSnapshotId;
|
|
return (
|
|
<g key={moon.id}>
|
|
<circle
|
|
cx={mx}
|
|
cy={my}
|
|
r={sel ? 5 : 3.5}
|
|
fill={sel ? "#a78bfa" : "#1e2647"}
|
|
stroke={sel ? "#c9d2f5" : color}
|
|
strokeWidth="0.8"
|
|
className="cursor-pointer"
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
onSelectSnapshot(moon.id);
|
|
}}
|
|
/>
|
|
</g>
|
|
);
|
|
})}
|
|
{!flowing && (
|
|
<text
|
|
x={x}
|
|
y={y - s.size - 6}
|
|
textAnchor="middle"
|
|
fill="#94a3d4"
|
|
fontSize="7"
|
|
className="font-mono pointer-events-none"
|
|
>
|
|
{s.orbit.name.length > 14
|
|
? `${s.orbit.name.slice(0, 12)}…`
|
|
: s.orbit.name}
|
|
</text>
|
|
)}
|
|
</g>
|
|
);
|
|
})}
|
|
</svg>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function BlackHoleCore({
|
|
t,
|
|
flowing,
|
|
onToggle,
|
|
}: {
|
|
t: number;
|
|
flowing: boolean;
|
|
onToggle: () => void;
|
|
}) {
|
|
const diskRot = flowing ? t * 28 : 0;
|
|
return (
|
|
<g className="cursor-pointer" onClick={onToggle}>
|
|
<title>{flowing ? "Click to lock vault for targeting" : "Click to resume galaxy flow"}</title>
|
|
<circle cx="0" cy="0" r="72" fill="url(#bhCore)" />
|
|
<ellipse
|
|
cx="0"
|
|
cy="0"
|
|
rx="52"
|
|
ry="14"
|
|
fill="none"
|
|
stroke="url(#bhDisk)"
|
|
strokeWidth="10"
|
|
opacity="0.55"
|
|
transform={`rotate(${diskRot})`}
|
|
/>
|
|
<ellipse
|
|
cx="0"
|
|
cy="0"
|
|
rx="38"
|
|
ry="9"
|
|
fill="url(#bhDisk)"
|
|
opacity="0.4"
|
|
transform={`rotate(${-diskRot * 1.3 + 20})`}
|
|
/>
|
|
<circle cx="0" cy="0" r="18" fill="#000" stroke="#a78bfa" strokeOpacity="0.5" strokeWidth="1" />
|
|
<circle cx="0" cy="0" r="48" fill="none" stroke="#a78bfa" strokeOpacity={flowing ? 0.15 : 0.35} strokeWidth="0.8" strokeDasharray="4 8">
|
|
{!flowing && null}
|
|
</circle>
|
|
<text y="4" textAnchor="middle" fill="#6b7ab8" fontSize="8" className="font-mono pointer-events-none">
|
|
VAULT
|
|
</text>
|
|
</g>
|
|
);
|
|
}
|