import { Fragment, ReactNode, useEffect, useMemo, useState } from "react";
import { appFactory, AutoUpdateApp } from "./starter";
import { css } from "@emotion/react";
import { DraggableWindow } from "./DraggableWindow";
import type { FC } from "react";
import { hydrateRoot } from "react-dom/client";
import { hashCode, mST, onSessionUpdate } from "./session";
import {
createHtmlPortalNode,
InPortal,
OutPortal,
} from "react-reverse-portal";
import { Editor } from "./Editor";
const RainbowContainer: FC<{ children: ReactNode }> = ({ children }) => (
{children}
);
const AppToRender: FC<{ codeSpace: string; children: FC }> = (
{ codeSpace, children },
) => {
const [hash, setHash] = useState(() => hashCode());
const [isStandalone, setIsStandalone] = useState(true);
useEffect(() => {
onSessionUpdate(async () => {
const newHash = hashCode();
if (hash !== newHash) {
try {
await appFactory(mST().transpiled);
setHash(newHash);
} catch (e) {
console.error({ e });
}
}
}, "myApp");
}, [hash, setHash]);
useEffect(() => {
setTimeout(() => {
const isStandalone = location.pathname.endsWith("public") ||
location.pathname.endsWith("hydrated");
setIsStandalone(isStandalone);
}, 800);
}, []);
const portalNode = useMemo(() =>
createHtmlPortalNode({
attributes: { id: `root-${codeSpace}`, style: "height: 100%" },
}), []);
return (
{isStandalone
? (
)
: (
{
// const model = globalThis.model;
// model.setValue(mST().code);
// }}
hashCode={0}
room={codeSpace}
>
)}
);
};
export const renderPreviewWindow = (
codeSpace: string,
App: FC,
) => {
return hydrateRoot(
document.getElementById("root")!,
{App},
);
};