-
Notifications
You must be signed in to change notification settings - Fork 307
/
SandpackConsole.tsx
172 lines (153 loc) · 4.48 KB
/
SandpackConsole.tsx
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import * as React from "react";
import { useSandpack, useSandpackShell } from "../..";
import { useSandpackShellStdout } from "../../hooks/useSandpackShellStdout";
import { css, THEME_PREFIX } from "../../styles";
import { classNames } from "../../utils/classNames";
import { PreviewProgress } from "../Preview/PreviewProgress";
import { SandpackStack } from "../common";
import { RoundedButton } from "../common/RoundedButton";
import { CleanIcon, RestartIcon } from "../icons";
import { ConsoleList } from "./ConsoleList";
import { Header } from "./Header";
import { StdoutList } from "./StdoutList";
import { useSandpackConsole } from "./useSandpackConsole";
import type { SandpackConsoleData } from "./utils/getType";
interface SandpackConsoleProps {
clientId?: string;
showHeader?: boolean;
showSyntaxError?: boolean;
showSetupProgress?: boolean;
maxMessageCount?: number;
onLogsChange?: (logs: SandpackConsoleData) => void;
resetOnPreviewRestart?: boolean;
}
export interface SandpackConsoleRef {
reset: () => void;
}
/**
* `SandpackConsole` is a Sandpack devtool that allows printing
* the console logs from a Sandpack client. It is designed to be
* a light version of a browser console, which means that it's
* limited to a set of common use cases you may encounter when coding.
*/
export const SandpackConsole = React.forwardRef<
SandpackConsoleRef,
React.HTMLAttributes<HTMLDivElement> & SandpackConsoleProps
>(
(
{
showHeader = true,
showSyntaxError = false,
maxMessageCount,
onLogsChange,
className,
showSetupProgress = false,
resetOnPreviewRestart = false,
...props
},
ref
) => {
const {
sandpack: { environment },
} = useSandpack();
const { restart } = useSandpackShell();
const [currentTab, setCurrentTab] = React.useState<"server" | "client">(
environment === "node" ? "server" : "client"
);
const { logs: consoleData, reset: resetConsole } = useSandpackConsole({
maxMessageCount,
showSyntaxError,
resetOnPreviewRestart,
});
const { logs: stdoutData, reset: resetStdout } = useSandpackShellStdout({
maxMessageCount,
resetOnPreviewRestart,
});
const wrapperRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
onLogsChange?.(consoleData);
if (wrapperRef.current) {
wrapperRef.current.scrollTop = wrapperRef.current.scrollHeight;
}
}, [onLogsChange, consoleData, stdoutData, currentTab]);
const isServerTab = currentTab === "server";
const isNodeEnvironment = environment === "node";
React.useImperativeHandle(ref, () => ({
reset() {
resetConsole();
resetStdout();
},
}));
return (
<SandpackStack
className={classNames(
css({
height: "100%",
background: "$surface1",
iframe: { display: "none" },
}),
`${THEME_PREFIX}-console`,
className
)}
{...props}
>
{showSetupProgress && (
<PreviewProgress timeout={1_000} dismissOnTimeout />
)}
{(showHeader || isNodeEnvironment) && (
<Header
currentTab={currentTab}
node={isNodeEnvironment}
setCurrentTab={setCurrentTab}
/>
)}
<div
ref={wrapperRef}
className={classNames(
css({ overflow: "auto", scrollBehavior: "smooth" })
)}
>
{isServerTab ? (
<StdoutList data={stdoutData} />
) : (
<ConsoleList data={consoleData} />
)}
</div>
<div
className={classNames(
css({
position: "absolute",
bottom: "$space$2",
right: "$space$2",
display: "flex",
gap: "$space$2",
})
)}
>
{isServerTab && (
<RoundedButton
onClick={(): void => {
restart();
resetConsole();
resetStdout();
}}
>
<RestartIcon />
</RoundedButton>
)}
<RoundedButton
onClick={(): void => {
if (currentTab === "client") {
resetConsole();
} else {
resetStdout();
}
}}
>
<CleanIcon />
</RoundedButton>
</div>
</SandpackStack>
);
}
);