Skip to content

Commit

Permalink
fix(SandpackConsole): clear logs based on maxMessageCount option (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
SSHari committed Apr 1, 2023
1 parent 3bb89b4 commit c8afd3e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
44 changes: 43 additions & 1 deletion sandpack-react/src/components/Console/Console.stories.tsx
@@ -1,4 +1,4 @@
import React from "react";
import React, { useState } from "react";

import { SandpackCodeEditor, SandpackPreview } from "..";
import { SandpackProvider, SandpackLayout, Sandpack } from "../..";
Expand Down Expand Up @@ -182,3 +182,45 @@ console.log("foo");`,
</SandpackLayout>
</SandpackProvider>
);

export const MaxMessageCount = () => {
const [mode, setMode] = useState('client');
const [maxMessageCount, setMaxMessageCount] = useState(5);

return (
<>
<SandpackProvider
key={mode}
files={{
"/index.js": `new Array(10).fill('').forEach((_, i) => console.log(i));`,
"/package.json": JSON.stringify({
scripts: { start: "node index.js" },
}),
}}
options={{ visibleFiles: ["/index.js"], recompileDelay: 500 }}
template={mode === 'client' ? 'vanilla' : 'node'}
>
<SandpackLayout>
<SandpackCodeEditor />
<SandpackConsole standalone maxMessageCount={Number(maxMessageCount)} />
</SandpackLayout>
<div style={{
marginTop: 32,
display: 'flex',
flexDirection: 'column',
gap: 8,
justifyItems: 'left',
width: 'fit-content'
}}>
<button onClick={() => setMode(mode === 'client' ? 'server' : 'client')}>
Toggle mode: {mode}
</button>
<label style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
<span>Max Message Count</span>
<input type="number" value={maxMessageCount} onChange={e => setMaxMessageCount(+e.target.value)} />
</label>
</div>
</SandpackProvider>
</>
);
}
Expand Up @@ -67,7 +67,7 @@ export const useSandpackConsole = ({
}
);

while (messages.length > MAX_MESSAGE_COUNT) {
while (messages.length > maxMessageCount) {
messages.shift();
}

Expand Down
2 changes: 1 addition & 1 deletion sandpack-react/src/hooks/useSandpackShellStdout.ts
Expand Up @@ -38,7 +38,7 @@ export const useSandpackShellStdout = ({
{ data: message.payload.data!, id: generateRandomId() },
];

while (messages.length > MAX_MESSAGE_COUNT) {
while (messages.length > maxMessageCount) {
messages.shift();
}

Expand Down

1 comment on commit c8afd3e

@vercel
Copy link

@vercel vercel bot commented on c8afd3e Apr 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sandpack-docs – ./website/docs

sandpack-docs-codesandbox1.vercel.app
sandpack-docs-git-main-codesandbox1.vercel.app
sandpack.vercel.app

Please sign in to comment.