~ 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
257 lines · 6.0 KB Raw History
<script lang="ts">
	import Avatar from '../atoms/Avatar.svelte';
	import Dot from '../atoms/Dot.svelte';
	import Icon from '../atoms/Icon.svelte';
	import Spinner from '../atoms/Spinner.svelte';
	import { handleText } from '../model';

	/**
	 * A canvas's tab: its name, who else is on it, and, while it is out of
	 * sight, what happened there (✦ and how many tiles an agent added, a dot
	 * for a member's change, a spinner while an agent works), and × to put
	 * it away. Its name, double-clicked, is renamed by its owner in place.
	 */
	let {
		id,
		name,
		active,
		title,
		present,
		agent = 0,
		changed = null,
		working = false,
		renaming,
		onselect,
		onclose,
		onrename,
		onstartrename
	}: {
		id: string;
		name: string;
		active: boolean;
		/** What it says on hover: its name, whose it is, its key. */
		title: string;
		present: { did: string; handle?: string }[];
		agent?: number;
		changed?: string | null;
		/** An agent is working there: nothing reports it yet. */
		working?: boolean;
		renaming: boolean;
		onselect: () => void;
		onclose: () => void;
		/** The name typed, or null when the rename was let go. */
		onrename: (name: string | null) => void;
		/** Null when it cannot be renamed here. */
		onstartrename: (() => void) | null;
	} = $props();

	let element: HTMLElement | undefined = $state();
	// The tab on screen is whole in its bar, which scrolls on a phone: as it
	// opens, and again as it grows (its faces and its × come after).
	$effect(() => {
		if (!active || !element) return;
		const tab = element;
		const show = () => tab.scrollIntoView({ block: 'nearest', inline: 'nearest' });
		show();
		const observer = new ResizeObserver(show);
		observer.observe(tab);
		return () => observer.disconnect();
	});

	let done = false;
	function field(node: HTMLInputElement) {
		done = false;
		node.focus();
		node.select();
	}
	function finish(value: string | null) {
		if (done) return;
		done = true;
		onrename(value?.trim() ? value.trim() : null);
	}
</script>

<div
	bind:this={element}
	class="tab"
	class:active
	role="tab"
	tabindex={active ? 0 : -1}
	aria-selected={active}
	data-tab={id}
	{title}
	onclick={(event) => {
		if (!(event.target as Element).closest('button, input')) onselect();
	}}
	onauxclick={(event) => {
		if (event.button === 1) onclose();
	}}
	onkeydown={(event) => {
		if (event.target === event.currentTarget && (event.key === 'Enter' || event.key === ' ')) {
			event.preventDefault();
			onselect();
		}
	}}
>
	<Icon name="canvas" />
	{#if renaming}
		<input
			class="rename"
			type="text"
			value={name}
			maxlength="80"
			aria-label="Canvas name"
			use:field
			onkeydown={(event) => {
				event.stopPropagation();
				if (event.key === 'Enter') finish(event.currentTarget.value);
				else if (event.key === 'Escape') finish(null);
			}}
			onblur={(event) => finish(event.currentTarget.value)}
		/>
	{:else}
		<!-- svelte-ignore a11y_no_static_element_interactions (renaming is the owner's, also from the palette) -->
		<span class="name" ondblclick={onstartrename}>{name}</span>
	{/if}
	{#if present.length > 0}
		<span class="here" aria-label="Here now: {present.map((p) => handleText(p.did, p.handle)).join(', ')}">
			{#each present.slice(0, 2) as person (person.did)}
				<Avatar did={person.did} handle={person.handle} size="sm" here prefix="Here now: " />
			{/each}
		</span>
	{/if}
	{#if !active}
		{#if working}<Spinner label="An agent is working here" />{/if}
		{#if agent > 0}
			<span class="agent" title="An agent added {agent} tile{agent === 1 ? '' : 's'}"><Icon name="agent" />{agent}</span>
		{/if}
		{#if changed}<Dot label={changed} />{/if}
	{/if}
	<button type="button" class="icon-button close" aria-label="Put away {name}" title="Put away (it stays in the + menu)" onclick={onclose}>
		<Icon name="close" />
	</button>
</div>

<style>
	.tab {
		display: inline-flex;
		flex: none;
		align-items: center;
		gap: var(--space-1-5);
		position: relative;
		height: var(--control);
		padding: 0 var(--space-2-5);
		border: 1px solid transparent;
		border-radius: var(--radius-md);
		color: var(--mute);
		font-size: var(--text-sm);
		white-space: nowrap;
		cursor: default;
		user-select: none;
	}

	.tab:hover {
		background: var(--hover);
		color: var(--fg);
	}

	.tab.active {
		padding-right: var(--space-1);
		border-color: var(--line);
		background: var(--bg);
		color: var(--fg);
		font-weight: var(--weight-medium);
	}

	.tab > :global(.i) {
		color: var(--faint);
	}

	.tab.active > :global(.i) {
		color: var(--accent);
	}

	/* A name is cut short only past its own most, whatever badges the tab carries. */
	.name {
		min-width: 0;
		max-width: var(--tab-name);
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.rename {
		width: 10rem;
		height: var(--control-sm);
		padding: 0 var(--space-1-5);
		border: 1px solid var(--primary);
		border-radius: var(--radius-sm);
		background: var(--bg);
		color: var(--fg);
		font-size: var(--text-sm);
		outline: 0;
	}

	.here {
		display: inline-flex;
		flex: none;
		gap: var(--space-1);
		padding: 0 var(--space-0-5);
	}

	.agent {
		display: inline-flex;
		flex: none;
		align-items: center;
		gap: var(--space-0-5);
		padding: 0 var(--space-1-5);
		border-radius: var(--radius-full);
		background: var(--agent-soft);
		color: var(--agent-fg);
		font-size: var(--text-2xs);
		font-weight: var(--weight-semibold);
		line-height: 18px;
	}

	.agent :global(.i) {
		width: var(--icon-sm);
		height: var(--icon-sm);
	}

	.close {
		width: var(--avatar);
		height: var(--avatar);
		visibility: hidden;
	}

	.close :global(.i) {
		width: var(--icon-sm);
		height: var(--icon-sm);
	}

	/* A tab out of sight keeps no room for its ×: it shows over the tab's
	   end on a hover, so every tab ends as it starts and the gaps between
	   them are even. */
	.tab:not(.active) .close {
		position: absolute;
		top: 50%;
		right: var(--space-1);
		background: var(--hover);
		transform: translateY(-50%);
	}

	.tab:hover .close,
	.tab.active .close,
	.close:focus-visible {
		visibility: visible;
	}

	@media (max-width: 640px) {
		.close {
			display: none;
		}

		.tab.active .close {
			display: inline-grid;
		}
	}
</style>