~ 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
434 lines · 11.1 KB Raw History
<script lang="ts">
	import { Glyph, type IconName } from '@hud/renderer';
	import type { Tab } from './app';
	import type { AccountItem, CallItem, Frame, HostItem } from './frame';

	// The app's navigation beside its screens (frame.ts), as Grain web's
	// sidebar (web/src/lib/components/organisms/Sidebar.svelte) draws it: the
	// wordmark; each tab with its icon (the account's picture on the tab the
	// frame names) and label, a link to its URL, with its badge, and Search
	// after the first where the app has a search (opening the search flyout
	// beside it); the account's screens (Settings); the app's action, a
	// filled button; and at its foot signing out, or signed out, a line saying
	// what the app is over Sign in, a quiet outlined row in the app's accent,
	// so the items sit right under the wordmark either way; then the links.
	// As wide as its band makes it (frame.ts): a rail of icons, each label a
	// tooltip as it is hovered or focused, the wordmark its first letter and
	// the links left out; else a sidebar with words. Which is the box's
	// width's to say (a container query), never code's.
	let {
		wordmark,
		tabs,
		hrefs,
		current,
		counts,
		picture,
		pictureTab,
		frame,
		on,
		hrefOf,
		signedIn,
		signIn,
		searchable,
		searchOpen,
		onchoose,
		onsearch,
		onitem,
		onlink,
		onaction,
		onactionahead,
		onsignout
	}: {
		wordmark: string;
		tabs: Tab[];
		/** Each tab's URL at its root. */
		hrefs: string[];
		current: number;
		counts: Record<number, number>;
		/** The account's picture, once read. */
		picture: string | null;
		/** The tab it is drawn on. */
		pictureTab: number;
		frame: Frame | undefined;
		/** The account's screen on show, by its place in the frame's items; -1 where none is. */
		on: number;
		/** The URL of a call's screen. */
		hrefOf: (call: CallItem['call']) => string | null;
		signedIn: boolean;
		/** Where signing in starts. */
		signIn: string;
		/** Whether the app has a search. */
		searchable: boolean;
		searchOpen: boolean;
		/** A tab's link clicked: the app chooses the tab, unless the browser opens it elsewhere. */
		onchoose: (event: MouseEvent, i: number) => void;
		onsearch: () => void;
		/** An account item's link clicked: the app opens its screen, unless the browser opens it elsewhere. */
		onitem: (event: MouseEvent, item: CallItem) => void;
		/** A link of the app's own clicked (a path, as one of its pages'): the app opens its screen, unless the browser opens it elsewhere. */
		onlink: (event: MouseEvent, href: string) => void;
		/** The action pressed: its form opened over the screen on show. */
		onaction: () => void;
		/** A pointer over the action, or pressing it: its call read ahead. */
		onactionahead: () => void;
		onsignout: () => void;
	} = $props();

	const items = $derived(frame?.account?.items ?? []);
	const screens = $derived(items.flatMap((item, i): { item: CallItem; i: number }[] => ('call' in item && signedIn ? [{ item, i }] : [])));
	const host = (kind: HostItem['host']) => items.find((item): item is HostItem => 'host' in item && item.host === kind);
	/** Signing out, signed in; in, signed out: as the frame words them, else as an app without a frame always did. */
	const signOut = $derived(signedIn ? (frame?.account?.items ? host('signOut') : { host: 'signOut' as const }) : undefined);
	const signInItem = $derived(signedIn ? undefined : frame?.account?.items ? host('signIn') : { host: 'signIn' as const });
	const action = $derived(signedIn ? frame?.action : undefined);
	const links = $derived(frame?.links ?? []);
</script>

{#snippet icon(name: IconName)}
	<svg viewBox="0 0 24 24" aria-hidden="true"><Glyph {name} /></svg>
{/snippet}

{#snippet tab(tab: Tab, i: number)}
	{@const n = i === current ? 0 : (counts[i] ?? 0)}
	<a
		href={hrefs[i]}
		class="item"
		class:on={i === current && on < 0 && !searchOpen}
		aria-current={i === current && on < 0 ? 'page' : undefined}
		aria-label={n ? `${tab.label}, ${n} new` : undefined}
		onclick={(event) => onchoose(event, i)}
	>
		<span class="icon">
			{#if picture && i === pictureTab}
				<img class="picture" src={picture} alt="" width="24" height="24" />
			{:else}
				{@render icon(tab.icon)}
			{/if}
			{#if n}<span class="badge" aria-hidden="true">{n > 99 ? '99+' : n}</span>{/if}
		</span>
		<span class="label">{tab.label}</span>
	</a>
{/snippet}

<nav class="nav" aria-label="Main">
	<div class="top">
		<a class="wordmark" href={hrefs[0]} aria-label={wordmark} translate="no" onclick={(event) => onchoose(event, 0)}>
			<span class="whole">{wordmark}</span><span class="initial" aria-hidden="true">{wordmark.slice(0, 1)}</span>
		</a>
	</div>

	<div class="items">
		{#each tabs as one, i (one.label)}
			{@render tab(one, i)}
			{#if i === 0 && searchable}
				<button type="button" class="item" class:on={searchOpen} aria-expanded={searchOpen} onclick={onsearch}>
					<span class="icon">{@render icon('search')}</span>
					<span class="label">Search</span>
				</button>
			{/if}
		{/each}
		{#each screens as { item, i } (i)}
			<a href={hrefOf(item.call) ?? hrefs[0]} class="item" class:on={on === i && !searchOpen} aria-current={on === i ? 'page' : undefined} onclick={(event) => onitem(event, item)}>
				<span class="icon">{@render icon(item.icon)}</span>
				<span class="label">{item.label}</span>
			</a>
		{/each}
	</div>

	{#if action}
		<button type="button" class="action" onclick={onaction} onpointerenter={onactionahead} onpointerdown={onactionahead}>
			<span class="icon">{@render icon(action.icon)}</span>
			<span class="label">{action.label}</span>
		</button>
	{/if}

	<div class="bottom">
		{#if signOut}
			<button type="button" class="item out" onclick={onsignout}>
				<span class="icon">{@render icon(signOut.icon ?? 'logOut')}</span>
				<span class="label">{signOut.label ?? 'Log out'}</span>
			</button>
		{:else if signInItem}
			{#if signInItem.about}<p class="pitch">{signInItem.about}</p>{/if}
			<a class="item signin" href={signIn} data-sveltekit-reload>
				<span class="icon">{@render icon(signInItem.icon ?? 'logIn')}</span>
				<span class="label">{signInItem.label ?? 'Sign in'}</span>
			</a>
		{/if}
		{#if links.length > 0}
			<div class="links">
				{#each links as link (link.href)}
					{#if link.href.startsWith('/')}
						<a href={link.href} onclick={(event) => onlink(event, link.href)}>{link.label}</a>
					{:else}
						<a href={link.href} rel="noopener noreferrer" target="_blank">{link.label}</a>
					{/if}
				{/each}
			</div>
		{/if}
	</div>
</nav>

<style>
	.nav {
		display: flex;
		flex-direction: column;
		height: 100%;
		padding: 16px 10px;
	}

	.top {
		padding: 6px 14px 22px;
	}

	.wordmark {
		color: var(--color-text-primary);
		font-family: var(--app-font-display, inherit);
		font-size: 24px;
		font-weight: 800;
		line-height: 1.5;
		letter-spacing: -0.02em;
		text-decoration: none;
	}

	.initial {
		display: none;
	}

	.items,
	.bottom {
		display: flex;
		flex-direction: column;
		gap: 2px;
	}

	.bottom {
		margin-top: auto;
	}

	.item {
		position: relative;
		display: flex;
		align-items: center;
		gap: 16px;
		width: 100%;
		height: 52px;
		padding: 0 14px;
		border: 0;
		border-radius: 14px;
		background: none;
		color: var(--color-text-secondary);
		font: inherit;
		font-size: 15px;
		font-weight: 600;
		text-align: left;
		text-decoration: none;
		cursor: pointer;
		transition:
			background 0.12s,
			color 0.12s;
	}

	.item:hover {
		background: var(--color-background-secondary);
		color: var(--color-text-primary);
	}

	.item.on {
		background: var(--app-accent-soft, var(--color-background-tertiary));
		color: var(--app-accent, var(--color-text-primary));
	}

	.item.out {
		color: var(--color-text-tertiary);
	}

	.item.out:hover {
		color: var(--color-text-danger);
	}

	/* The app's one primary action, below the items as a filled button
	   rather than one more of them, as Grain web's Create is. */
	.action {
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 10px;
		height: 52px;
		margin: 16px 6px 0;
		border: 0;
		border-radius: var(--border-radius-full);
		background: var(--app-primary, var(--color-background-inverse));
		color: var(--app-on-primary, var(--color-text-inverse));
		font: inherit;
		font-size: 15px;
		font-weight: 600;
		cursor: pointer;
	}

	.action:hover {
		filter: brightness(0.92);
	}

	/* Signed out: what the app is, a line over Sign in, quiet as the
	   navigation's other words are. */
	.pitch {
		margin: 0 0 6px;
		padding: 0 14px;
		color: var(--color-text-tertiary);
		font-size: 13px;
		font-weight: 500;
		line-height: 1.35;
	}

	/* Sign in: a row as the tabs are, outlined and in the app's accent, so
	   it reads as the one thing to do here without shouting. */
	.item.signin {
		box-shadow: inset 0 0 0 1px var(--color-border-primary);
		color: var(--app-accent, var(--color-text-primary));
	}

	.item.signin:hover {
		background: var(--app-accent-soft, var(--color-background-secondary));
		color: var(--app-accent, var(--color-text-primary));
	}

	.links {
		display: flex;
		flex-wrap: wrap;
		gap: 6px 12px;
		padding: 14px 16px 8px;
		font-size: 11px;
		line-height: 1.6;
	}

	.links a {
		color: var(--color-text-tertiary);
		text-decoration: none;
	}

	.links a:hover {
		color: var(--color-text-secondary);
	}

	.label {
		white-space: nowrap;
	}

	.icon {
		position: relative;
		display: flex;
		flex: none;
	}

	svg {
		width: 24px;
		height: 24px;
		fill: none;
		stroke: currentColor;
		stroke-width: 2;
		stroke-linecap: round;
		stroke-linejoin: round;
	}

	.picture {
		width: 24px;
		height: 24px;
		border-radius: var(--border-radius-full);
		object-fit: cover;
		background: var(--color-background-tertiary);
	}

	.badge {
		position: absolute;
		top: -6px;
		right: -8px;
		display: flex;
		align-items: center;
		justify-content: center;
		min-width: 16px;
		height: 16px;
		padding: 0 4px;
		border-radius: 8px;
		background: var(--app-accent, var(--color-text-info));
		color: var(--color-background-primary);
		font-size: 10px;
		font-weight: 700;
		line-height: 1;
	}

	/* A rail: the same items, icons alone in a column of 52px squares, each
	   label a tooltip beside it as it is hovered (where a pointer hovers: a
	   touch screen's first tap would only show it, as iOS takes a hover that
	   shows something) or focused; the wordmark its first letter; the links
	   left out. */
	@container hud-nav (max-width: 160px) {
		.nav {
			align-items: center;
		}

		.top {
			padding: 6px 0 22px;
		}

		.whole {
			display: none;
		}

		.initial {
			display: inline;
			font-size: 26px;
		}

		.items,
		.bottom {
			align-items: center;
		}

		.item,
		.action {
			justify-content: center;
			width: 52px;
			padding: 0;
		}

		.action {
			margin: 16px 0 0;
		}

		.label {
			position: absolute;
			top: 50%;
			left: calc(100% + 8px);
			z-index: 1;
			padding: 4px 8px;
			border-radius: 6px;
			background: var(--color-background-inverse);
			color: var(--color-text-inverse);
			font-size: 13px;
			font-weight: 500;
			translate: 0 -50%;
			opacity: 0;
			pointer-events: none;
			transition: opacity 0.12s;
		}

		.item:focus-visible .label,
		.action:focus-visible .label {
			opacity: 1;
		}

		@media (hover: hover) {
			.item:hover .label,
			.action:hover .label {
				opacity: 1;
			}
		}

		.pitch,
		.links {
			display: none;
		}
	}
</style>