~ 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
38 lines · 1.9 KB Raw History
// How a blob in a value is drawn: its picture, its audio, or its type and
// size (JsonTree's), and where a picture of it loads from.

import { httpUrl } from './format';
import type { Size, Source } from './types';

export const isObject = (value: unknown): value is Record<string, unknown> =>
	typeof value === 'object' && value !== null && !Array.isArray(value);

/** A blob as drawn: a picture, audio, or a file of a type and size. */
export type BlobShown =
	| { kind: 'image'; source: Source; mimeType: string; size: number | null }
	| { kind: 'audio'; url: string; mimeType: string; size: number | null }
	| { kind: 'file'; mimeType: string; size: number | null };

/** A blob's picture, drawn at `size`: its `url`, or its `thumb` drawn small,
 * with its `proxy` to fall back on; null when it has no `url`. */
export function blobSource(blob: Record<string, unknown>, size: Size): Source | null {
	if (typeof blob.url !== 'string') return null;
	const src = size === 'small' && typeof blob.thumb === 'string' ? blob.thumb : blob.url;
	return { src, fallback: typeof blob.proxy === 'string' && blob.proxy !== src ? blob.proxy : null };
}

/** A blob, new form or legacy: an image (drawn small, beside its type and
 * size) or audio when it has a URL, else its MIME type and size. Null when
 * the value is no blob. */
export function blob(value: unknown): BlobShown | null {
	if (!isObject(value) || typeof value.mimeType !== 'string') return null;
	if (value.$type !== 'blob' && typeof value.cid !== 'string') return null;
	const size = typeof value.size === 'number' ? value.size : null;
	const { mimeType } = value;
	if (typeof value.url === 'string' && httpUrl(value.url)) {
		const source = blobSource(value, 'small');
		if (mimeType.startsWith('image/') && source) return { kind: 'image', source, mimeType, size };
		if (mimeType.startsWith('audio/')) return { kind: 'audio', url: value.url, mimeType, size };
	}
	return { kind: 'file', mimeType, size };
}