import { useState } from "react"; import type { Json } from "../model.js"; const scopes = new Map>(); function JsonNode({ value, name, path, state, root = false, }: { value: Json; name: string; path: string; state: Map; root?: boolean; }) { const [open, setOpen] = useState(() => state.get(path) ?? root); const structured = value !== null && typeof value === "object"; if (!structured) return (
{name}:{" "} {JSON.stringify(value)}
); const entries = Object.entries(value); return (
{ state.set(path, event.currentTarget.open); setOpen(event.currentTarget.open); }} > {name}{" "} {Array.isArray(value) ? `[${entries.length} items]` : `{${entries.length} fields}`} {
{entries.map(([key, child]) => ( ))}
}
); } export function JsonTree({ value, scope }: { value: Json; scope: string }) { let state = scopes.get(scope); if (!state) { state = new Map(); scopes.set(scope, state); if (scopes.size > 128) { const first = scopes.keys().next().value; if (first !== undefined) scopes.delete(first); } } return (
); }