1
1
"use client" ;
2
2
import Link from "next/link" ;
3
- import { useEffect , useRef , useState } from "react" ;
3
+ import { SyntheticEvent , useEffect , useRef , useState } from "react" ;
4
4
import { WebContainer } from "@webcontainer/api" ;
5
5
import Editor from "@monaco-editor/react" ;
6
6
import { Terminal } from "xterm" ;
@@ -21,11 +21,11 @@ export default function Home() {
21
21
const timeoutId = useRef < NodeJS . Timeout | null > ( null ) ;
22
22
const webcontainerInstanceRef = useRef < WebContainer > ( ) ;
23
23
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
+ // };
29
29
function showValue ( ) {
30
30
if ( editorRef . current ) {
31
31
return editorRef . current ! . getValue ( ) ;
@@ -35,7 +35,9 @@ export default function Home() {
35
35
}
36
36
function showMe ( ) {
37
37
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
+ ) ;
39
41
}
40
42
}
41
43
const monacoEditor = ( editor : any , monaco : any ) => {
@@ -93,7 +95,7 @@ export default function Home() {
93
95
// Send the JSX content as a separate payload
94
96
await webcontainerInstanceRef . current ! . fs . writeFile ( filePath , value ) ;
95
97
// Optionally, you can log a success message
96
- console . log ( `Successfully updated ${ filePath } ` ) ;
98
+ // console.log(`Successfully updated ${filePath}`);
97
99
} catch ( error : any ) {
98
100
// Handle errors
99
101
throw new Error ( `Unable to update ${ filePath } : ${ error . message } ` ) ;
@@ -126,10 +128,33 @@ export default function Home() {
126
128
const webcontainerInstance = await WebContainer . boot ( ) ;
127
129
webcontainerInstanceRef . current = webcontainerInstance ;
128
130
webcontainerInstance . on ( "server-ready" , async ( port , url ) => {
129
- setLoadingWebContainer ( false ) ;
130
131
if ( previewRef . current ) {
131
132
previewRef . current . src = url ;
132
133
}
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
+ // };
133
158
} ) ;
134
159
const startDevServer = async ( ) => {
135
160
const initProcess = await webcontainerInstance . spawn ( "npx" , [
@@ -142,7 +167,7 @@ export default function Home() {
142
167
initProcess . output . pipeTo (
143
168
new WritableStream ( {
144
169
write : ( chunk ) => {
145
- console . log ( chunk ) ;
170
+ // console.log(chunk);
146
171
} ,
147
172
} )
148
173
) ;
@@ -247,6 +272,10 @@ export default function Home() {
247
272
document . removeEventListener ( "mousedown" , handleClickOutside ) ;
248
273
} ;
249
274
} , [ 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
+ } ;
250
279
return (
251
280
< main className = "bg-[#1B1D24] h-[calc(100svh-64px)] text-white " >
252
281
< div className = "container mx-auto flex flex-col md:flex-row h-full" >
@@ -308,7 +337,7 @@ export default function Home() {
308
337
</ p >
309
338
< button
310
339
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 "
312
341
>
313
342
Show Me!
314
343
</ button >
@@ -350,7 +379,7 @@ export default function Home() {
350
379
</ div >
351
380
< div
352
381
className = {
353
- loadingWebContainer && ! isTerminal
382
+ loadingWebContainer /* && !isTerminal*/
354
383
? "flex-1 flex justify-center items-center flex-col gap-4"
355
384
: "hidden"
356
385
}
@@ -378,21 +407,25 @@ export default function Home() {
378
407
</ div >
379
408
< div
380
409
className = {
381
- ! loadingWebContainer && ! isTerminal
410
+ ! loadingWebContainer /* && !isTerminal*/
382
411
? "bg-white text-black rounded-md m-3 flex-1"
383
412
: "hidden"
384
413
}
385
414
>
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 >
387
420
</ div >
388
- < div
421
+ { /* <div
389
422
className={
390
423
isTerminal
391
424
? "bg-black flex-1 text-white p-4"
392
425
: "bg-black flex-1 hidden"
393
426
}
394
427
ref={terminalRef}
395
- > </ div >
428
+ ></div> */ }
396
429
</ div >
397
430
</ div >
398
431
</ section >
0 commit comments