import Icon from '@ant-design/icons' import { Background, Controls, MiniMap, Node, Panel, ReactFlow } from '@xyflow/react' import '@xyflow/react/dist/style.css' import { AppContext } from '@/App' import useStyles from '@/components/Playground/Output/Preview/render.style' import iframeRaw from '@/components/Playground/Output/Preview/iframe.html?raw' import devices, { DeviceName } from '@/components/Playground/Output/Preview/devices' import Simulation, { SimulationData } from '@/components/Playground/Output/Preview/Simulation' interface RenderProps { iframeKey: string compiledCode: string mobileMode?: boolean globalJsVariables?: Record globalCssVariables?: string } interface IMessage { type: 'LOADED' | 'ERROR' | 'UPDATE' | 'DONE' | 'GLOBAL_VARIABLES' msg: string data: { compiledCode?: string zoom?: number globalJsVariables?: Record globalCssVariables?: string } } const getIframeUrl = (iframeRaw: string) => { return URL.createObjectURL(new Blob([iframeRaw], { type: 'text/html' })) } const iframeUrl = getIframeUrl(iframeRaw) const Render = ({ iframeKey, compiledCode, mobileMode = false, globalJsVariables, globalCssVariables }: RenderProps) => { const { styles, theme } = useStyles() const { isDarkMode } = useContext(AppContext) const iframeRef = useRef(null) const [isLoaded, setIsLoaded] = useState(false) const [selectedDevice, setSelectedDevice] = useState('Pixel 7') const [isRotate, setIsRotate] = useState(false) const [nodes, setNodes] = useState[]>([]) const handleOnRotateDevice = () => { setIsRotate(!isRotate) } const loadGlobalVariables = () => { iframeRef.current?.contentWindow?.postMessage( { type: 'GLOBAL_VARIABLES', data: { globalJsVariables, globalCssVariables } } as IMessage, '*' ) } useEffect(() => { if (!isLoaded) { return } iframeRef.current?.contentWindow?.postMessage( { type: 'UPDATE', data: { compiledCode } } as IMessage, '*' ) loadGlobalVariables() }, [isLoaded, compiledCode]) useEffect(() => { if (!isLoaded) { return } loadGlobalVariables() }, [isLoaded, globalJsVariables, globalCssVariables]) useEffect(() => { setNodes([ { id: 'device', type: 'simulation', position: { x: 0, y: 0 }, data: { deviceWidth: devices[selectedDevice].width, deviceHeight: devices[selectedDevice].height, isRotate, iframeKey, iframeRef, iframeUrl, setIsLoaded } } ]) }, []) return mobileMode ? ( <> ({ label: item.name, value: item.name }))} value={selectedDevice} onChange={setSelectedDevice} /> } /> ) : (