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 { const m = new Map(); 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 (
{/* faint orbital guides */} {stars.map((s, i) => ( ))} {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 ( { e.stopPropagation(); if (!flowing) onSelectOrbit(s.orbit.id); }} /> { 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 ( { e.stopPropagation(); onSelectSnapshot(moon.id); }} /> ); })} {!flowing && ( {s.orbit.name.length > 14 ? `${s.orbit.name.slice(0, 12)}…` : s.orbit.name} )} ); })}
); } function BlackHoleCore({ t, flowing, onToggle, }: { t: number; flowing: boolean; onToggle: () => void; }) { const diskRot = flowing ? t * 28 : 0; return ( {flowing ? "Click to lock vault for targeting" : "Click to resume galaxy flow"} {!flowing && null} VAULT ); }