~ 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
496 lines · 13.9 KB Raw History
<script lang="ts">
	import { onMount } from 'svelte';
	import AppIcon from '../atoms/AppIcon.svelte';
	import Avatar from '../atoms/Avatar.svelte';
	import Handle from '../atoms/Handle.svelte';
	import Icon from '../atoms/Icon.svelte';
	import { launcher, type Row } from '../launcher.svelte';
	import AppCell from '../molecules/AppCell.svelte';
	import LauncherRow from '../molecules/LauncherRow.svelte';
	import type { Actor, Canvas } from '../model';
	import type { Opening } from '../workspace.svelte';

	/**
	 * A launcher column, in place of the sidebar: your atmosphere. Unsearched,
	 * the account (opening your profile across your apps), then its apps
	 * (most collections first, two rows of them, opening each app's page),
	 * the chosen app's entry points (its feeds, the queries its bundled views
	 * offer, the collections a view draws), then the canvases. A search goes
	 * through all of it. A pick turns the column into that tile in place
	 * (`onpick`, its call); one already on the strip says so, and goes to it;
	 * a canvas opens as its tab (`oncanvas`). The arrows move through the grid
	 * as a grid and the lists in turn; Escape empties the search, then
	 * closes an empty launcher; ← and → in an empty search, off the grid,
	 * move the focus.
	 */
	let {
		me,
		focused,
		readonly,
		empty,
		canvases,
		opened,
		onfocus,
		onpick,
		oncanvas,
		onclose,
		onstep,
		onconnect
	}: {
		me: Actor;
		focused: boolean;
		readonly: boolean;
		/** The canvas has no tiles yet: the launcher says so. */
		empty: boolean;
		/** The person's other canvases, to open. */
		canvases: Canvas[];
		/** Whether a row's call is on the strip already. */
		opened: (row: Row) => boolean;
		onfocus: () => void;
		onpick: (call: Opening) => void;
		oncanvas: (id: string) => void;
		onclose: () => void;
		onstep: (step: -1 | 1) => void;
		onconnect: () => void;
	} = $props();

	/** The grid's columns, and the cells it shows: two rows. */
	const COLUMNS = 4;
	const CELLS = 2 * COLUMNS;
	/** The canvases shown unsearched, the latest changed first. */
	const CANVASES = 5;

	type Item = { key: string; pick: () => void };

	let query = $state('');
	let active = $state<string | null>(null);
	/** The app whose entry points show, as the arrows last went to it: by its domain. */
	let chosen = $state<string | null>(null);

	const q = $derived(query.trim().toLowerCase());
	const shown = $derived(launcher.rows.filter((row) => !q || row.find.includes(q)));
	const mine = $derived(shown.filter((row) => row.section === 'mine'));
	const views = $derived(shown.filter((row) => row.section === 'views'));
	const feeds = $derived(shown.filter((row) => row.section === 'feeds'));
	const apps = $derived(shown.filter((row) => row.section === 'apps'));
	const byApp = $derived(Map.groupBy(apps, (row) => row.group ?? ''));

	/** Unsearched: the apps the grid has room for. */
	const grid = $derived(mine.slice(0, CELLS));
	const selected = $derived(mine.find((row) => row.domain === chosen) ?? mine[0] ?? null);
	/** The selected app's entry points: its queries, the collections a view draws (feeds are their own section). */
	const entries = $derived.by(() => {
		const domain = selected?.domain;
		if (!domain) return [];
		return [...apps.filter((row) => row.domain === domain), ...views.filter((row) => row.domain === domain)];
	});
	const places = $derived(
		canvases
			.filter((canvas) => !q || canvas.name.toLowerCase().includes(q))
			.toSorted((a, b) => b.updatedAt.localeCompare(a.updatedAt))
			.slice(0, q ? undefined : CANVASES)
	);
	/** What Enter and the arrows go through, in the order they show. */
	const reachable = $derived.by((): Item[] => {
		const rows = q ? [...mine, ...views, ...feeds, ...apps] : [...grid, ...feeds, ...entries];
		return [...rows.map((row) => ({ key: row.key, pick: () => onpick(row.call) })), ...places.map((canvas) => ({ key: `canvas:${canvas.id}`, pick: () => oncanvas(canvas.id) }))];
	});
	/** A search's first row, which it picks; none unsearched. */
	const first = $derived(q ? (reachable[0]?.key ?? null) : null);

	$effect(() => {
		active = first;
	});

	function move(to: number) {
		const next = Math.min(Math.max(to, 0), reachable.length - 1);
		active = reachable[next]?.key ?? null;
		const cell = !q && next < grid.length ? grid[next] : null;
		if (cell?.domain) chosen = cell.domain;
		document.querySelector(`[data-launcher-row="${CSS.escape(active ?? '')}"]`)?.scrollIntoView({ block: 'nearest' });
	}

	function keydown(event: KeyboardEvent) {
		if (event.metaKey || event.ctrlKey || event.altKey) return;
		const at = reachable.findIndex((item) => item.key === active);
		const inGrid = !q && at >= 0 && at < grid.length;
		if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
			event.preventDefault();
			const down = event.key === 'ArrowDown';
			if (at < 0) move(down ? 0 : reachable.length - 1);
			// In the grid, a row at a time: down from its last row to the list under it, from a shorter last row's gap to its last cell.
			else if (inGrid && down) move(at + COLUMNS < grid.length ? at + COLUMNS : Math.floor(at / COLUMNS) === Math.floor((grid.length - 1) / COLUMNS) ? grid.length : grid.length - 1);
			else if (inGrid) move(at - COLUMNS >= 0 ? at - COLUMNS : at);
			else move(at + (down ? 1 : -1));
		} else if (event.key === 'Enter') {
			event.preventDefault();
			reachable[Math.max(at, 0)]?.pick();
		} else if (event.key === 'Escape') {
			event.preventDefault();
			event.stopPropagation();
			if (query) query = '';
			else onclose();
		} else if (!query && (event.key === 'ArrowLeft' || event.key === 'ArrowRight')) {
			event.preventDefault();
			const step = event.key === 'ArrowLeft' ? -1 : 1;
			// In the grid, a cell at a time; past its ends, and off it, the next column.
			if (inGrid && at + step >= 0 && at + step < grid.length) move(at + step);
			else onstep(step);
		}
	}

	onMount(() => launcher.start(me));
</script>

{#snippet rows(list: Row[])}
	<ul role="listbox" aria-label="Rows">
		{#each list as row (row.key)}
			<li data-launcher-row={row.key}>
				<LauncherRow
					text={row.text}
					sub={row.sub}
					icon={row.icon}
					domain={row.domain}
					picture={row.picture}
					tag={row.tag}
					open={opened(row)}
					active={row.key === active}
					onpick={() => onpick(row.call)}
					onhover={() => (active = row.key)}
				/>
			</li>
		{/each}
	</ul>
{/snippet}

{#snippet feedsState()}
	{#if launcher.feedsError}
		<p class="problem">{launcher.feedsError} <button type="button" class="text-button" onclick={() => void launcher.loadFeeds()}>Try again</button></p>
	{:else if launcher.feeds === null}
		<p class="empty-state">Loading your feeds…</p>
	{/if}
{/snippet}

<!-- svelte-ignore a11y_no_noninteractive_element_interactions (a pointer anywhere in a column focuses it; the keys move focus too) -->
<section class="launcher" class:focused aria-label="Open" onpointerdown={onfocus}>
	<header class="head">
		<Icon name="add" />
		<div class="titles">
			<h2>New tile</h2>
			<p>{empty ? 'This canvas is empty: search, or pick from your atmosphere' : 'Search, or pick from your atmosphere'}</p>
		</div>
		<button type="button" class="icon-button" title="Close (Esc)" aria-label="Close" onclick={onclose}><Icon name="close" /></button>
	</header>
	<div class="body">
		<label class="search">
			<Icon name="search" />
			<input bind:value={query} type="search" placeholder="Search your apps, feeds and canvases" aria-label="Search your atmosphere" autocomplete="off" spellcheck="false" onkeydown={keydown} />
		</label>

		<div class="account">
			<button type="button" class="me" title="Your profile across your apps" onclick={() => onpick({ tool: 'describe_repo', arguments: { actor: me.did } })}>
				<Avatar did={me.did} handle={me.handle} size="md" />
				<span class="names">
					<strong>{launcher.account.name}</strong>
					<span><Handle did={me.did} handle={me.handle} /></span>
				</span>
			</button>
			<button type="button" class="icon-button" title="Connect your AI" aria-label="Connect your AI" onclick={onconnect}><Icon name="connect" /></button>
		</div>

		{#if readonly}
			<p class="note">You’re a viewer of this canvas: you can look, but only its owner and editors can put things on it.</p>
		{/if}

		{#if launcher.repoError}
			<p class="problem">{launcher.repoError} <button type="button" class="text-button" onclick={() => void launcher.loadRepo()}>Try again</button></p>
		{:else if !launcher.repo}
			<p class="empty-state">Loading your apps…</p>
		{:else if !q}
			<section>
				<h3>Your apps</h3>
				{#if launcher.mine.length === 0}
					<p class="empty-state">No apps in your repository yet</p>
				{:else}
					<ul class="grid" role="listbox" aria-label="Your apps">
						{#each grid as row (row.key)}
							<li data-launcher-row={row.key}>
								<AppCell
									text={row.text}
									domain={row.domain}
									icon={row.icon ?? 'app'}
									active={row.key === active}
									selected={row.key === selected?.key}
									onpick={() => onpick(row.call)}
									onhover={() => (active = row.key)}
								/>
							</li>
						{/each}
					</ul>
				{/if}
			</section>

			<section>
				<h3>Feeds</h3>
				{@render rows(feeds)}
				{@render feedsState()}
			</section>

			{#if selected}
				<section>
					<h3 class="app-head"><AppIcon domain={selected.domain} fallback="app" />{selected.text}</h3>
					{@render rows(entries)}
					{#if entries.length === 0}
						<p class="empty-state">Nothing more to open in {selected.text}: its page is above</p>
					{/if}
				</section>
			{/if}
		{:else}
			{#if mine.length > 0}
				<section>
					<h3>Apps</h3>
					{@render rows(mine)}
				</section>
			{/if}

			{#if views.length > 0}
				<section>
					<h3>On atproto</h3>
					{@render rows(views)}
				</section>
			{/if}

			{#if feeds.length > 0}
				<section>
					<h3>Feeds</h3>
					{@render rows(feeds)}
					{@render feedsState()}
				</section>
			{/if}

			{#each byApp as [app, list] (app)}
				<section>
					<h3>{app}</h3>
					{@render rows(list)}
				</section>
			{/each}
		{/if}

		{#if !q || places.length > 0}
			<section>
				<h3>Canvases</h3>
				{#if places.length === 0}
					<p class="empty-state">No other canvases</p>
				{:else}
					<ul role="listbox" aria-label="Canvases">
						{#each places as canvas (canvas.id)}
							{@const key = `canvas:${canvas.id}`}
							<li data-launcher-row={key}>
								<LauncherRow
									text={canvas.name}
									icon="canvas"
									tag={{ text: String(canvas.tiles), kind: 'count' }}
									active={key === active}
									onpick={() => oncanvas(canvas.id)}
									onhover={() => (active = key)}
								/>
							</li>
						{/each}
					</ul>
				{/if}
			</section>
		{/if}

		{#if q && reachable.length === 0}
			<p class="empty-state">Nothing in your atmosphere matches “{query.trim()}”</p>
		{/if}
	</div>
</section>

<style>
	.launcher {
		display: flex;
		flex: 1 1 0;
		flex-direction: column;
		min-height: 0;
		border: var(--border);
		border-radius: var(--radius-lg);
		background: var(--bg);
		overflow: hidden;
	}

	.launcher.focused {
		border-color: var(--primary);
		box-shadow: 0 0 0 1px var(--primary);
	}

	.head {
		display: flex;
		flex: none;
		align-items: center;
		gap: var(--space-2);
		min-height: var(--strip);
		padding: var(--space-2) var(--space-2) var(--space-2) var(--space-3);
		border-bottom: var(--border);
		background: var(--panel);
	}

	.head > :global(.i) {
		color: var(--mute);
	}

	.titles {
		flex: 1;
		min-width: 0;
	}

	h2 {
		font-size: var(--text-base);
		line-height: var(--leading-tight);
	}

	.titles p {
		color: var(--mute);
		font-size: var(--text-xs);
	}

	.body {
		display: flex;
		flex: 1;
		flex-direction: column;
		gap: var(--space-4);
		min-height: 0;
		padding: 0 var(--space-3) var(--space-3);
		overflow-y: auto;
		/* As a tile's body: a swipe sideways over it goes on to the strip. */
		overscroll-behavior-y: contain;
	}

	.search {
		position: sticky;
		top: 0;
		z-index: 1;
		display: flex;
		align-items: center;
		gap: var(--space-2);
		margin: 0 calc(-1 * var(--space-3));
		padding: var(--space-3) var(--space-3) var(--space-2);
		background: var(--bg);
		color: var(--faint);
	}

	.search input {
		flex: 1;
		min-width: 0;
		height: var(--control);
		padding: 0 var(--space-2-5);
		border: var(--border);
		border-radius: var(--radius-md);
		background: var(--bg);
		color: var(--fg);
	}

	.search input:focus {
		outline: 0;
		border-color: var(--primary);
		box-shadow: var(--ring);
	}

	.account {
		display: flex;
		align-items: center;
		gap: var(--space-2);
		padding-bottom: var(--space-3);
		border-bottom: var(--border);
	}

	/* The account, opening your profile across your apps: a row's look, its hover inside the column as a row's is. */
	.me {
		display: flex;
		flex: 1;
		align-items: center;
		gap: var(--space-2);
		min-width: 0;
		margin-block: calc(-1 * var(--space-1));
		padding: var(--space-1);
		border: 0;
		border-radius: var(--radius-md);
		background: none;
		color: var(--fg);
		font: inherit;
		text-align: left;
		cursor: pointer;
	}

	.me:hover {
		background: var(--hover);
	}

	.names {
		display: flex;
		flex-direction: column;
		min-width: 0;
		line-height: var(--leading-tight);
	}

	.names strong,
	.names span {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.names span {
		color: var(--mute);
		font-size: var(--text-xs);
	}

	.note {
		padding: var(--space-2) var(--space-3);
		border-radius: var(--radius-md);
		background: var(--notice);
		color: var(--notice-fg);
		font-size: var(--text-sm);
	}

	section {
		display: flex;
		flex-direction: column;
		gap: var(--space-1);
	}

	h3 {
		padding: 0 var(--space-2);
		color: var(--mute);
		font-size: var(--text-xs);
		letter-spacing: 0;
	}

	.app-head {
		display: flex;
		align-items: center;
		gap: var(--space-2);
	}

	ul {
		display: flex;
		flex-direction: column;
		gap: 1px;
		margin: 0;
		padding: 0;
		list-style: none;
	}

	/* Your apps: four to a row, at most two rows. */
	ul.grid {
		display: grid;
		grid-template-columns: repeat(4, minmax(0, 1fr));
		gap: var(--space-2);
		margin-top: var(--space-1);
	}

	.problem,
	.empty-state {
		padding: 0 var(--space-2);
	}
</style>