The map is a canopy with a bezel, an inset window and a sill carrying a Window | Radar view switch. Consoles open as centered screens; Orbits, Outposts, Activity and the snapshot sheet follow one vocabulary of recessed fields, bevelled keys and lamps that stay distinct by shape. - Activity replaces the Restore tab with one log of backups and restores. Rows lead to a snapshot or an orbit; restores and exports start from the snapshot. The model is a pure function with tests. - Restore opens from a snapshot as a two column screen: choose and confirm on the left, job progress or countdown on the right. - Add Segmented and a themed Select (portal list, keyboard and form support). - Add a spotlight-style search panel. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
184 lines
8.3 KiB
TypeScript
184 lines
8.3 KiB
TypeScript
import { useId, useLayoutEffect, useMemo, useRef, useState, type KeyboardEvent } from "react";
|
|
import { animate, motion, useMotionValue, useTransform } from "motion/react";
|
|
import type { Orbit, Snapshot } from "../../api";
|
|
import { gentleSpring, uiSpring } from "../../lib/motion";
|
|
import { createVaultSearch, type SearchResult } from "../../lib/vaultSearch";
|
|
import { hudItems } from "./HUDRail";
|
|
|
|
const resultKey = ({ kind, id }: SearchResult) => `${kind}-${id}`;
|
|
|
|
export function SearchIcon() {
|
|
return (
|
|
<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor"
|
|
strokeWidth="1.5" strokeLinecap="round" aria-hidden="true">
|
|
<circle cx="10.5" cy="10.5" r="5.5" /><path d="m15 15 4 4" />
|
|
</svg>
|
|
);
|
|
}
|
|
|
|
export function VaultSearch({ open, reducedMotion, orbits, snapshots, orbitStatus, snapshotStatus, onClose, onSelect }: {
|
|
open: boolean;
|
|
reducedMotion: boolean;
|
|
orbits?: Orbit[];
|
|
snapshots?: Snapshot[];
|
|
orbitStatus?: string;
|
|
snapshotStatus?: string;
|
|
onClose: () => void;
|
|
onSelect: (result: SearchResult) => void;
|
|
}) {
|
|
const id = useId();
|
|
const input = useRef<HTMLInputElement>(null);
|
|
const close = useRef<HTMLButtonElement>(null);
|
|
const overlay = useRef<HTMLDivElement>(null);
|
|
const body = useRef<HTMLDivElement>(null);
|
|
const progress = useMotionValue(0);
|
|
const y = useTransform(progress, [0, 1], [-12, 0]);
|
|
const height = useMotionValue(0);
|
|
const [query, setQuery] = useState("");
|
|
const [activeId, setActiveId] = useState<string>();
|
|
const search = useMemo(() => createVaultSearch(hudItems, orbits, snapshots), [orbits, snapshots]);
|
|
const groups = useMemo(() => search(query), [query, search]);
|
|
const results = useMemo(() => groups.flatMap(group => group.results), [groups]);
|
|
const active = results.find(result => resultKey(result) === activeId) ?? results[0];
|
|
const activeKey = active && resultKey(active);
|
|
const activeOption = activeKey && `${id}-${encodeURIComponent(activeKey)}`;
|
|
|
|
useLayoutEffect(() => {
|
|
if (!open) return;
|
|
setQuery("");
|
|
setActiveId(undefined);
|
|
input.current?.focus({ preventScroll: true });
|
|
const viewport = window.visualViewport;
|
|
if (!viewport) return;
|
|
const style = overlay.current!.style;
|
|
const updateViewport = () => {
|
|
style.setProperty("--search-viewport", `${viewport.height}px`);
|
|
style.setProperty("--search-viewport-top", `${viewport.offsetTop}px`);
|
|
};
|
|
updateViewport();
|
|
viewport.addEventListener("resize", updateViewport);
|
|
viewport.addEventListener("scroll", updateViewport);
|
|
return () => {
|
|
viewport.removeEventListener("resize", updateViewport);
|
|
viewport.removeEventListener("scroll", updateViewport);
|
|
};
|
|
}, [open]);
|
|
|
|
useLayoutEffect(() => {
|
|
if (reducedMotion) progress.jump(Number(open));
|
|
else animate(progress, Number(open), uiSpring);
|
|
}, [open, reducedMotion, progress]);
|
|
|
|
useLayoutEffect(() => {
|
|
const element = body.current!;
|
|
let target = element.offsetHeight;
|
|
height.jump(target);
|
|
const observer = new ResizeObserver(() => {
|
|
const next = element.offsetHeight;
|
|
if (next === target) return;
|
|
target = next;
|
|
if (reducedMotion) height.jump(next);
|
|
else animate(height, next, gentleSpring);
|
|
});
|
|
observer.observe(element);
|
|
return () => observer.disconnect();
|
|
}, [height, reducedMotion]);
|
|
|
|
useLayoutEffect(() => () => {
|
|
progress.stop();
|
|
height.stop();
|
|
}, [progress, height]);
|
|
|
|
useLayoutEffect(() => {
|
|
if (!open) return;
|
|
if (activeKey !== activeId) setActiveId(activeKey);
|
|
if (activeOption) document.getElementById(activeOption)?.scrollIntoView({ block: "nearest" });
|
|
}, [open, activeId, activeKey, activeOption, results]);
|
|
|
|
function handleKeys(event: KeyboardEvent) {
|
|
if (event.key === "Tab") {
|
|
event.preventDefault();
|
|
(event.target === input.current ? close.current : input.current)?.focus();
|
|
return;
|
|
}
|
|
if (event.target !== input.current || event.nativeEvent.isComposing || event.keyCode === 229) return;
|
|
if (event.key === "Enter" && active && !event.repeat) {
|
|
event.preventDefault();
|
|
onSelect(active);
|
|
} else if ((event.key === "ArrowDown" || event.key === "ArrowUp") && results.length) {
|
|
event.preventDefault();
|
|
const offset = event.key === "ArrowDown" ? 1 : -1;
|
|
const next = results[(results.indexOf(active) + offset + results.length) % results.length];
|
|
setActiveId(resultKey(next));
|
|
}
|
|
}
|
|
|
|
return (
|
|
<motion.div ref={overlay} className="search-overlay" inert={!open} style={{ opacity: progress }} onClick={event => {
|
|
if (event.target === event.currentTarget) onClose();
|
|
}}>
|
|
<motion.section className="search-panel" style={{ y }} role="dialog" aria-modal="true" aria-label="Vault search" onKeyDown={handleKeys}>
|
|
<div className="search-field">
|
|
<SearchIcon />
|
|
<input ref={input} type="text" role="combobox" aria-label="Search vault"
|
|
aria-autocomplete="list" aria-expanded={!!results.length} aria-controls={`${id}-results`}
|
|
aria-activedescendant={activeOption}
|
|
autoComplete="off" autoCapitalize="none" spellCheck={false} placeholder="Search orbits, snapshots, consoles…"
|
|
value={query} onChange={event => {
|
|
setQuery(event.currentTarget.value);
|
|
setActiveId(undefined);
|
|
}} />
|
|
<button ref={close} className="console-control search-close" type="button" aria-label="Close search" onClick={onClose}>
|
|
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor"
|
|
strokeWidth="1.5" strokeLinecap="round" aria-hidden="true"><path d="m7 7 10 10M17 7 7 17" /></svg>
|
|
</button>
|
|
</div>
|
|
<motion.div className="search-list-frame" style={{ height }}>
|
|
<div ref={body} className="search-body">
|
|
<div role="listbox" id={`${id}-results`} aria-label="Search results" hidden={!results.length}>
|
|
{groups.map(group => (
|
|
<div key={group.label} role="group" aria-label={group.label}>
|
|
<p className="instrument-label search-group" aria-hidden="true">{group.label}</p>
|
|
{group.results.map(result => {
|
|
const key = resultKey(result);
|
|
const icon = result.kind === "navigation" ? result.icon
|
|
: result.kind === "orbit" ? hudItems[1].icon : "M12 3l8 9-8 9-8-9z M12 9v6";
|
|
return (
|
|
<div key={key} id={`${id}-${encodeURIComponent(key)}`} role="option" className="search-result"
|
|
aria-selected={activeKey === key} onMouseDown={event => event.preventDefault()} onClick={() => onSelect(result)}>
|
|
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor"
|
|
strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d={icon} /></svg>
|
|
<span className="search-result-text">
|
|
<span>{result.label}</span>
|
|
{result.detail && <small>{result.detail}</small>}
|
|
</span>
|
|
<span className="search-result-hint" aria-hidden="true">
|
|
{result.kind === "navigation" ? "Console" : result.kind === "orbit" ? "Orbit" : "Snapshot"}
|
|
</span>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
))}
|
|
</div>
|
|
{!results.length && (
|
|
<div className="search-empty" aria-hidden="true">
|
|
<p>No signal</p>
|
|
<span>Nothing matches that. Try an orbit name, ID, provider, or a capture date.</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</motion.div>
|
|
<footer className="search-footer">
|
|
<span className="search-keys" aria-hidden="true"><span>↑↓ Move</span><span>↵ Open</span><span>Esc Close</span></span>
|
|
<p className="search-status" role="status">
|
|
<span>{results.length ? `${results.length} ${query.trim() ? "results shown" : "destinations"}` : "No matching results."}</span>
|
|
{orbitStatus && <span>{orbitStatus}</span>}
|
|
{snapshotStatus && <span>{snapshotStatus}</span>}
|
|
</p>
|
|
</footer>
|
|
</motion.section>
|
|
</motion.div>
|
|
);
|
|
}
|