mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 07:09:11 +02:00
66 lines
1.7 KiB
Text
66 lines
1.7 KiB
Text
export const PrefabDemoFrame = ({ demo, height, title }) => {
|
|
const [blobUrl, setBlobUrl] = React.useState(null);
|
|
|
|
React.useEffect(() => {
|
|
let active = true;
|
|
let objectUrl = null;
|
|
let payloadsPromise = window.__FASTMCP_PREFAB_DEMOS_PROMISE__;
|
|
|
|
if (window.__FASTMCP_PREFAB_DEMOS__) {
|
|
payloadsPromise = Promise.resolve(window.__FASTMCP_PREFAB_DEMOS__);
|
|
} else if (!payloadsPromise) {
|
|
payloadsPromise = new Promise((resolve, reject) => {
|
|
const script = document.createElement("script");
|
|
script.src = "/prefab-demo-payloads.js";
|
|
script.onload = () => resolve(window.__FASTMCP_PREFAB_DEMOS__);
|
|
script.onerror = reject;
|
|
document.head.appendChild(script);
|
|
});
|
|
window.__FASTMCP_PREFAB_DEMOS_PROMISE__ = payloadsPromise;
|
|
}
|
|
|
|
payloadsPromise
|
|
.then((payloads) => {
|
|
const html = payloads[demo];
|
|
if (!html) {
|
|
throw new Error(`Unknown Prefab demo: ${demo}`);
|
|
}
|
|
objectUrl = URL.createObjectURL(
|
|
new Blob([html], { type: "text/html" }),
|
|
);
|
|
if (active) {
|
|
setBlobUrl(objectUrl);
|
|
} else {
|
|
URL.revokeObjectURL(objectUrl);
|
|
}
|
|
});
|
|
|
|
return () => {
|
|
active = false;
|
|
if (objectUrl) {
|
|
URL.revokeObjectURL(objectUrl);
|
|
}
|
|
};
|
|
}, [demo]);
|
|
|
|
if (!blobUrl) {
|
|
return <div style={{ width: "100%", height, borderRadius: "8px" }} />;
|
|
}
|
|
|
|
return (
|
|
<iframe
|
|
src={blobUrl}
|
|
title={title}
|
|
style={{
|
|
width: "100%",
|
|
height,
|
|
border: "none",
|
|
overflow: "hidden",
|
|
borderRadius: "8px",
|
|
}}
|
|
frameBorder="0"
|
|
scrolling="no"
|
|
sandbox="allow-scripts allow-same-origin"
|
|
/>
|
|
);
|
|
};
|