-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathExampleTerminal.tsx
More file actions
67 lines (54 loc) · 1.52 KB
/
ExampleTerminal.tsx
File metadata and controls
67 lines (54 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type { Terminal as XTerm } from '@xterm/xterm';
import { Suspense, lazy, useEffect, useState } from 'react';
import { useTheme } from './hooks/useTheme';
import { useWebContainer } from './hooks/useWebcontainer';
const Terminal = lazy(() => import('@tutorialkit/react/core/Terminal'));
export default function ExampleTerminal() {
// only needed in astro because of SSR
const [domLoaded, setDomLoaded] = useState(false);
const theme = useTheme();
const { setTerminal } = useTerminal();
useEffect(() => {
setDomLoaded(true);
}, []);
return (
domLoaded && (
<Suspense>
<Terminal className="h-32" readonly={false} theme={theme} onTerminalReady={setTerminal} />
</Suspense>
)
);
}
function useTerminal() {
const webcontainerPromise = useWebContainer();
const [terminal, setTerminal] = useState<XTerm | null>(null);
useEffect(() => {
if (!terminal) {
return;
}
run(terminal);
async function run(terminal: XTerm) {
const webcontainer = await webcontainerPromise;
const process = await webcontainer.spawn('jsh', {
terminal: {
cols: terminal.cols,
rows: terminal.rows,
},
});
process.output.pipeTo(
new WritableStream({
write(data) {
terminal.write(data);
},
}),
);
const shellWriter = process.input.getWriter();
terminal.onData((data) => {
shellWriter.write(data);
});
}
}, [terminal]);
return {
setTerminal,
};
}