~ 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
109 lines · 3.6 KB Raw History
<script lang="ts" module>
	import type { Opener } from './open';
	import type { Source } from './types';

	/** A field's value that is no array, group or JSON, as it is drawn;
	 * `open` what a click on a record or an account opens, where the app
	 * opens it (a link, where it has an address), or what a click on a page
	 * does. */
	export type Leaf =
		| { kind: 'text'; text: string; mono: boolean }
		| { kind: 'datetime'; iso: string }
		| { kind: 'record'; uri: string; open?: Opener }
		| { kind: 'link'; url: string; open?: () => void }
		| { kind: 'actor'; actor: string; label: string; open?: Opener }
		| { kind: 'image'; source: Source | null; mimeType: string; size: number | null }
		| { kind: 'audio'; url: string; mimeType: string; size: number | null }
		| { kind: 'file'; mimeType: string; size: number | null }
		| { kind: 'scalar'; text: string };
</script>

<script lang="ts">
	import ClampedText from './ClampedText.svelte';
	import LinkButton from './LinkButton.svelte';
	import Picture from './Picture.svelte';
	import { formatBytes, formatDateTime } from './format';
	import { clock } from './view/clock.svelte';

	// One value of a record's field drawn as what it is: text, a date, a
	// link to a record, an account or a page, an image or audio with its
	// type and size, a file's type and size, or a number or boolean. Drawn
	// by the server-resolved tree's Field.
	let { leaf }: { leaf: Leaf } = $props();

	function openLink(event: MouseEvent, open: (() => void) | undefined) {
		if (!open) return;
		event.preventDefault();
		open();
	}
</script>

{#if leaf.kind === 'text'}
	<ClampedText text={leaf.text} mono={leaf.mono} />
{:else if leaf.kind === 'datetime'}
	<time datetime={leaf.iso} title={leaf.iso}>{formatDateTime(leaf.iso, clock.fixed)}</time>
{:else if leaf.kind === 'record'}
	{#if leaf.open}
		<LinkButton mono onclick={leaf.open.run} href={leaf.open.href}>{leaf.uri}</LinkButton>
	{:else}
		<span class="mono">{leaf.uri}</span>
	{/if}
{:else if leaf.kind === 'link'}
	{@const open = leaf.open}
	<a class="external" href={leaf.url} target="_blank" rel="noopener noreferrer" onclick={(event) => openLink(event, open)}>{leaf.url}</a>
{:else if leaf.kind === 'actor'}
	{#if leaf.open}
		<LinkButton onclick={leaf.open.run} href={leaf.open.href} title={leaf.actor}>{leaf.label}</LinkButton>
	{:else}
		<span title={leaf.actor}>{leaf.label}</span>
	{/if}
{:else if leaf.kind === 'image'}
	<span class="image"><Picture src={leaf.source} alt="" fit="contain" /></span>
	<span class="meta">{leaf.mimeType}{leaf.size === null ? '' : ` · ${formatBytes(leaf.size)}`}</span>
{:else if leaf.kind === 'audio'}
	<audio class="audio" controls preload="none" src={leaf.url}></audio>
	<span class="meta">{leaf.mimeType}{leaf.size === null ? '' : ` · ${formatBytes(leaf.size)}`}</span>
{:else if leaf.kind === 'file'}
	<span class="meta">{leaf.mimeType}{leaf.size === null ? '' : ` · ${formatBytes(leaf.size)}`}</span>
{:else}
	<span class="mono">{leaf.text}</span>
{/if}

<style>
	.mono {
		font-family: var(--font-mono);
		font-size: 0.92em;
		overflow-wrap: anywhere;
	}

	.external {
		color: var(--color-text-info);
		overflow-wrap: anywhere;
		text-decoration-color: color-mix(in srgb, currentColor 40%, transparent);
		text-underline-offset: 0.15em;
	}

	.external:hover {
		text-decoration-color: currentColor;
	}

	.audio {
		display: block;
		width: min(100%, 22rem);
		height: 2.25rem;
	}

	.image {
		display: block;
		width: min(100%, 14rem);
		height: 10rem;
		border-radius: var(--border-radius-sm);
		overflow: hidden;
	}

	.meta {
		display: block;
		color: var(--color-text-tertiary);
		font-size: var(--font-text-xs-size);
	}
</style>