Files
tessera/web/src/components/hud/HUDRail.tsx
Adnan Zahir 95778d54e4 fix: harden the vault before release
Require a private passphrase and loopback-or-token admin auth. New vaults use Argon2id; existing PBKDF2 vaults still open, and new snapshot metadata is encrypted. Wormhole tokens survive restart, offset uploads seal into the vault, and client-certificate uploads are real. Reject the command provider and keep Postgres passwords off the process arguments. Keep the interruptible canvas motion.
2026-11-10 09:53:06 +07:00

190 lines
7.3 KiB
TypeScript

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<HTMLElement>(null);
const slots = useRef<Slot[]>([]);
const gesture = useRef<Gesture>(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<HTMLElement>) {
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<HTMLElement>) {
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<HTMLElement>) {
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 (
<motion.nav
ref={nav}
className="vault-nav"
aria-label="Vault systems"
initial={false}
animate={{ "--dock-morph": panelOpen && !floating ? 1 : 0 }}
onPointerDown={start}
onPointerMove={move}
onPointerUp={finish}
onPointerCancel={finish}
onLostPointerCapture={event => {
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 ? (
<svg className="nav-liquid" aria-hidden="true">
<defs>
<filter id={`${id}-goo`} x="-25%" y="-50%" width="150%" height="200%">
<feGaussianBlur in="SourceGraphic" stdDeviation="3.5" result="blur" />
<feColorMatrix in="blur" type="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 20 -8" result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="atop" />
</filter>
<linearGradient id={`${id}-fill`} x2="0%" y2="100%">
<stop stopColor="#724a34" />
<stop offset="100%" stopColor="#38251e" />
</linearGradient>
</defs>
<g filter={`url(#${id}-goo)`} fill={`url(#${id}-fill)`}>
<motion.ellipse cx={reducedMotion ? center : tail} cy="50%" rx="17" ry="13" />
<motion.ellipse cx={center} cy="50%" rx="21" ry="17" />
</g>
</svg>
) : (
<>
<span aria-hidden="true" className="nav-text-dock" />
<motion.span aria-hidden="true" className="nav-underline" style={{ x: center, width }} />
</>
)}
{items.map(({ id, label, icon }) => (
<button
key={id}
type="button"
aria-label={floating ? label : undefined}
title={floating ? label : undefined}
onClick={() => onSelect(id)}
aria-current={active === id ? "page" : undefined}
>
{floating ? (
<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d={icon} />
</svg>
) : label}
</button>
))}
</motion.nav>
);
}