-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
Copy pathConsole.tsx
145 lines (128 loc) · 4.12 KB
/
Console.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
import cn from 'classnames';
import * as React from 'react';
import {IconChevron} from 'components/Icon/IconChevron';
import {SandpackCodeViewer, useSandpack} from '@codesandbox/sandpack-react';
const getType = (message: Methods): 'info' | 'warning' | 'error' => {
if (message === 'log' || message === 'info') {
return 'info';
}
if (message === 'warn') {
return 'warning';
}
return 'error';
};
type ConsoleData = Array<{
data: Array<string | Record<string, string>>;
id: string;
method: Methods;
}>;
type Methods =
| 'log'
| 'debug'
| 'info'
| 'warn'
| 'error'
| 'table'
| 'clear'
| 'time'
| 'timeEnd'
| 'count'
| 'assert';
const MAX_MESSAGE_COUNT = 100;
export const SandpackConsole: React.FC<{clientId?: string}> = ({clientId}) => {
const {listen} = useSandpack();
const [logs, setLogs] = React.useState<ConsoleData>([]);
const wrapperRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
const unsubscribe = listen((message) => {
if (message.type === 'start') {
setLogs([]);
}
// there is no such type as console in Sandpack
if (message.type === 'console' && message.codesandbox) {
setLogs((prev) => {
const messages = [...prev, ...message.log];
messages.slice(Math.max(0, messages.length - MAX_MESSAGE_COUNT));
return messages;
});
}
});
return unsubscribe;
}, [listen]);
const [showConsole, toggleConsole] = React.useState(false);
React.useEffect(() => {
if (wrapperRef.current) {
wrapperRef.current.scrollTop = wrapperRef.current.scrollHeight;
}
}, [logs]);
return (
<div
className={cn(
'absolute dark:border-gray-700 dark:bg-gray-95 border-t bottom-0 w-full',
!!!logs.length && 'cursor-not-allowed'
)}>
<div className="flex justify-between h-8 items-center">
<div onClick={() => !!logs.length && toggleConsole(!showConsole)}>
<IconChevron displayDirection={showConsole ? 'down' : 'right'} />
</div>
<p className="p-1 text-md">console ({logs.length})</p>
<button
className={cn('p-1', !!!logs.length && 'cursor-not-allowed')}
onClick={() => {
setLogs([]);
toggleConsole(false);
}}>
<svg
viewBox="0 0 24 24"
width="18"
height="18"
stroke="currentColor"
strokeWidth="2"
fill="none"
strokeLinecap="round"
strokeLinejoin="round">
<circle cx="12" cy="12" r="10"></circle>
<line x1="4.93" y1="4.93" x2="19.07" y2="19.07"></line>
</svg>
</button>
</div>
{showConsole && (
<div className="w-full h-full border-y dark:border-gray-700 dark:bg-gray-95 dark:text-white">
<div className={cn('console-scroll')} ref={wrapperRef}>
{logs.map(({data, id, method}) => {
return (
<p
key={id}
className={cn(
'border-y border dark:border-gray-700 text-md p-1 pl-2',
`console-${getType(method)}`
)}>
<span className={cn('console-message')}>
{data.map((msg, index) => {
if (typeof msg === 'string') {
return <span key={`${msg}-${index}`}>{msg}</span>;
}
console.log('console', console);
const children = JSON.stringify(msg);
return (
<span
className={cn('console-span')}
key={`${msg}-${index}`}>
<SandpackCodeViewer
initMode="user-visible"
// fileType="js"
code={children}
/>
</span>
);
})}
</span>
</p>
);
})}
</div>
</div>
)}
</div>
);
};