~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
205 lines · 6.1 KB Raw History
<script module lang="ts">
	import type { IconName } from '../icons';
	import type { Tile } from '../model';

	/** A row of the palette's own: an action, a canvas, or a tile open on a canvas. */
	export type PaletteItem = {
		key: string;
		section: 'Actions' | 'Canvases' | 'Open tiles';
		icon: IconName;
		tile?: Pick<Tile, 'tool' | 'arguments' | 'icon'>;
		text: string;
		sub: string | null;
		keys?: string;
		run: () => void;
	};
</script>

<script lang="ts">
	import { onMount } from 'svelte';
	import Icon from '../atoms/Icon.svelte';
	import { launcher, type Row } from '../launcher.svelte';
	import LauncherRow from '../molecules/LauncherRow.svelte';
	import PaletteRow from '../molecules/PaletteRow.svelte';
	import type { Actor } from '../model';

	/**
	 * The palette (K): one search over what can be done, every canvas, the
	 * tiles open on every open canvas, and what the launcher offers. Enter
	 * goes there. On a phone it is a sheet.
	 */
	let { me, items, onpick, onclose }: { me: Actor; items: PaletteItem[]; onpick: (row: Row) => void; onclose: () => void } = $props();

	let dialog: HTMLDialogElement | undefined = $state();
	let query = $state('');
	let active = $state(0);

	const q = $derived(query.trim().toLowerCase());
	const sections = $derived.by(() => {
		const own = items.filter((item) => !q || `${item.text} ${item.sub ?? ''} ${item.section}`.toLowerCase().includes(q));
		const rows = launcher.rows.filter((row) => !q || row.find.includes(q));
		return [
			...(['Actions', 'Canvases', 'Open tiles'] as const).map((name) => ({ name, items: own.filter((item) => item.section === name), rows: [] as Row[] })),
			{ name: 'Apps', items: [], rows: rows.filter((row) => row.section === 'mine') },
			{ name: 'On atproto', items: [], rows: rows.filter((row) => row.section === 'views') },
			{ name: 'Feeds', items: [], rows: rows.filter((row) => row.section === 'feeds') },
			...Array.from(
				Map.groupBy(
					rows.filter((row) => row.section === 'apps'),
					(row) => row.group ?? ''
				),
				([name, list]) => ({ name, items: [], rows: list })
			)
		].filter((section) => section.items.length + section.rows.length > 0);
	});
	/** Every row in turn, as the arrows go through them. */
	const flat = $derived(sections.flatMap((section) => [...section.items.map((item) => ({ item })), ...section.rows.map((row) => ({ row }))]));

	$effect(() => {
		void q;
		active = 0;
	});

	function go(at: number) {
		const found = flat[at];
		if (!found) return;
		onclose();
		if ('item' in found) found.item.run();
		else onpick(found.row);
	}

	function keydown(event: KeyboardEvent) {
		if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
			event.preventDefault();
			active = Math.min(Math.max(active + (event.key === 'ArrowDown' ? 1 : -1), 0), flat.length - 1);
			dialog?.querySelector(`[data-at="${active}"]`)?.scrollIntoView({ block: 'nearest' });
		} else if (event.key === 'Enter') {
			event.preventDefault();
			go(active);
		}
	}

	onMount(() => {
		launcher.start(me);
		dialog?.showModal();
	});
</script>

<dialog bind:this={dialog} class="palette" aria-label="Search everything" onclose={onclose} onclick={(event) => event.target === dialog && dialog?.close()}>
	<label class="search">
		<Icon name="search" />
		<!-- svelte-ignore a11y_autofocus (the palette is for typing into) -->
		<input bind:value={query} type="search" placeholder="Search everything" aria-label="Search everything" autocomplete="off" spellcheck="false" autofocus onkeydown={keydown} />
	</label>
	<div class="list" role="listbox" aria-label="Results">
		{#each sections as section (section.name)}
			<p class="head">{section.name}</p>
			{#each section.items as item (item.key)}
				{@const at = flat.findIndex((found) => 'item' in found && found.item === item)}
				<div data-at={at}>
					<PaletteRow icon={item.icon} tile={item.tile} text={item.text} sub={item.sub} keys={item.keys} active={at === active} onpick={() => go(at)} onhover={() => (active = at)} />
				</div>
			{/each}
			{#each section.rows as row (row.key)}
				{@const at = flat.findIndex((found) => 'row' in found && found.row === row)}
				<div data-at={at}>
					<LauncherRow text={row.text} sub={row.sub} icon={row.icon} domain={row.domain} picture={row.picture} tag={row.tag} active={at === active} onpick={() => go(at)} onhover={() => (active = at)} />
				</div>
			{/each}
		{:else}
			<p class="empty-state">Nothing matches “{query.trim()}”</p>
		{/each}
	</div>
	<footer>
		<span><kbd>↑</kbd> <kbd>↓</kbd> to choose</span>
		<span><kbd>Enter</kbd> to go</span>
		<span><kbd>Esc</kbd> to close</span>
	</footer>
</dialog>

<style>
	.palette {
		width: min(var(--palette-width), calc(100vw - 2 * var(--space-4)));
		max-height: min(32rem, calc(100vh - 8rem));
		margin: 12vh auto auto;
		padding: 0;
		border: var(--border);
		border-radius: var(--radius-md);
		background: var(--bg);
		box-shadow: var(--shadow-pop);
		color: var(--fg);
		overflow: hidden;
	}

	.palette[open] {
		display: flex;
		flex-direction: column;
	}

	.palette::backdrop {
		background: var(--scrim);
	}

	.search {
		display: flex;
		flex: none;
		align-items: center;
		gap: var(--space-2-5);
		min-height: var(--strip);
		padding: 0 var(--pad-x);
		border-bottom: var(--border);
		color: var(--faint);
	}

	.search input {
		flex: 1;
		min-width: 0;
		border: 0;
		background: transparent;
		color: var(--fg);
		font-size: max(var(--text-md), var(--text-field));
		outline: 0;
	}

	.search input::-webkit-search-cancel-button {
		display: none;
	}

	.list {
		flex: 1;
		min-height: 0;
		padding: var(--space-1) var(--space-2) var(--space-2);
		overflow-y: auto;
	}

	.head {
		padding: var(--space-2) var(--space-2) var(--space-1);
		color: var(--mute);
		font-size: var(--text-xs);
		font-weight: var(--weight-semibold);
	}

	footer {
		display: flex;
		flex: none;
		gap: var(--space-4);
		padding: var(--space-2) var(--pad-x);
		border-top: var(--border);
		color: var(--mute);
		font-size: var(--text-xs);
	}

	@media (max-width: 640px) {
		.palette {
			position: fixed;
			width: auto;
			max-width: none;
			max-height: 75vh;
			margin: auto var(--space-2) var(--space-2);
		}

		footer {
			display: none;
		}
	}
</style>