import { Component, useState } from "react";
import { parseOptionsSource } from "./openui-options-schema";
import type { OptionModel, OptionsSectionModel, ViewModel } from "./openui-options-schema";
import type { ReactNode } from "react";
import "./openui-options.css";
function Media({ media }: { media: OptionModel["media"] }) {
let [failed, setFailed] = useState(false);
return (
{media.kind === "image" && !failed
? (
setFailed(true)}
/>
)
: (
{media.kind === "image" ? "Image unavailable" : media.text}
)}
{media.caption}
);
}
function Gallery({ view, options }: { view: ViewModel; options: OptionModel[] }) {
return (
{view.title}
{options.map(option => (
{option.title}
{option.strength}
))}
);
}
function Comparison({ view, options }: { view: ViewModel; options: OptionModel[] }) {
return (
{view.title}
| Option |
Strength |
Tradeoff |
{options.map(option => (
| {option.title} |
{option.strength} |
{option.tradeoff} |
))}
);
}
function Details({ view, options }: { view: ViewModel; options: OptionModel[] }) {
return (
{view.title}
{options.map(option => (
{option.title}
{option.detail}
))}
);
}
function Section({ section }: { section: OptionsSectionModel }) {
let [filter, setFilter] = useState("");
let categories = [...new Set(section.views[0]?.options.map(option => option.category) ?? [])];
return (
{section.title}
{section.introduction}
{section.views.map(view => {
let options = view.options.filter(option => !filter || option.category === filter);
if (view.kind === "gallery") {
return ;
}
if (view.kind === "comparison") {
return ;
}
return ;
})}
);
}
class RenderBoundary extends Component<{ children: ReactNode }, { failed: boolean }> {
override state = { failed: false };
static getDerivedStateFromError() {
return { failed: true };
}
override render() {
return this.state.failed
? (
The options could not be drawn. Source is preserved.
)
: this.props.children;
}
}
export function OpenUIOptionsPreview({ source }: { source: string }) {
let result = parseOptionsSource(source);
if ("error" in result) {
return (
{result.error} Source is preserved.
);
}
return (
);
}