~ 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
101 lines · 2.6 KB Raw History
<script lang="ts">
	import { Tree, type UiItem, type UiNode, type UiTree } from '@hud/renderer';
	import { tick } from 'svelte';

	// A workspace column, as Tile draws one: a header, and a body that
	// scrolls, holding a feed's answer drawn as the server resolved it
	// (`_meta.ui`). `more` adds a page, as Load more does, its page list's
	// items after these in a new array replacing the old; `remount` stops
	// drawing it and draws it again, its scroll put back, as a column going
	// far out of view and coming back does.
	let { first }: { first: UiTree } = $props();

	// The first page only: a page more replaces it (`more`).
	// svelte-ignore state_referenced_locally
	let ui = $state.raw(first);
	let live = $state(true);
	let body: HTMLElement | undefined = $state();
	let kept = 0;

	$effect.pre(() => {
		if (!live && body) kept = body.scrollTop;
	});
	$effect(() => {
		if (!live || !body || !kept) return;
		body.scrollTop = kept;
		kept = 0;
	});

	/** A tree's page list: the node with `p.page` and items. */
	function pageOf(node: UiNode): UiNode | null {
		if (node.p?.page && node.items) return node;
		for (const k of node.k ?? []) {
			const found = pageOf(k);
			if (found) return found;
		}
		return null;
	}

	/** `node` with its page list's items followed by `extra`. */
	function grown(node: UiNode, extra: UiItem[]): UiNode {
		if (node.p?.page && node.items) return { ...node, items: [...node.items, ...extra] };
		return node.k ? { ...node, k: node.k.map((k) => grown(k, extra)) } : node;
	}

	export function more(next: UiTree) {
		ui = { ...ui, root: grown(ui.root, pageOf(next.root)?.items ?? []) };
	}

	export async function remount() {
		live = false;
		await tick();
		live = true;
		await tick();
	}

	const nothing = async () => ({ uri: null });
</script>

<main class="column">
	<header>What’s Hot</header>
	<div class="body" bind:this={body}>
		{#if live}
			<Tree {ui} onaction={nothing} oninteraction={async () => {}} onseen={() => {}} oncall={() => {}} onopenlink={() => {}} />
		{/if}
	</div>
</main>

<style>
	:global(body) {
		margin: 0;
		background: var(--color-background-secondary);
		font-family: var(--font-sans);
	}

	/* A third of a 1440 px strip, as a workspace column is by default. */
	.column {
		display: flex;
		flex-direction: column;
		width: 480px;
		height: 100vh;
		margin-left: 16px;
		background: var(--color-background-primary);
	}

	header {
		flex: none;
		height: 44px;
		padding: 0 12px;
		line-height: 44px;
		font-weight: 600;
	}

	.body {
		flex: 1;
		min-height: 0;
		padding: 12px;
		overflow-x: hidden;
		overflow-y: auto;
		overscroll-behavior: contain;
	}
</style>