From d4c65ad1a3e6d8c63322c615479198cff26c5e2f Mon Sep 17 00:00:00 2001 From: James Murdza Date: Thu, 1 Aug 2024 10:56:39 -0700 Subject: [PATCH] Reload the live preview when the app is restarted. --- frontend/components/editor/index.tsx | 14 +++++++-- frontend/components/editor/navbar/run.tsx | 2 ++ frontend/components/editor/preview/index.tsx | 31 +++++++++++--------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/frontend/components/editor/index.tsx b/frontend/components/editor/index.tsx index 2e719b9..595c914 100644 --- a/frontend/components/editor/index.tsx +++ b/frontend/components/editor/index.tsx @@ -106,6 +106,13 @@ export default function CodeEditor({ // Preview state const [previewURL, setPreviewURL] = useState(""); + const loadPreviewURL = (url: string) => { + // This will cause a reload if previewURL changed. + setPreviewURL(url); + // If the URL didn't change, still reload the preview. + previewWindowRef.current?.refreshIframe(); + } + const isOwner = sandboxData.userId === userData.id const clerk = useClerk() @@ -131,6 +138,7 @@ export default function CodeEditor({ const generateWidgetRef = useRef(null) const previewPanelRef = useRef(null) const editorPanelRef = useRef(null) + const previewWindowRef = useRef<{ refreshIframe: () => void }>(null) // Pre-mount editor keybindings const handleEditorWillMount: BeforeMount = (monaco) => { @@ -467,7 +475,7 @@ export default function CodeEditor({ socketRef.current?.on("error", onError) socketRef.current?.on("terminalResponse", onTerminalResponse) socketRef.current?.on("disableAccess", onDisableAccess) - socketRef.current?.on("previewURL", setPreviewURL) + socketRef.current?.on("previewURL", loadPreviewURL) return () => { socketRef.current?.off("connect", onConnect) @@ -476,7 +484,7 @@ export default function CodeEditor({ socketRef.current?.off("error", onError) socketRef.current?.off("terminalResponse", onTerminalResponse) socketRef.current?.off("disableAccess", onDisableAccess) - socketRef.current?.off("previewURL", setPreviewURL) + socketRef.current?.off("previewURL", loadPreviewURL) } // }, []); }, [terminals]) @@ -824,7 +832,7 @@ export default function CodeEditor({ open={() => { usePreview().previewPanelRef.current?.expand() setIsPreviewCollapsed(false) - } } collapsed={isPreviewCollapsed} src={previewURL}/> + } } collapsed={isPreviewCollapsed} src={previewURL} ref={previewWindowRef} /> void src: string -}) { - const ref = useRef(null) +}, +ref: React.Ref<{ + refreshIframe: () => void +}>) { + const frameRef = useRef(null) const [iframeKey, setIframeKey] = useState(0) + const refreshIframe = () => { + setIframeKey(prev => prev + 1) + } + // Refresh the preview when the URL changes. + useEffect(refreshIframe, [src]) + // Expose refreshIframe method to the parent. + useImperativeHandle(ref, () => ({ refreshIframe })) return ( <> @@ -49,14 +59,7 @@ export default function PreviewWindow({ > - { - // if (ref.current) { - // ref.current.contentWindow?.location.reload(); - // } - setIframeKey((prev) => prev + 1) - }} - > + @@ -68,7 +71,7 @@ export default function PreviewWindow({