Skip to content

Commit 14a360d

Browse files
fixed show me button
1 parent a86870b commit 14a360d

File tree

3 files changed

+462
-23
lines changed

3 files changed

+462
-23
lines changed

Diff for: frontend/app/page.tsx

+49-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22
import Link from "next/link";
3-
import { useEffect, useRef, useState } from "react";
3+
import { SyntheticEvent, useEffect, useRef, useState } from "react";
44
import { WebContainer } from "@webcontainer/api";
55
import Editor from "@monaco-editor/react";
66
import { Terminal } from "xterm";
@@ -21,11 +21,11 @@ export default function Home() {
2121
const timeoutId = useRef<NodeJS.Timeout | null>(null);
2222
const webcontainerInstanceRef = useRef<WebContainer>();
2323
const editorRef = useRef<any>(null);
24-
const terminalRef = useRef<HTMLDivElement>(null);
25-
const [isTerminal, setIsTerminal] = useState(false);
26-
const toggleTerminal = () => {
27-
setIsTerminal(!isTerminal);
28-
};
24+
// const terminalRef = useRef<HTMLDivElement>(null);
25+
// const [isTerminal, setIsTerminal] = useState(false);
26+
// const toggleTerminal = () => {
27+
// setIsTerminal(!isTerminal);
28+
// };
2929
function showValue() {
3030
if (editorRef.current) {
3131
return editorRef.current!.getValue();
@@ -35,7 +35,9 @@ export default function Home() {
3535
}
3636
function showMe() {
3737
if (editorRef.current) {
38-
editorRef.current!.setValue("Hello World");
38+
editorRef.current!.setValue(
39+
`import React from 'react';\n\nfunction App() {\n return (\n <div>\n\t\t\tHello React\n\t\t</div>\n );\n}\n\nexport default App;`
40+
);
3941
}
4042
}
4143
const monacoEditor = (editor: any, monaco: any) => {
@@ -93,7 +95,7 @@ export default function Home() {
9395
// Send the JSX content as a separate payload
9496
await webcontainerInstanceRef.current!.fs.writeFile(filePath, value);
9597
// Optionally, you can log a success message
96-
console.log(`Successfully updated ${filePath}`);
98+
// console.log(`Successfully updated ${filePath}`);
9799
} catch (error: any) {
98100
// Handle errors
99101
throw new Error(`Unable to update ${filePath}: ${error.message}`);
@@ -126,10 +128,33 @@ export default function Home() {
126128
const webcontainerInstance = await WebContainer.boot();
127129
webcontainerInstanceRef.current = webcontainerInstance;
128130
webcontainerInstance.on("server-ready", async (port, url) => {
129-
setLoadingWebContainer(false);
130131
if (previewRef.current) {
131132
previewRef.current.src = url;
132133
}
134+
setLoadingWebContainer(false);
135+
136+
// const iframe = previewRef.current;
137+
138+
// const handleMessage = (event: any) => {
139+
// console.log("This is called");
140+
// console.log(event);
141+
// console.log(event.source);
142+
// console.log(event.data);
143+
// // Check if the message is from the iframe
144+
// if (event.source[0] === iframe!.contentWindow) {
145+
// // Handle the error message from the iframe
146+
// const errorMessage = event.data;
147+
// console.error("Error in iframe:", errorMessage);
148+
// }
149+
// };
150+
151+
// // Add an event listener for the 'message' event
152+
// window.addEventListener("message", handleMessage);
153+
154+
// // Cleanup the event listener when the component is unmounted
155+
// return () => {
156+
// window.removeEventListener("message", handleMessage);
157+
// };
133158
});
134159
const startDevServer = async () => {
135160
const initProcess = await webcontainerInstance.spawn("npx", [
@@ -142,7 +167,7 @@ export default function Home() {
142167
initProcess.output.pipeTo(
143168
new WritableStream({
144169
write: (chunk) => {
145-
console.log(chunk);
170+
// console.log(chunk);
146171
},
147172
})
148173
);
@@ -247,6 +272,10 @@ export default function Home() {
247272
document.removeEventListener("mousedown", handleClickOutside);
248273
};
249274
}, [isDropDownActive]);
275+
const handleError = (event: any) => {
276+
console.error("Error loading iframe:", event.message);
277+
// Handle the error here, like displaying an error message to the user
278+
};
250279
return (
251280
<main className="bg-[#1B1D24] h-[calc(100svh-64px)] text-white ">
252281
<div className="container mx-auto flex flex-col md:flex-row h-full">
@@ -308,7 +337,7 @@ export default function Home() {
308337
</p>
309338
<button
310339
onClick={showMe as any}
311-
className="my-2 border-2 border-sky-600 rounded-md py-2 bg-sky-600 font-bold"
340+
className="my-2 border-2 border-sky-600 rounded-md py-2 bg-sky-600 font-bold hover:bg-sky-700 transition-all ease-in-out duration-75 w-full text-white active:bg-white active:text-sky-600 active:border-sky-600 active:shadow-md hover:shadow-md hover:text-white hover:border-sky-700"
312341
>
313342
Show Me!
314343
</button>
@@ -350,7 +379,7 @@ export default function Home() {
350379
</div>
351380
<div
352381
className={
353-
loadingWebContainer && !isTerminal
382+
loadingWebContainer /*&& !isTerminal*/
354383
? "flex-1 flex justify-center items-center flex-col gap-4"
355384
: "hidden"
356385
}
@@ -378,21 +407,25 @@ export default function Home() {
378407
</div>
379408
<div
380409
className={
381-
!loadingWebContainer && !isTerminal
410+
!loadingWebContainer /*&& !isTerminal*/
382411
? "bg-white text-black rounded-md m-3 flex-1"
383412
: "hidden"
384413
}
385414
>
386-
<iframe ref={previewRef} className="h-full w-full"></iframe>
415+
<iframe
416+
ref={previewRef}
417+
onError={handleError}
418+
className="h-full w-full"
419+
></iframe>
387420
</div>
388-
<div
421+
{/* <div
389422
className={
390423
isTerminal
391424
? "bg-black flex-1 text-white p-4"
392425
: "bg-black flex-1 hidden"
393426
}
394427
ref={terminalRef}
395-
></div>
428+
></div> */}
396429
</div>
397430
</div>
398431
</section>

0 commit comments

Comments
 (0)