Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -647,4 +647,17 @@ describe("OpenGenUIActivityRenderer", () => {
});
expect(parsed.success).toBe(true);
});

it("uses the latest streamed preview when sandbox readiness is delayed", async () => {
const base = { cssComplete: true, htmlComplete: false, generating: true };
const { rerender } = renderRenderer({ ...base, css: "p{color:red}", html: ["<p>old</p>"] });
await flushImport();
rerender(rendererElement({ ...base, css: "p{color:blue}", html: ["<p>latest</p>"] }));
await act(async () => { await new Promise((r) => setTimeout(r, THROTTLE_MS + 50)); });
await resolveSandboxReady();
expect(runCallsContaining("Idiomorph.morph").at(-1)?.[0]).toContain("latest");
expect(runCallsContaining("document.head.innerHTML").at(-1)?.[0]).toContain("color:blue");
expect(mockCreate).toHaveBeenCalledTimes(1);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ const OpenGenUIActivityRendererInner = React.memo(
const previewBody = partialHtml ? processPartialHtml(partialHtml) : undefined;
const previewStyles = partialHtml ? extractCompleteStyles(partialHtml) : "";
const hasPreview = cssReady && !!previewBody?.trim();
const latestPreviewRef = useRef({ css, previewStyles, previewBody });
useEffect(() => {
latestPreviewRef.current = { css, previewStyles, previewBody };
}, [css, previewStyles, previewBody]);
const hasVisibleSandbox = !!fullHtml || hasPreview;

const containerRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -210,15 +214,16 @@ const OpenGenUIActivityRendererInner = React.memo(
sandbox.promise.then(() => {
if (cancelled) return;
previewReadyRef.current = true;
const latest = latestPreviewRef.current;

sandbox.run(IDIOMORPH_JS);
sandbox.run(
`document.head.innerHTML = ${JSON.stringify(
buildPreviewHeadContent(css, previewStyles)
buildPreviewHeadContent(latest.css, latest.previewStyles)
)}`
);
if (previewBody) {
sandbox.run(buildPreviewBodyMorph(previewBody));
if (latest.previewBody) {
sandbox.run(buildPreviewBodyMorph(latest.previewBody));
}
});
})
Expand Down