Skip to content

Commit c722d07

Browse files
committed
fix console types
1 parent 90d93dc commit c722d07

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

sandpack-client/src/types.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ export interface BaseSandpackMessage {
112112
codesandbox?: boolean;
113113
}
114114

115+
export type SandpackConsoleMethods =
116+
| "log"
117+
| "debug"
118+
| "info"
119+
| "warn"
120+
| "error"
121+
| "table"
122+
| "clear"
123+
| "time"
124+
| "timeEnd"
125+
| "count"
126+
| "assert";
127+
115128
export type SandpackMessage = BaseSandpackMessage &
116129
(
117130
| {
@@ -193,8 +206,8 @@ export type SandpackMessage = BaseSandpackMessage &
193206
}
194207
| {
195208
type: "console";
196-
logs: Array<{
197-
method: string;
209+
log: Array<{
210+
method: SandpackConsoleMethods;
198211
id: string;
199212
data: string[];
200213
}>;

sandpack-react/src/components/Console/index.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { useClasser } from "@code-hike/classer";
22
import * as React from "react";
33

44
import { useSandpack } from "../../hooks/useSandpack";
5-
import { getType, ConsoleData } from "./utils";
65
import { RefreshIcon } from "../../icons";
76
import { CodeEditor } from "../CodeEditor";
87

8+
import type { ConsoleData } from "./utils";
9+
import { getType } from "./utils";
10+
911
const MAX_MESSAGE_COUNT = 100;
1012

13+
// TODO: consume clientid
1114
export const SandpackConsole: React.FC<{ clientId?: string }> = ({
1215
clientId,
1316
}) => {
@@ -20,7 +23,6 @@ export const SandpackConsole: React.FC<{ clientId?: string }> = ({
2023
React.useEffect(() => {
2124
const unsubscribe = listen((message) => {
2225
if (message.type === "console" && message.codesandbox) {
23-
console.log(message);
2426
setLogs((prev) => {
2527
const messages = [...prev, ...message.log];
2628
messages.slice(Math.max(0, messages.length - MAX_MESSAGE_COUNT));
@@ -41,7 +43,7 @@ export const SandpackConsole: React.FC<{ clientId?: string }> = ({
4143

4244
return (
4345
<div className={c("console")}>
44-
<div className={c("console-scroll")} ref={wrapperRef}>
46+
<div ref={wrapperRef} className={c("console-scroll")}>
4547
{logs.map(({ data, id, method }) => {
4648
return (
4749
<p
@@ -58,11 +60,11 @@ export const SandpackConsole: React.FC<{ clientId?: string }> = ({
5860
const children = JSON.stringify(msg);
5961

6062
return (
61-
<span className={c("console-span")} key={`${msg}-${index}`}>
63+
<span key={`${msg}-${index}`} className={c("console-span")}>
6264
<CodeEditor
63-
initMode="user-visible"
64-
fileType="js"
6565
code={children}
66+
fileType="js"
67+
initMode="user-visible"
6668
readOnly
6769
/>
6870
</span>
@@ -74,7 +76,7 @@ export const SandpackConsole: React.FC<{ clientId?: string }> = ({
7476
})}
7577
</div>
7678

77-
<button className={c("console-clean")} onClick={() => setLogs([])}>
79+
<button className={c("console-clean")} onClick={(): void => setLogs([])}>
7880
<RefreshIcon />
7981
</button>
8082
</div>
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export const getType = (message: Methods): "info" | "warning" | "error" => {
1+
import type { SandpackConsoleMethods } from "@codesandbox/sandpack-client";
2+
3+
export const getType = (
4+
message: SandpackConsoleMethods
5+
): "info" | "warning" | "error" => {
26
if (message === "log" || message === "info") {
37
return "info";
48
}
@@ -13,18 +17,5 @@ export const getType = (message: Methods): "info" | "warning" | "error" => {
1317
export type ConsoleData = Array<{
1418
data: Array<string | Record<string, string>>;
1519
id: string;
16-
method: Methods;
20+
method: SandpackConsoleMethods;
1721
}>;
18-
19-
type Methods =
20-
| "log"
21-
| "debug"
22-
| "info"
23-
| "warn"
24-
| "error"
25-
| "table"
26-
| "clear"
27-
| "time"
28-
| "timeEnd"
29-
| "count"
30-
| "assert";

0 commit comments

Comments
 (0)