Files
plugins/packages/fingerprint/web/AppMenu.tsx
T
KaranocaVe 00f4d84b8f fingerprint 0.1.0: a page that checks whether a model answers like the one it claims to be (#516)
Adds the Fingerprint page plugin (packages/fingerprint): it checks whether a model behind the local magpie gateway answers like the model it claims to be.

Reviewed at 531ca11f4 (KaranocaVe). Run at that head merged with main 848497c, through mote-run on the Mac mini with Bun 1.4.2:
- bun run rebuild from the committed bun.lock (frozen, --ignore-scripts): typecheck, 30/30 tests, build; diff -r against the committed dist/ is empty.
- HOME=$(mktemp -d) bun test: 3838 pass, 0 fail. bun scripts/check.mjs: exit 0.
- Offline preload refusing every non-loopback fetch/socket: 30/30, nothing refused.
- Broken by hand, each red: ensureDetector() in bootstrap, dropping ttl: Infinity, validating the job after the download.

Maintainer decision: the reference data comes from two third-party hosts (lm.ikale.io, cdn.jsdmirror.com), only on an explicit Download or a valid job, and both are named on the page, in registry.json and the READMEs. It is data used locally to rank candidates and is sent nowhere; the @latest fallback has no pinned hash, so a bad snapshot would rank wrongly, which the page states.

Not on npm yet (E404): magpie installs it from the dist branch until an owner publishes the first version.
2026-10-31 02:00:33 +08:00

155 lines
4.6 KiB
TypeScript

import { useEffect, useId, useRef, useState } from 'react';
import type { KeyboardEvent } from 'react';
export type AppMenuOption = { value: string; label: string };
type AppMenuProps = {
label: string;
value: string;
options: AppMenuOption[];
onChange: (value: string) => void;
disabled?: boolean;
};
/** A Magpie-style listbox trigger, with native keyboard semantics. */
export function AppMenu({ label, value, options, onChange, disabled = false }: AppMenuProps) {
const id = useId();
const listId = `app-menu-${id.replace(/:/g, '')}`;
const root = useRef<HTMLDivElement>(null);
const button = useRef<HTMLButtonElement>(null);
const list = useRef<HTMLDivElement>(null);
const [open, setOpen] = useState(false);
const selected = Math.max(
0,
options.findIndex((option) => option.value === value),
);
const [active, setActive] = useState(selected);
useEffect(() => setActive(selected), [selected]);
useEffect(() => {
if (open) list.current?.focus();
}, [open]);
useEffect(() => {
if (!open) return;
const closeOutside = (event: MouseEvent | PointerEvent) => {
if (!root.current?.contains(event.target as Node)) setOpen(false);
};
document.addEventListener('pointerdown', closeOutside);
return () => document.removeEventListener('pointerdown', closeOutside);
}, [open]);
const choose = (next: number) => {
const option = options[next];
if (!option) return;
setActive(next);
onChange(option.value);
setOpen(false);
button.current?.focus();
};
const move = (delta: number) => {
if (!options.length) return;
setActive((current) => (current + delta + options.length) % options.length);
};
const onListKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
if (event.key === 'Escape') {
event.preventDefault();
setOpen(false);
button.current?.focus();
return;
}
if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
event.preventDefault();
move(1);
} else if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
event.preventDefault();
if (!open) setOpen(true);
move(-1);
} else if (event.key === 'Home') {
event.preventDefault();
setActive(0);
} else if (event.key === 'End') {
event.preventDefault();
setActive(Math.max(0, options.length - 1));
} else if (event.key === 'Tab') {
setOpen(false);
} else if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
choose(active);
}
};
return (
<div className="app-menu" ref={root}>
<button
ref={button}
type="button"
className="app-menu-trigger"
aria-label={label}
aria-haspopup="listbox"
aria-controls={listId}
aria-expanded={open}
disabled={disabled}
onClick={() => {
setActive(selected);
setOpen((current) => !current);
}}
onKeyDown={(event) => {
if (
event.key === 'ArrowDown' ||
event.key === 'ArrowUp' ||
event.key === 'Enter' ||
event.key === ' '
) {
event.preventDefault();
setActive(selected);
setOpen(true);
} else if (event.key === 'Escape') {
setOpen(false);
}
}}
>
<span>{options.find((option) => option.value === value)?.label ?? value}</span>
<span className="app-menu-chevron" aria-hidden="true">
⌄
</span>
</button>
<div
id={listId}
ref={list}
className="app-menu-list"
role="listbox"
aria-label={label}
aria-activedescendant={`${listId}-option-${active}`}
tabIndex={open ? 0 : -1}
hidden={!open}
onKeyDown={onListKeyDown}
onBlur={(event) => {
if (!root.current?.contains(event.relatedTarget as Node)) setOpen(false);
}}
>
{options.map((option, index) => (
<button
key={option.value}
id={`${listId}-option-${index}`}
type="button"
role="option"
aria-selected={option.value === value}
className={[option.value === value ? 'selected' : '', index === active ? 'active' : '']
.filter(Boolean)
.join(' ')}
tabIndex={-1}
onMouseEnter={() => setActive(index)}
onClick={() => choose(index)}
>
<span>{option.label}</span>
{option.value === value && <span aria-hidden="true">✓</span>}
</button>
))}
</div>
</div>
);
}