~ 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
233 lines · 6.1 KB Raw History
<script lang="ts">
	import { onMount } from 'svelte';
	import Handle from '../atoms/Handle.svelte';
	import Icon from '../atoms/Icon.svelte';
	import { styleOf, themeId, type Theme } from '../theme';

	/**
	 * Themes people made, everyone's or one account's, a page at a time,
	 * each drawn in itself: its name and maker on its own colours. Using one
	 * draws the workspace in it, as it is now; one's own can be deleted.
	 */
	type Page = { themes: Theme[]; cursor?: string };

	let {
		me,
		chosen,
		list,
		onuse,
		ondelete,
		onclose
	}: {
		me: string;
		chosen: Theme | null;
		/** A page of themes: an account's, by handle or DID, or everyone's. */
		list: (actor: string | null, cursor: string | undefined) => Promise<Page>;
		onuse: (theme: Theme) => void;
		ondelete: (theme: Theme) => Promise<void>;
		onclose: () => void;
	} = $props();

	let dialog: HTMLDialogElement | undefined = $state();
	let actor = $state('');
	/** The account listed; none, everyone. */
	let shown = $state<string | null>(null);
	let themes = $state.raw<Theme[]>([]);
	let cursor = $state<string | undefined>(undefined);
	let loading = $state(false);
	let error = $state<string | null>(null);

	async function load(more: boolean) {
		loading = true;
		error = null;
		try {
			const page = await list(shown, more ? cursor : undefined);
			themes = more ? [...themes, ...page.themes] : page.themes;
			cursor = page.cursor;
		} catch (failed) {
			error = failed instanceof Error ? failed.message : String(failed);
		} finally {
			loading = false;
		}
	}

	function search(event: SubmitEvent) {
		event.preventDefault();
		shown = actor.trim().replace(/^@/, '') || null;
		void load(false);
	}

	function everyone() {
		actor = '';
		shown = null;
		void load(false);
	}

	async function remove(theme: Theme) {
		if (!window.confirm(`Delete “${theme.name}”? Anyone using it keeps its colours.`)) return;
		await ondelete(theme);
		themes = themes.filter((found) => found.uri !== theme.uri);
	}

	onMount(() => {
		dialog?.showModal();
		void load(false);
	});
</script>

<dialog bind:this={dialog} class="dialog browser" aria-labelledby="browser-title" {onclose}>
	<h2 id="browser-title">{shown ? 'Themes by' : 'Everyone’s themes'} {#if shown}<span class="mute">{shown}</span>{/if}</h2>
	<button type="button" class="icon-button close" aria-label="Close" onclick={() => dialog?.close()}><Icon name="close" /></button>

	<form class="search" onsubmit={search}>
		<input class="input" type="text" bind:value={actor} placeholder="An account’s, as alice.bsky.social" aria-label="Handle whose themes to show" autocomplete="off" spellcheck="false" />
		<button type="submit" class="btn">Show</button>
		{#if shown}<button type="button" class="btn" onclick={everyone}>Everyone’s</button>{/if}
	</form>

	{#if error}<p class="problem" role="alert">{error}</p>{/if}

	<ul class="cards">
		{#each themes as theme (themeId(theme))}
			{@const used = chosen !== null && themeId(chosen) === themeId(theme)}
			<li class="card" style={styleOf(theme)} data-palette>
				<div class="sample">
					<strong class="name">{theme.name}</strong>
					{#if theme.creator}<span class="by">by <Handle did={theme.creator.did} handle={theme.creator.handle} /></span>{/if}
					<span class="dots" aria-hidden="true">
						<span class="dot accent"></span><span class="dot primary"></span><span class="dot success"></span><span class="dot danger"></span><span class="dot attention"></span>
					</span>
				</div>
				<div class="foot">
					<button type="button" class="btn sm primary" disabled={used} onclick={() => onuse(theme)}>{used ? 'In use' : 'Use'}</button>
					{#if theme.creator?.did === me}
						<button type="button" class="icon-button" title="Delete" aria-label="Delete {theme.name}" onclick={() => void remove(theme)}><Icon name="close" /></button>
					{/if}
				</div>
			</li>
		{:else}
			{#if !loading && !error}
				<li class="empty-state">{shown ? 'No themes by this account' : 'No one has published a theme yet'}</li>
			{/if}
		{/each}
	</ul>

	<div class="actions">
		{#if loading}<span class="mute">Loading themes…</span>{/if}
		{#if cursor && !loading}<button type="button" class="btn" onclick={() => void load(true)}>More</button>{/if}
		<span class="grow"></span>
		<button type="button" class="btn" onclick={() => dialog?.close()}>Done</button>
	</div>
</dialog>

<style>
	.browser {
		width: min(var(--dialog-wide), calc(100vw - 2 * var(--space-4)));
		max-height: calc(100vh - 2 * var(--space-8));
		margin-top: var(--space-8);
		overflow-y: auto;
	}

	.search {
		display: flex;
		gap: var(--space-2);
		margin-top: var(--space-3);
	}

	.search .input {
		flex: 1;
	}

	.cards {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(var(--card), 1fr));
		gap: var(--space-3);
		margin: var(--space-4) 0 0;
		padding: 0;
		list-style: none;
	}

	.card {
		display: flex;
		flex-direction: column;
		overflow: hidden;
		border: var(--border);
		border-radius: var(--radius-md);
		background: var(--canvas);
		color: var(--fg);
	}

	.sample {
		display: flex;
		flex-direction: column;
		gap: var(--space-1);
		margin: var(--space-3) var(--space-3) 0;
		padding: var(--space-2-5) var(--space-3);
		border: var(--border);
		border-bottom: 0;
		border-radius: var(--radius-sm) var(--radius-sm) 0 0;
		background: var(--bg);
	}

	.name {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

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

	.dots {
		display: flex;
		gap: var(--space-1);
		margin-top: var(--space-1);
	}

	.dot {
		width: var(--space-2-5);
		height: var(--space-2-5);
		border-radius: var(--radius-full);
	}

	.dot.accent {
		background: var(--accent);
	}

	.dot.primary {
		background: var(--primary);
	}

	.dot.success {
		background: var(--success);
	}

	.dot.danger {
		background: var(--danger);
	}

	.dot.attention {
		background: var(--attention);
	}

	.foot {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: var(--space-2) var(--space-3);
		background: var(--panel);
		border-top: var(--border);
	}

	.grow {
		flex: 1;
	}

	@media (max-width: 640px) {
		.browser {
			margin-top: auto;
		}
	}
</style>