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 JsonTree from './JsonTree.svelte';
import Picture from './Picture.svelte';
import { blob, isObject } from './lexicon';
// A value as JSON, each object and array a branch that opens and closes,
// those above `open` levels deep open at first. An image blob shows its
// picture too, with the `alt` beside it in its parent where there is one,
// as that is how atproto's records pair them; an audio blob, a player.
let {
value,
name,
depth = 0,
open = 1,
alt = ''
}: { value: unknown; name?: string; depth?: number; open?: number; alt?: string } = $props();
/** How many children a branch lists before a button for the rest. */
const PAGE = 50;
let all = $state(false);
const branch = $derived(isObject(value) || Array.isArray(value));
const entries = $derived(
Array.isArray(value)
? value.map((v, i): [string, unknown] => [String(i), v])
: isObject(value)
? Object.entries(value)
: []
);
const shown = $derived(all ? entries : entries.slice(0, PAGE));
const preview = $derived.by(() => {
const b = blob(value);
return b?.kind === 'image' || b?.kind === 'audio' ? b : null;
});
const childAlt = $derived(isObject(value) && typeof value.alt === 'string' ? value.alt : '');
const leaf = $derived(
value === null ? 'null' : typeof value === 'string' ? 'string' : typeof value === 'number' ? 'number' : typeof value === 'boolean' ? 'boolean' : 'other'
);
</script>
{#if branch}
<details class="branch" open={depth < open}>
<summary>{#if name !== undefined}<span class="key">{name}</span>{/if}<span class="count">{Array.isArray(value) ? `[${entries.length}]` : `{${entries.length}}`}</span></summary>
<ul>
{#each shown as [key, child] (key)}
<li><JsonTree name={key} value={child} depth={depth + 1} {open} alt={childAlt} /></li>
{/each}
</ul>
{#if shown.length < entries.length}
<button type="button" class="rest" onclick={() => (all = true)}>+{entries.length - shown.length} more</button>
{/if}
</details>
{#if preview?.kind === 'image'}
<span class="image"><Picture src={preview.source} {alt} fit="contain" /></span>
{:else if preview?.kind === 'audio'}
<audio class="audio" controls preload="none" src={preview.url}></audio>
{/if}
{:else}
<span class="leaf">{#if name !== undefined}<span class="key">{name}</span>{/if}<span class={leaf}>{value === undefined ? 'undefined' : JSON.stringify(value)}</span></span>
{/if}
<style>
.branch,
.leaf {
font-family: var(--font-mono);
font-size: var(--font-text-xs-size);
line-height: var(--font-text-xs-line-height);
}
.leaf {
display: block;
overflow-wrap: anywhere;
white-space: pre-wrap;
}
summary {
width: fit-content;
cursor: pointer;
border-radius: var(--border-radius-xs);
}
summary:hover .count {
color: var(--color-text-primary);
}
.key {
color: var(--color-text-secondary);
}
.key::after {
content: ':';
margin-right: 0.5ch;
}
.count {
color: var(--color-text-tertiary);
}
ul {
margin: 0;
padding-left: 1.25ch;
margin-left: 0.5ch;
border-left: var(--border-width-regular) solid var(--color-border-secondary);
list-style: none;
}
.string {
color: var(--color-text-primary);
}
.number,
.boolean {
color: var(--color-text-info);
}
.null,
.other {
color: var(--color-text-tertiary);
}
.audio {
display: block;
width: min(100%, 22rem);
height: 2.25rem;
margin: var(--hud-space-1) 0;
}
.image {
display: block;
width: 8rem;
height: 8rem;
margin: var(--hud-space-1) 0;
border-radius: var(--border-radius-sm);
overflow: hidden;
}
.rest {
margin-left: 1.75ch;
padding: 0;
border: 0;
background: none;
color: var(--color-text-info);
font: inherit;
cursor: pointer;
}
</style>