~ 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
438 lines · 12.4 KB Raw History
<script lang="ts" module>
	export type Width = 'column' | 'wide' | 'phone' | 'full';
	export type Shown = 'preview' | 'tree' | 'data';
</script>

<script lang="ts">
	import { Loading, type ActionHandler, type UiToolHandler, type UiUploadHandler } from '@hud/renderer';
	import Result from '@hud/shared/Result.svelte';
	import { pageOf, type Call, type Entry } from '@hud/shared/result';
	import CodeEditor from '../atoms/CodeEditor.svelte';
	import Icon from '../atoms/Icon.svelte';
	import Spinner from '../atoms/Spinner.svelte';
	import ThemeSwatch from '../atoms/ThemeSwatch.svelte';
	import Time from '../atoms/Time.svelte';
	import Segmented from '../molecules/Segmented.svelte';
	import { readable } from '../playground/editor';
	import type { Opened, Status } from '../playground/playground.svelte';
	import { styleOf, themeId, type Theme } from '../theme';
	import { learn, summary as callSummary, tileTitle } from '../tiles';

	/**
	 * What the view draws, as a tile on the workspace's canvas: at a
	 * column's width, a wide one's, a phone's or the pane's, in a theme of
	 * HUD's or as the page is; or the tree it resolved to (`_meta.ui`), or
	 * the answer it was drawn over, as JSON. Over it, what it shows and how,
	 * and refreshing it; under that, when it was read and as whom, or why it
	 * is not drawn.
	 * What a drawing opens is said, not followed, and can be taken up as
	 * the call; nothing it does writes.
	 */
	let {
		entry,
		data,
		summary,
		description,
		status,
		busy,
		problems,
		opened,
		themes,
		width = $bindable('column'),
		theme = $bindable(null),
		shown = $bindable('preview'),
		onread,
		oncall,
		onfollow,
		ondismiss
	}: {
		entry: Entry | null;
		/** The answer it was drawn over, as read. */
		data: unknown;
		/** The tool's text, as read: what a tile's title learns from. */
		summary: string;
		/** The view's description, said where its title is hovered. */
		description: string | null;
		status: Status | null;
		busy: boolean;
		/** How many problems the server found with the view, and the call's and the view's JSON errors: nothing is drawn, the last drawing stays. */
		problems: number;
		opened: Opened | null;
		themes: Theme[];
		width?: Width;
		/** The theme it is drawn in, by its id; none, as the page is. */
		theme?: string | null;
		shown?: Shown;
		onread: () => void;
		oncall: (call: Call) => void;
		onfollow: (opened: Opened) => void;
		ondismiss: () => void;
	} = $props();

	let themeOpen = $state(false);

	const chosen = $derived(themes.find((t) => themeId(t) === theme) ?? null);
	const view = $derived(entry?.view ?? null);
	const handle = $derived.by(() => {
		const page = view ? pageOf(view) : null;
		if (page?.handle) return page.handle;
		const args = entry?.call?.args ?? {};
		const params = typeof args.params === 'object' && args.params !== null ? (args.params as Record<string, unknown>) : {};
		const actor = args.actor ?? params.actor;
		return typeof actor === 'string' && !actor.startsWith('did:') ? actor : null;
	});
	/** Its title, as the workspace titles a tile of the same call. */
	const heading = $derived(entry?.call ? tileTitle({ tool: entry.call.name, arguments: entry.call.args, title: '' }, view ? learn(view, summary) : null, null) : 'Preview');
	const json = $derived.by(() => {
		if (shown === 'tree') return view?.renderer === 'tree' ? JSON.stringify(view.ui, null, 2) : '';
		if (shown === 'data') return data === undefined ? '' : JSON.stringify(data, null, 2);
		return '';
	});

	/** Nothing a drawing does writes here: it says so where it would. */
	const REFUSED = 'The playground does not write: try it on the workspace.';
	const refuse: ActionHandler = async () => {
		throw new Error(REFUSED);
	};
	const refuseTool: UiToolHandler = async () => {
		throw new Error(REFUSED);
	};
	const refuseUpload: UiUploadHandler = async () => {
		throw new Error(REFUSED);
	};

	const WIDTHS: { value: Width; label: string }[] = [
		{ value: 'column', label: 'Column' },
		{ value: 'wide', label: 'Wide' },
		{ value: 'phone', label: 'Phone' },
		{ value: 'full', label: 'Full' }
	];
	const SHOWN: { value: Shown; label: string; title: string }[] = [
		{ value: 'preview', label: 'Preview', title: 'What the view draws' },
		{ value: 'tree', label: 'Tree', title: 'The tree it resolved to, _meta.ui' },
		{ value: 'data', label: 'Data', title: 'The answer it was drawn over' }
	];

	function choose(next: Theme | null) {
		themeOpen = false;
		theme = next ? themeId(next) : null;
	}
</script>

{#snippet who(viewer: Status['viewer'])}
	{viewer ? `as ${viewer.handle ? `@${viewer.handle}` : viewer.did}` : 'signed out'}
{/snippet}

{#snippet row(option: Theme | null)}
	{@const id = option ? themeId(option) : null}
	<button type="button" class="menu-item" role="menuitemradio" aria-checked={id === theme} onclick={() => choose(option)}>
		{#if option?.palette}
			<ThemeSwatch palette={option.palette} />
		{:else}
			<span class="mark"><Icon name={option ? (option.colorScheme === 'dark' ? 'dark' : 'light') : 'system'} /></span>
		{/if}
		<span class="grow">{option?.name ?? 'As the page is'}</span>
		<span class="check">{#if id === theme}<Icon name="check" />{/if}</span>
	</button>
{/snippet}

<section class="preview" aria-label="Preview">
	<header class="bar">
		<Segmented tabs label="Show" value={shown} options={SHOWN} onchange={(next) => (shown = next)} />
		{#if shown === 'preview'}
			<span class="widths"><Segmented label="Width" value={width} options={WIDTHS} onchange={(next) => (width = next)} /></span>
		{/if}
		<span class="end">
			<details class="popover sheet end" bind:open={themeOpen}>
				{#if chosen}
					<summary class="btn sm" title="Theme of the preview">
						{#if chosen.palette}<ThemeSwatch palette={chosen.palette} />{:else}<Icon name={chosen.colorScheme === 'dark' ? 'dark' : 'light'} />{/if}<span class="theme-name">{chosen.name}</span>
					</summary>
				{:else}
					<summary class="icon-button" title="Theme of the preview" aria-label="Theme of the preview"><Icon name="theme" /></summary>
				{/if}
				<div class="menu" role="menu">
					<p class="menu-head">Draw the preview in</p>
					{@render row(null)}
					{#each themes as option (themeId(option))}
						{@render row(option)}
					{/each}
				</div>
			</details>
			<button type="button" class="btn sm" title="Make the call again, afresh" disabled={busy} onclick={onread}><Icon name="refresh" />Refresh</button>
		</span>
	</header>
	<p class="status" class:problem={problems > 0} aria-live="polite">
		{#if problems}
			<Icon name="issue" /><span>Not drawn: {problems === 1 ? '1 problem' : `${problems} problems`} in the editor.{entry ? ' Showing the last drawing' : ''}</span>
		{:else if busy}
			<Spinner label="Drawing" /><span>Drawing…</span>
		{:else if status?.how === 'drawn'}
			<span>Drawn <Time at={status.at} /> · read <Time at={status.read} />, {@render who(status.viewer)}</span>
		{:else if status}
			<span>Read <Time at={status.at} />, {@render who(status.viewer)}</span>
		{:else}
			<span>Not drawn yet</span>
		{/if}
	</p>
	<div class="stage" style={chosen ? styleOf(chosen) : undefined} data-palette={chosen?.palette ? '' : undefined}>
		{#if shown === 'preview'}
			<article class="tile {width}" aria-label={heading}>
				<header class="head">
					<h2 title={description ?? heading}>{heading}</h2>
					{#if handle}<span class="who">@{handle}</span>{/if}
				</header>
				<div class="body" aria-busy={entry === null}>
					{#if entry === null && problems}
						<p class="empty-state">Nothing drawn yet</p>
					{:else if entry === null}
						<Loading label="Drawing the view…" />
					{:else}
						<Result
							{entry}
							oncall={(call) => oncall(call)}
							onreplace={(call) => oncall(call)}
							onaction={refuse}
							onopenlink={(url) => void window.open(url, '_blank', 'noopener,noreferrer')}
							onretry={onread}
							ontool={refuseTool}
							onupload={refuseUpload}
							shown={{ title: heading, handle }}
						/>
					{/if}
				</div>
			</article>
		{:else}
			<div class="json">
				{#if json}
					{#key shown}
						<CodeEditor value={json} extensions={readable()} label={shown === 'tree' ? 'Resolved tree' : 'Answer'} />
					{/key}
				{:else}
					<p class="empty-state">{shown === 'tree' ? 'No tree: nothing drawn yet' : 'No answer: nothing read yet'}</p>
				{/if}
			</div>
		{/if}
	</div>
	{#if opened}
		<div class="opened" role="status">
			<Icon name="info" />
			<p>Opens <code>{callSummary(opened.call.name, opened.call.args)}</code></p>
			{#if opened.tool}
				<button type="button" class="btn sm" onclick={() => onfollow(opened)}>{opened.template ? `Try it with ${opened.template.name}` : 'Try this call'}</button>
			{/if}
			<button type="button" class="icon-button" aria-label="Dismiss" title="Dismiss" onclick={ondismiss}><Icon name="close" /></button>
		</div>
	{/if}
</section>

<style>
	.preview {
		display: flex;
		flex-direction: column;
		height: 100%;
		min-height: 0;
		min-width: 0;
	}

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

	.end {
		display: flex;
		align-items: center;
		gap: var(--space-2);
		margin-left: auto;
	}

	.mark {
		display: inline-grid;
		flex: none;
		place-items: center;
		width: var(--swatch);
	}

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

	.check {
		display: inline-grid;
		flex: none;
		place-items: center;
		width: var(--icon);
	}

	/* Chrome under the bar, one line tall whatever it says, so the tile never moves. */
	.status {
		display: flex;
		flex: none;
		align-items: center;
		gap: var(--space-1-5);
		height: var(--map);
		padding: 0 var(--space-3);
		overflow: hidden;
		border-bottom: var(--border);
		background: var(--panel);
		color: var(--mute);
		font-size: var(--text-xs);
		white-space: nowrap;
	}

	.status > span {
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.status.problem {
		color: var(--danger);
	}

	.theme-name {
		max-width: var(--tab-name);
		overflow: hidden;
		text-overflow: ellipsis;
	}

	/* The canvas, in the preview's theme: a tile on it, as wide as chosen, as tall as it is, and a word under it. */
	.stage {
		display: flex;
		flex: 1;
		flex-direction: column;
		align-items: center;
		min-height: 0;
		padding: var(--gap) var(--gap) var(--space-2);
		background: var(--canvas);
		color: var(--fg);
	}

	.tile {
		display: flex;
		flex: 1;
		flex-direction: column;
		width: 100%;
		min-height: 0;
		border: var(--border);
		border-radius: var(--radius-lg);
		background: var(--bg);
	}

	.tile.column {
		max-width: var(--preview-column);
	}

	.tile.wide {
		max-width: var(--preview-wide);
	}

	.tile.phone {
		max-width: var(--preview-phone);
	}

	.head {
		display: flex;
		flex: none;
		align-items: baseline;
		gap: var(--space-1-5);
		min-height: var(--strip);
		padding: var(--space-3) var(--space-3);
		border-bottom: var(--border);
		border-radius: var(--radius-lg) var(--radius-lg) 0 0;
		background: var(--panel);
	}

	h2 {
		min-width: 0;
		overflow: hidden;
		font-size: var(--text-base);
		line-height: var(--leading-tight);
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.who {
		flex: none;
		color: var(--mute);
		font-size: var(--text-xs);
	}

	/* As a tile's body on the workspace: its own scroll, its rows meeting its edges. */
	.body {
		flex: 1;
		min-height: 0;
		padding: var(--space-3);
		--hud-bleed: var(--space-3);
		overflow-x: hidden;
		overflow-y: auto;
		overscroll-behavior-y: contain;
		border-radius: 0 0 var(--radius-lg) var(--radius-lg);
	}

	.body > :global(.rows:first-child) {
		margin-top: calc(-1 * var(--space-3));
	}

	.body > :global(.rows:last-child) {
		margin-bottom: calc(-1 * var(--space-3));
	}

	.json {
		flex: 1;
		width: 100%;
		min-height: 0;
		overflow: hidden;
		border: var(--border);
		border-radius: var(--radius-lg);
		background: var(--bg);
	}

	.json .empty-state {
		padding: var(--space-4);
	}

	.opened {
		display: flex;
		flex: none;
		align-items: center;
		gap: var(--space-2);
		padding: var(--space-2) var(--space-3);
		border-top: var(--border);
		background: var(--panel);
		color: var(--mute);
		font-size: var(--text-xs);
	}

	.opened p {
		flex: 1;
		min-width: 0;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.opened code {
		color: var(--fg);
		font-family: var(--font-mono);
	}

	/* On a phone, the preview is the screen's width: no width to choose. */
	@media (max-width: 640px) {
		.widths {
			display: none;
		}

		.stage {
			padding: var(--space-2) var(--space-2) var(--space-1);
		}
	}
</style>