chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.3.1 Latest 5 packages
0
0
<script lang="ts">
import { tick } from 'svelte';
import type { SignInPlan } from './app';
import type { SignInState } from './screen';
import { findActors, partsOf, type Found } from './typeahead';
// The app's own sign-in screen (its app file's `signIn`), drawn in the
// frame at the host's sign-in path, as Grain's web asks: its heading and
// a line under it, a field for the handle, its terms, Cancel and Sign in,
// and a way to make an account. The form posts to the host's server
// (`Hud.signIn`), which asks HUD to go straight on to the account's
// server, the handle its login hint; where it cannot, it draws this
// again saying why. As the handle is typed, whoever matches drops down
// under the field (the app's `typeahead`, asked by the browser): the
// arrows choose one, Enter or a click picks it, filling in the handle
// and going on; Escape closes them. Without JavaScript it is a form as
// any other.
let { plan, name, action, asked }: { plan: SignInPlan | undefined; name: string; action: string; asked: SignInState } = $props();
const id = 'hud-sign-in';
const list = `${id}-list`;
// svelte-ignore state_referenced_locally
let handle = $state(asked.handle);
let found: Found[] = $state.raw([]);
/** Which one Enter picks: none till the arrows choose one, so Enter goes on with what is typed. */
let at = $state(-1);
let open = $state(false);
let sending = $state(false);
/** The accounts whose picture failed, and whose fallback failed too. */
let failed = $state(new Map<string, number>());
let form: HTMLFormElement | undefined = $state();
const showing = $derived(open && found.length > 0);
const typeahead = $derived(plan?.typeahead);
// Asked a moment after typing stops, as Grain's web does; one overtaken by more typing is let go.
$effect(() => {
const q = handle;
const finding = typeahead;
if (!finding) return;
const stop = new AbortController();
const wait = setTimeout(() => {
void findActors(finding, q, stop.signal).then((actors) => {
if (stop.signal.aborted) return;
found = actors;
at = -1;
});
}, 200);
return () => {
stop.abort();
clearTimeout(wait);
};
});
/** An account picked: its handle filled in, and on to sign in as it. */
async function pick(actor: Found) {
handle = actor.handle;
found = [];
open = false;
await tick();
form?.requestSubmit();
}
function keydown(event: KeyboardEvent) {
if (!showing) return;
if (event.key === 'ArrowDown') {
event.preventDefault();
at = Math.min(at + 1, found.length - 1);
} else if (event.key === 'ArrowUp') {
event.preventDefault();
at = Math.max(at - 1, 0);
} else if (event.key === 'Enter' && found[at]) {
event.preventDefault();
void pick(found[at]);
} else if (event.key === 'Escape') {
event.preventDefault();
open = false;
}
}
/** An account's picture: its own, else its fallback once that fails, else none. */
function pictureOf(actor: Found): string | null {
const tries = failed.get(actor.did) ?? 0;
return tries === 0 ? (actor.avatar ?? actor.fallback) : tries === 1 && actor.avatar ? actor.fallback : null;
}
function broke(actor: Found) {
failed = new Map(failed).set(actor.did, (failed.get(actor.did) ?? 0) + 1);
}
/** A link of a line: a path of the app's own in this tab, another site's in a new one. */
const external = (href: string) => !href.startsWith('/');
</script>
<svelte:window onpageshow={() => (sending = false)} />
{#snippet line(text: string)}
{#each partsOf(text) as part, i (i)}
{#if part.href}
<a href={part.href} target={external(part.href) ? '_blank' : undefined} rel={external(part.href) ? 'noopener noreferrer' : undefined}>{part.text}</a>
{:else}{part.text}{/if}
{/each}
{/snippet}
<section class="sign-in" aria-labelledby="{id}-title">
<h1 id="{id}-title">{plan?.title ?? `Sign in to ${name}`}</h1>
{#if plan?.text}<p class="text">{@render line(plan.text)}</p>{/if}
<form method="post" {action} bind:this={form} onsubmit={() => (sending = true)}>
<input type="hidden" name="back" value={asked.back} />
<div class="field">
<label class="hidden" for={id}>Your handle</label>
<input
{id}
name="handle"
type="text"
placeholder={plan?.placeholder ?? 'alice.bsky.social'}
autocomplete="username"
autocapitalize="none"
spellcheck="false"
required
role="combobox"
aria-expanded={showing}
aria-controls={list}
aria-autocomplete="list"
aria-activedescendant={showing && at >= 0 ? `${list}-${at}` : undefined}
aria-invalid={asked.error ? true : undefined}
aria-describedby={asked.error ? `${id}-error` : undefined}
bind:value={handle}
oninput={() => (open = true)}
onfocus={() => (open = true)}
onblur={() => (open = false)}
onkeydown={keydown}
/>
{#if showing}
<ul id={list} class="list" role="listbox" aria-label="Accounts">
{#each found as actor, i (actor.did)}
{@const picture = pictureOf(actor)}
<!-- Picked on the press, before the field loses focus and the list closes. -->
<li
id="{list}-{i}"
role="option"
aria-selected={i === at}
onmousedown={(event) => {
event.preventDefault();
void pick(actor);
}}
onmouseenter={() => (at = i)}
>
<span class="avatar">
{#if picture}<img src={picture} alt="" width="32" height="32" onerror={() => broke(actor)} />{/if}
</span>
<span class="who">
<b>@{actor.handle}</b>
{#if actor.name}<small>{actor.name}</small>{/if}
</span>
</li>
{/each}
</ul>
{/if}
</div>
{#if asked.error}<p id="{id}-error" class="error" role="alert">{asked.error}</p>{/if}
{#if plan?.legal}<p class="legal">{@render line(plan.legal)}</p>{/if}
<div class="actions">
<a class="cancel" href={asked.back}>Cancel</a>
<button class="primary" type="submit" disabled={sending}>{sending ? 'Signing in…' : 'Sign in'}</button>
</div>
{#if plan?.create}
<p class="create">
{#if plan.create.text}<span>{plan.create.text}</span>{/if}
<button class="link" type="submit" name="create" value="1" formnovalidate disabled={sending}>{plan.create.label}</button>
</p>
{/if}
</form>
</section>
<style>
.sign-in {
width: min(100%, 26rem);
padding: 28px 24px;
border: var(--border-width-regular) solid var(--color-border-secondary);
border-radius: 16px;
background: var(--color-background-primary);
color: var(--color-text-primary);
}
h1 {
margin: 0 0 12px;
font-family: var(--app-font-display, inherit);
font-size: 22px;
font-weight: 700;
line-height: 1.25;
text-align: center;
}
.text {
margin: 0 0 20px;
font-size: 13px;
text-align: center;
}
.text a,
.create .link {
color: var(--app-accent, var(--color-text-primary));
text-decoration: none;
}
.text a:hover,
.create .link:hover {
text-decoration: underline;
}
.hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
.field {
position: relative;
margin-bottom: 16px;
}
input {
box-sizing: border-box;
width: 100%;
padding: 10px 14px;
border: var(--border-width-regular) solid var(--color-border-primary);
border-radius: 8px;
outline: none;
background: var(--color-background-secondary);
color: var(--color-text-primary);
font: inherit;
font-size: 16px;
transition: border-color 0.15s;
}
input:focus {
border-color: var(--app-accent, var(--color-ring-primary));
}
input::placeholder {
color: var(--color-text-tertiary);
}
.list {
position: absolute;
z-index: 10;
top: 100%;
right: 0;
left: 0;
max-height: 240px;
margin: 4px 0 0;
padding: 4px;
overflow-y: auto;
border: var(--border-width-regular) solid var(--color-border-primary);
border-radius: 10px;
background: var(--color-background-primary);
box-shadow: 0 4px 12px rgb(0 0 0 / 0.25);
list-style: none;
}
li {
display: flex;
align-items: center;
gap: 10px;
padding: 8px 10px;
border-radius: 8px;
cursor: pointer;
}
li[aria-selected='true'] {
background: var(--color-background-tertiary);
}
.avatar {
flex: none;
width: 32px;
height: 32px;
overflow: hidden;
border-radius: 50%;
background: var(--color-background-secondary);
}
.avatar img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
.who {
display: grid;
flex: 1;
min-width: 0;
}
.who b,
.who small {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.who b {
font-size: 14px;
font-weight: 500;
}
.who small {
color: var(--color-text-secondary);
font-size: 12px;
}
.error {
margin: -4px 0 14px;
color: var(--color-text-danger);
font-size: 13px;
}
.legal {
margin: 0 0 14px;
color: var(--color-text-tertiary);
font-size: 11px;
line-height: 1.5;
}
.legal a {
color: var(--color-text-secondary);
text-decoration: none;
}
.legal a:hover {
text-decoration: underline;
}
.actions {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-bottom: 12px;
}
.cancel,
.primary {
padding: 8px 16px;
border-radius: 8px;
font: inherit;
font-size: 14px;
font-weight: 600;
text-decoration: none;
}
.cancel {
color: var(--color-text-secondary);
}
.cancel:hover {
background: var(--color-background-secondary);
}
.primary {
border: 0;
background: var(--app-primary, var(--color-background-action));
color: var(--app-on-primary, var(--color-text-on-action));
cursor: pointer;
}
.primary:disabled {
opacity: 0.6;
cursor: default;
}
.create {
margin: 0;
font-size: 13px;
text-align: center;
}
.create span {
color: var(--color-text-secondary);
}
.create .link {
padding: 0;
border: 0;
background: none;
font: inherit;
cursor: pointer;
}
</style>