Browse Source
[Beta] fix(SandpackConsole): avoid unsubscribing the logs listeners (#5093)
* Update 3 files
* Delete console-issue.md
* Filter out console error addendum
Co-authored-by: dan <dan.abramov@gmail.com>
main
Danilo Woznica
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
21 additions and
9 deletions
-
beta/src/components/MDX/Sandpack/Console.tsx
-
beta/src/components/MDX/Sandpack/Preview.tsx
|
|
@ -87,7 +87,7 @@ type ConsoleData = Array<{ |
|
|
|
|
|
|
|
const MAX_MESSAGE_COUNT = 100; |
|
|
|
|
|
|
|
export const SandpackConsole = () => { |
|
|
|
export const SandpackConsole = ({visible}: {visible: boolean}) => { |
|
|
|
const {listen} = useSandpack(); |
|
|
|
const [logs, setLogs] = React.useState<ConsoleData>([]); |
|
|
|
const wrapperRef = React.useRef<HTMLDivElement>(null); |
|
|
@ -107,7 +107,19 @@ export const SandpackConsole = () => { |
|
|
|
} |
|
|
|
if (message.type === 'console' && message.codesandbox) { |
|
|
|
setLogs((prev) => { |
|
|
|
const newLogs = message.log.map((consoleData) => { |
|
|
|
const newLogs = message.log |
|
|
|
.filter((consoleData) => { |
|
|
|
if ( |
|
|
|
typeof consoleData.data[0] === 'string' && |
|
|
|
consoleData.data[0].indexOf('The above error occurred') !== -1 |
|
|
|
) { |
|
|
|
// Don't show React error addendum because
|
|
|
|
// we have a custom error overlay.
|
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
}) |
|
|
|
.map((consoleData) => { |
|
|
|
return { |
|
|
|
...consoleData, |
|
|
|
data: formatStr(...consoleData.data), |
|
|
@ -136,7 +148,7 @@ export const SandpackConsole = () => { |
|
|
|
} |
|
|
|
}, [logs]); |
|
|
|
|
|
|
|
if (logs.length === 0) { |
|
|
|
if (!visible || logs.length === 0) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -222,7 +222,7 @@ export function Preview({ |
|
|
|
loading={!isReady && iframeComputedHeight === null} |
|
|
|
/> |
|
|
|
</div> |
|
|
|
{!error && <SandpackConsole />} |
|
|
|
<SandpackConsole visible={!error} /> |
|
|
|
</SandpackStack> |
|
|
|
); |
|
|
|
} |
|
|
|