~ 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
314 lines · 7.1 KB Raw History
<script lang="ts">
	import { Glyph } from '@hud/renderer';
	import type { Call } from '@hud/shared/result';
	import type { Client } from './client';

	// Search beside the side navigation, sliding out of it as Grain web's
	// SearchFlyout.svelte does: people as you type (the app's typeahead, its
	// call's `params` given `q`), Enter or the first row searching the app for the
	// text, a person opening their profile. Each is a screen of the app's
	// own, opened by the shell.
	type Actor = { did: string; handle?: string; displayName?: string; avatar?: string };

	let {
		open = $bindable(false),
		client,
		typeahead,
		onsearch,
		onactor
	}: {
		open?: boolean;
		client: Client;
		/** The app's people typeahead. */
		typeahead: Call;
		onsearch: (q: string) => void;
		onactor: (did: string) => void;
	} = $props();

	let value = $state('');
	let actors: Actor[] = $state([]);
	let active = $state(-1);
	let searched = $state(false);
	let input: HTMLInputElement | undefined = $state();
	let timer: ReturnType<typeof setTimeout> | undefined;
	/** The text last asked for: an answer to an older one is dropped. */
	let asked = '';

	$effect(() => {
		if (open) input?.focus();
		else reset();
	});

	function reset() {
		value = '';
		actors = [];
		active = -1;
		searched = false;
	}

	function typed() {
		const q = value.trim();
		clearTimeout(timer);
		if (q.length < 2) {
			actors = [];
			searched = false;
			return;
		}
		timer = setTimeout(() => void find(q), 200);
	}

	async function find(q: string) {
		asked = q;
		const args = typeahead.args as { params?: Record<string, unknown> };
		const result = await client.callTool(typeahead.name, { ...args, params: { ...args.params, q } }).catch(() => null);
		if (asked !== q) return;
		const data = result && !result.isError ? (result.structuredContent as Record<string, unknown> | null) : null;
		const found = ((data && 'response' in data ? data.response : data) as { actors?: unknown } | null)?.actors;
		actors = Array.isArray(found) ? (found as Actor[]) : [];
		active = -1;
		searched = true;
	}

	function submit() {
		const q = value.trim();
		if (!q) return;
		open = false;
		onsearch(q);
	}

	function pick(actor: Actor) {
		open = false;
		onactor(actor.did);
	}

	function keydown(event: KeyboardEvent) {
		const total = actors.length + 1;
		if (event.key === 'ArrowDown' && total > 1) active = Math.min(active + 1, total - 1);
		else if (event.key === 'ArrowUp' && total > 1) active = Math.max(active - 1, -1);
		else if (event.key === 'Enter') {
			if (active > 0) pick(actors[active - 1]);
			else submit();
		} else return;
		event.preventDefault();
	}
</script>

<svelte:window
	onkeydown={(event) => {
		if (open && event.key === 'Escape') open = false;
	}}
/>

{#if open}
	<button type="button" class="scrim" aria-label="Close search" tabindex="-1" onclick={() => (open = false)}></button>
	<aside class="flyout">
		<h2>Search</h2>
		<div class="field">
			<svg class="lead" viewBox="0 0 24 24" aria-hidden="true"><Glyph name="search" /></svg>
			<input bind:this={input} bind:value type="text" placeholder="Search people…" autocomplete="off" oninput={typed} onkeydown={keydown} />
			{#if value}
				<button type="button" class="clear" aria-label="Clear search" onclick={reset}>
					<svg viewBox="0 0 24 24" aria-hidden="true"><Glyph name="x" /></svg>
				</button>
			{/if}
		</div>
		{#if value.trim().length >= 2}
			<div class="results">
				<button type="button" class="row" class:active={active === 0} onclick={submit}>
					<span class="row-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><Glyph name="search" /></svg></span>
					<span class="name">{value.trim()}</span>
				</button>
				{#each actors as actor, i (actor.did)}
					<button type="button" class="row" class:active={active === i + 1} onclick={() => pick(actor)}>
						{#if actor.avatar}<img class="avatar" src={actor.avatar} alt="" />{:else}<span class="avatar"></span>{/if}
						<span class="text" translate="no">
							<span class="name">{actor.displayName || actor.handle || actor.did}</span>
							{#if actor.handle}<span class="handle">@{actor.handle}</span>{/if}
						</span>
					</button>
				{/each}
				{#if searched && !actors.length}<p class="empty">No people found.</p>{/if}
			</div>
		{/if}
	</aside>
{/if}

<style>
	/* Stops short of the sidebar, which stays clickable beside it. */
	.scrim {
		position: fixed;
		inset: 0 0 0 var(--hud-frame-nav-end);
		z-index: 4;
		padding: 0;
		border: 0;
		background: rgb(0 0 0 / 0.35);
		cursor: default;
	}

	.flyout {
		position: fixed;
		inset: 0 auto 0 var(--hud-frame-nav-end);
		z-index: 5;
		display: flex;
		flex-direction: column;
		gap: 14px;
		width: 392px;
		max-width: calc(100vw - var(--hud-frame-nav-end));
		padding: 20px 14px;
		overflow-y: auto;
		background: var(--app-panel, var(--color-background-secondary));
		box-shadow: 12px 0 34px rgb(0 0 0 / 0.28);
	}

	h2 {
		margin: 0 6px;
		font-family: var(--app-font-display, inherit);
		font-size: 22px;
		font-weight: 800;
		letter-spacing: -0.01em;
	}

	.field {
		position: relative;
	}

	svg {
		width: 20px;
		height: 20px;
		fill: none;
		stroke: currentColor;
		stroke-width: 2;
		stroke-linecap: round;
		stroke-linejoin: round;
	}

	.lead {
		position: absolute;
		top: 50%;
		left: 12px;
		width: 16px;
		height: 16px;
		color: var(--color-text-tertiary);
		transform: translateY(-50%);
		pointer-events: none;
	}

	input {
		width: 100%;
		padding: 10px 34px 10px 36px;
		border: 1px solid var(--color-border-secondary);
		border-radius: 10px;
		outline: none;
		background: var(--color-background-primary);
		color: var(--color-text-primary);
		font: inherit;
		font-size: 14px;
	}

	input::placeholder {
		color: var(--color-text-tertiary);
	}

	input:focus {
		border-color: var(--app-accent, var(--color-ring-primary));
	}

	.clear {
		position: absolute;
		top: 50%;
		right: 10px;
		display: flex;
		padding: 2px;
		border: 0;
		background: none;
		color: var(--color-text-secondary);
		transform: translateY(-50%);
		cursor: pointer;
	}

	.clear svg {
		width: 14px;
		height: 14px;
	}

	.results {
		display: flex;
		flex-direction: column;
	}

	.row {
		display: flex;
		align-items: center;
		gap: 11px;
		width: 100%;
		padding: 9px 8px;
		border: 0;
		border-radius: 10px;
		background: none;
		color: var(--color-text-primary);
		font: inherit;
		font-size: 14px;
		text-align: left;
		cursor: pointer;
	}

	.row:hover,
	.row.active {
		background: var(--color-background-secondary);
	}

	.row-icon {
		display: flex;
		flex: none;
		align-items: center;
		justify-content: center;
		width: 36px;
		height: 36px;
		color: var(--color-text-secondary);
	}

	.avatar {
		flex: none;
		width: 36px;
		height: 36px;
		border-radius: 50%;
		background: var(--color-background-tertiary);
		object-fit: cover;
	}

	.text {
		display: flex;
		flex-direction: column;
		min-width: 0;
	}

	.name,
	.handle {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.name {
		font-weight: 600;
	}

	.handle {
		color: var(--color-text-secondary);
		font-size: 12.5px;
	}

	.empty {
		margin: 0;
		padding: 10px 8px;
		color: var(--color-text-secondary);
		font-size: 13px;
	}

	@media (max-width: 600px) {
		.scrim,
		.flyout {
			display: none;
		}
	}
</style>