import { animate, motion, useMotionValue, useTransform, useVelocity } from "motion/react"; import { useCallback, useId, useLayoutEffect, useRef, type PointerEvent } from "react"; import { uiSpring } from "../../lib/motion"; export type HUDMode = "scene" | "orbits" | "wormhole" | "decay" | "reconstitute" | "snapshot"; const items = [ { id: "scene", label: "Vault", icon: "M4 9l8-4 8 4v6l-8 4-8-4z M4 9l8 4 8-4 M12 13v6" }, { id: "orbits", label: "Orbits", icon: "M21 12a9 5 0 1 1-18 0 9 5 0 1 1 18 0z M12 10.5a1.5 1.5 0 1 1 0 3 1.5 1.5 0 1 1 0-3z M18 5h.01" }, { id: "wormhole", label: "Outposts", icon: "M12 3v3 M4.2 7.5l2.6 1.5 M4.2 16.5 6.8 15 M12 18v3 M17.2 15l2.6 1.5 M17.2 9l2.6-1.5 M15.5 12a3.5 3.5 0 1 1-7 0 3.5 3.5 0 1 1 7 0z" }, { id: "decay", label: "Retention", icon: "M6 4h12 M6 20h12 M8 4v3l4 5-4 5v3 M16 4v3l-4 5 4 5v3" }, { id: "reconstitute", label: "Restore", icon: "M4 12a8 8 0 1 1 2.3 5.7 M4 6v6h6 M12 8v4l3 2" }, ] as const; type Slot = { center: number; width: number }; type Gesture = { pointer: number; x: number; y: number; origin?: number }; export function HUDRail({ active, floating, panelOpen, reducedMotion, onSelect, }: { active: HUDMode; floating: boolean; panelOpen: boolean; reducedMotion: boolean; onSelect: (mode: HUDMode) => void; }) { const nav = useRef(null); const slots = useRef([]); const gesture = useRef(undefined); const suppressClick = useRef(false); const center = useMotionValue(0); const width = useMotionValue(0); const velocity = useVelocity(center); const tail = useTransform(() => center.get() - Math.max(-22, Math.min(22, velocity.get() * 0.032))); const id = useId(); const activeIndex = Math.max(0, items.findIndex(item => item.id === active)); const snap = useCallback((index: number, immediate = false) => { const slot = slots.current[index]; if (!slot) return; if (immediate || reducedMotion) { center.jump(slot.center); width.jump(slot.width); } else { animate(center, slot.center, uiSpring); animate(width, slot.width, uiSpring); } }, [center, reducedMotion, width]); useLayoutEffect(() => () => { center.stop(); width.stop(); }, [center, width]); useLayoutEffect(() => { const element = nav.current!; const buttons = Array.from(element.querySelectorAll("button")); const measure = () => { const left = element.getBoundingClientRect().left + element.clientLeft; const next = buttons.map(button => { const rect = button.getBoundingClientRect(); return { center: rect.left + rect.width / 2 - left, width: rect.width - 10 }; }); const changed = next.some((slot, index) => slot.center !== slots.current[index]?.center || slot.width !== slots.current[index]?.width, ); slots.current = next; if (changed) gesture.current = undefined; return changed; }; snap(activeIndex, measure()); const observer = new ResizeObserver(() => { if (measure()) snap(activeIndex, true); }); observer.observe(element); buttons.forEach(button => observer.observe(button)); return () => observer.disconnect(); }, [activeIndex, floating, snap]); function start(event: PointerEvent) { if (!event.isPrimary || event.button !== 0) return; suppressClick.current = false; const button = (event.target as Element).closest("button"); if (button?.hasAttribute("aria-current")) { gesture.current = { pointer: event.pointerId, x: event.clientX, y: event.clientY }; } } function move(event: PointerEvent) { const drag = gesture.current; if (drag?.pointer !== event.pointerId) return; const dx = event.clientX - drag.x; if (drag.origin === undefined) { if (Math.abs(dx) < 5 || Math.abs(dx) < Math.abs(event.clientY - drag.y)) return; center.stop(); drag.origin = center.get(); suppressClick.current = true; event.currentTarget.setPointerCapture(event.pointerId); } center.set(Math.max(slots.current[0].center, Math.min(slots.current.at(-1)!.center, drag.origin + dx))); } function finish(event: PointerEvent) { const drag = gesture.current; if (drag?.pointer !== event.pointerId) return; gesture.current = undefined; if (drag.origin === undefined) return; if (event.type !== "pointerup") { snap(activeIndex); return; } const projected = center.get() + Math.max(-18, Math.min(18, center.getVelocity() * 0.1)); const next = slots.current.reduce((nearest, slot, index) => Math.abs(slot.center - projected) < Math.abs(slots.current[nearest].center - projected) ? index : nearest, 0); if (next === activeIndex) snap(activeIndex); else onSelect(items[next].id); } return ( { if (event.target === event.currentTarget) finish(event); }} onPointerLeave={event => { if (gesture.current?.pointer === event.pointerId && gesture.current.origin === undefined) { gesture.current = undefined; } }} onClickCapture={event => { if (event.detail > 0 && suppressClick.current) { event.preventDefault(); event.stopPropagation(); } }} > {floating ? ( ) : ( <> ); }