chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.5.0 Latest 5 packages
0
0
<script lang="ts">
import { ErrorMessage, Loading } from '@hud/renderer';
import { onMount } from 'svelte';
import Icon from './lib/atoms/Icon.svelte';
import { clicked, pressed } from './lib/menus';
import Dialog from './lib/molecules/Dialog.svelte';
import PlaygroundEditor from './lib/organisms/PlaygroundEditor.svelte';
import PlaygroundPreview, { type Shown, type Width } from './lib/organisms/PlaygroundPreview.svelte';
import TemplateMenu from './lib/organisms/TemplateMenu.svelte';
import { Playground } from './lib/playground/playground.svelte';
import PlaygroundPage from './lib/templates/PlaygroundPage.svelte';
// The view playground, at /playground: a place.hud.view document and the
// call that draws it, written on the right, drawn on the left as it is
// typed (place.hud.previewView), as anyone, or as the account signed in
// to the workspace. It starts from a bundled view, a blank one, a link,
// or the work this browser kept.
/** How long typing rests before the view is drawn again. */
const REST = 300;
const pg = new Playground();
let width: Width = $state('column');
let theme: string | null = $state(null);
let shown: Shown = $state('preview');
let copied = $state(false);
/** What waits on the person's word, as it would replace their edits. */
let pending: { run: () => void } | null = $state(null);
const template = $derived(pg.templates.find((t) => t.name === pg.template) ?? null);
const description = $derived(typeof pg.document?.description === 'string' ? pg.document.description : null);
const shareable = $derived(pg.call !== null && pg.document !== null);
const resetTo = $derived(pg.template ? pg.template : null);
onMount(() => {
void pg.load();
});
// Drawn again, and kept, once typing rests.
$effect(() => {
void pg.work;
if (pg.phase.kind !== 'ready') return;
const timer = setTimeout(() => {
pg.keep();
void pg.draw();
}, REST);
return () => clearTimeout(timer);
});
/** Run `replace`, which replaces the work, once the person says so if they edited it. */
function unlessEdited(replace: () => void) {
if (pg.edited) pending = { run: replace };
else replace();
}
/** Replace the work as `pending` would, read before the dialog that holds it goes. */
function replace() {
const run = pending?.run;
pending = null;
run?.();
}
async function copyLink() {
const url = await pg.link();
if (!url) return;
history.replaceState(null, '', url);
await navigator.clipboard.writeText(url);
copied = true;
setTimeout(() => (copied = false), 1500);
}
</script>
<svelte:window onclick={clicked} onkeydown={pressed} />
{#if pg.phase.kind === 'loading'}
<main class="center"><Loading label="Opening the playground…" /></main>
{:else if pg.phase.kind === 'error'}
<main class="center"><ErrorMessage message={pg.phase.message} onretry={() => void pg.load()} /></main>
{:else}
<PlaygroundPage>
{#snippet bar()}
<a class="home" href="/workspace" title="HUD workspace"><img src="/favicon.svg" alt="HUD" width="24" height="24" /></a>
<h1>Playground</h1>
<TemplateMenu templates={pg.templates} current={pg.template} onchoose={(name) => unlessEdited(() => pg.choose(name))} onblank={() => unlessEdited(() => pg.blank())} />
<span class="end">
<button type="button" class="btn sm primary" disabled={!shareable} title={shareable ? 'A link to this view and its call' : 'The call and the view are JSON first'} onclick={() => void copyLink()}>
<Icon name={copied ? 'check' : 'copy'} />{copied ? 'Copied' : 'Copy link'}
</button>
</span>
{/snippet}
{#snippet preview()}
<PlaygroundPreview
entry={pg.entry}
data={pg.answer?.data}
status={pg.status}
busy={pg.busy}
problems={pg.problems.length + (pg.call ? 0 : 1) + (pg.document ? 0 : 1)}
summary={pg.answer?.summary ?? ''}
{description}
opened={pg.opened}
themes={pg.themes}
bind:width
bind:theme
bind:shown
onread={() => void pg.draw(true)}
oncall={(call) => pg.open(call)}
onfollow={(opened) => (opened.template ? unlessEdited(() => pg.follow(opened)) : pg.follow(opened))}
ondismiss={() => (pg.opened = null)}
/>
{/snippet}
{#snippet editor()}
<PlaygroundEditor
tool={pg.tool}
bind:args={pg.args}
bind:view={pg.view}
problems={pg.problems}
catalog={pg.catalog}
edited={pg.edited}
onswitch={(tool) => pg.switchTool(tool)}
onreset={resetTo ? () => unlessEdited(() => pg.choose(resetTo)) : null}
resetTitle={template ? `Reset the call and the view to ${template.app}: ${template.example.title}` : ''}
/>
{/snippet}
{#snippet overlays()}
{#if pending}
<Dialog id="replace" title="Replace your work?" onclose={() => (pending = null)}>
<p class="mute">What you changed in the call and the view goes. Copy a link first to keep it.</p>
<div class="actions">
<button type="button" class="btn" onclick={() => (pending = null)}>Keep editing</button>
<button type="button" class="btn primary" onclick={replace}>Replace</button>
</div>
</Dialog>
{/if}
{/snippet}
</PlaygroundPage>
{/if}
<style>
.center {
display: grid;
place-items: center;
height: 100%;
}
.home {
display: inline-flex;
flex: none;
}
.home img {
width: var(--mark);
height: var(--mark);
}
h1 {
margin-right: var(--space-2);
font-size: var(--text-base);
}
.end {
display: flex;
align-items: center;
gap: var(--space-2);
margin-left: auto;
}
@media (max-width: 640px) {
h1 {
display: none;
}
}
</style>