Unverified Commit 44c0809a authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

[devtools] port call stack (#80550)

Since the Error Overlay call stack had dedicated handlers for animation, it wasn't able to reuse them. Therefore, port the UI part of the call stack and receive handlers as props, so that the call stack component can be shared with the Error Overlay and the DevTools panel.
parent 8a9682f7
import type { Meta, StoryObj } from '@storybook/react' import type { Meta, StoryObj } from '@storybook/react'
import { CallStack } from './call-stack' import { CallStack } from './call-stack'
import { withShadowPortal } from '../../../storybook/with-shadow-portal' import { withShadowPortal } from '../../storybook/with-shadow-portal'
const meta: Meta<typeof CallStack> = { const meta: Meta<typeof CallStack> = {
component: CallStack, component: CallStack,
......
import type { OriginalStackFrame } from '../../../../shared/stack-frame' import type { OriginalStackFrame } from '../../../shared/stack-frame'
import { useMemo, useState, useRef } from 'react'
import { CallStackFrame } from '../../call-stack-frame/call-stack-frame' import { CallStackFrame } from '../call-stack-frame/call-stack-frame'
import { ChevronUpDownIcon } from '../../icons/chevron-up-down'
interface CallStackProps { import { css } from '../../utils/css'
export function CallStack({
frames,
isIgnoreListOpen,
ignoredFramesTally,
onToggleIgnoreList,
}: {
frames: OriginalStackFrame[] frames: OriginalStackFrame[]
dialogResizerRef: React.RefObject<HTMLDivElement | null> isIgnoreListOpen: boolean
} ignoredFramesTally: number
onToggleIgnoreList: () => void
export function CallStack({ frames, dialogResizerRef }: CallStackProps) { }) {
const initialDialogHeight = useRef<number>(NaN)
const [isIgnoreListOpen, setIsIgnoreListOpen] = useState(false)
const ignoredFramesTally = useMemo(() => {
return frames.reduce((tally, frame) => tally + (frame.ignored ? 1 : 0), 0)
}, [frames])
function onToggleIgnoreList() {
const dialog = dialogResizerRef?.current as HTMLElement
if (!dialog) {
return
}
const { height: currentHeight } = dialog?.getBoundingClientRect()
if (!initialDialogHeight.current) {
initialDialogHeight.current = currentHeight
}
if (isIgnoreListOpen) {
function onTransitionEnd() {
dialog.removeEventListener('transitionend', onTransitionEnd)
setIsIgnoreListOpen(false)
}
dialog.style.height = `${initialDialogHeight.current}px`
dialog.addEventListener('transitionend', onTransitionEnd)
} else {
setIsIgnoreListOpen(true)
}
}
return ( return (
<div className="error-overlay-call-stack-container"> <div data-nextjs-call-stack-container>
<div className="error-overlay-call-stack-header"> <div data-nextjs-call-stack-header>
<p className="error-overlay-call-stack-title"> <p data-nextjs-call-stack-title>
Call Stack{' '} Call Stack <span data-nextjs-call-stack-count>{frames.length}</span>
<span className="error-overlay-call-stack-count">
{frames.length}
</span>
</p> </p>
{ignoredFramesTally > 0 && ( {ignoredFramesTally > 0 && (
<button <button
data-expand-ignore-button={isIgnoreListOpen} // The isIgnoreListOpen value is used by tests to confirm whether it is open or not.
className="error-overlay-call-stack-ignored-list-toggle-button" data-nextjs-call-stack-ignored-list-toggle-button={isIgnoreListOpen}
onClick={onToggleIgnoreList} onClick={onToggleIgnoreList}
> >
{`${isIgnoreListOpen ? 'Hide' : 'Show'} ${ignoredFramesTally} ignore-listed frame(s)`} {`${isIgnoreListOpen ? 'Hide' : 'Show'} ${ignoredFramesTally} ignore-listed frame(s)`}
<ChevronUpDown /> <ChevronUpDownIcon />
</button> </button>
)} )}
</div> </div>
...@@ -69,32 +41,13 @@ export function CallStack({ frames, dialogResizerRef }: CallStackProps) { ...@@ -69,32 +41,13 @@ export function CallStack({ frames, dialogResizerRef }: CallStackProps) {
) )
} }
function ChevronUpDown() { export const CALL_STACK_STYLES = css`
return ( [data-nextjs-call-stack-container] {
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M8.70722 2.39641C8.3167 2.00588 7.68353 2.00588 7.29301 2.39641L4.46978 5.21963L3.93945 5.74996L5.00011 6.81062L5.53044 6.28029L8.00011 3.81062L10.4698 6.28029L11.0001 6.81062L12.0608 5.74996L11.5304 5.21963L8.70722 2.39641ZM5.53044 9.71963L5.00011 9.1893L3.93945 10.25L4.46978 10.7803L7.29301 13.6035C7.68353 13.994 8.3167 13.994 8.70722 13.6035L11.5304 10.7803L12.0608 10.25L11.0001 9.1893L10.4698 9.71963L8.00011 12.1893L5.53044 9.71963Z"
fill="currentColor"
/>
</svg>
)
}
export const CALL_STACK_STYLES = `
.error-overlay-call-stack-container {
position: relative; position: relative;
margin-top: 8px; margin-top: 8px;
} }
.error-overlay-call-stack-header { [data-nextjs-call-stack-header] {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
...@@ -103,7 +56,7 @@ export const CALL_STACK_STYLES = ` ...@@ -103,7 +56,7 @@ export const CALL_STACK_STYLES = `
width: 100%; width: 100%;
} }
.error-overlay-call-stack-title { [data-nextjs-call-stack-title] {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
...@@ -116,7 +69,7 @@ export const CALL_STACK_STYLES = ` ...@@ -116,7 +69,7 @@ export const CALL_STACK_STYLES = `
font-weight: 500; font-weight: 500;
} }
.error-overlay-call-stack-count { [data-nextjs-call-stack-count] {
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
...@@ -135,7 +88,7 @@ export const CALL_STACK_STYLES = ` ...@@ -135,7 +88,7 @@ export const CALL_STACK_STYLES = `
background: var(--color-gray-300); background: var(--color-gray-300);
} }
.error-overlay-call-stack-ignored-list-toggle-button { [data-nextjs-call-stack-ignored-list-toggle-button] {
all: unset; all: unset;
display: flex; display: flex;
align-items: center; align-items: center;
......
import type { Meta, StoryObj } from '@storybook/react'
import { ErrorOverlayCallStack } from './error-overlay-call-stack'
import { withShadowPortal } from '../../../storybook/with-shadow-portal'
const meta: Meta<typeof ErrorOverlayCallStack> = {
component: ErrorOverlayCallStack,
parameters: {
layout: 'fullscreen',
backgrounds: {
default: 'background-100-dark',
},
a11y: {
config: {
rules: [
{
id: 'color-contrast',
// Manual testing shows no violation.
// TODO: We might have setup more explicit backgrounds depending on theme.
enabled: false,
},
],
},
},
},
decorators: [withShadowPortal],
}
export default meta
type Story = StoryObj<typeof ErrorOverlayCallStack>
const frame = {
originalStackFrame: {
file: './app/page.tsx',
methodName: 'MyComponent',
arguments: [],
lineNumber: 10,
column: 5,
ignored: false,
},
sourceStackFrame: {
file: './app/page.tsx',
methodName: 'MyComponent',
arguments: [],
lineNumber: 10,
column: 5,
},
originalCodeFrame: 'export default function MyComponent() {',
error: false as const,
reason: null,
external: false,
ignored: false,
}
const ignoredFrame = {
...frame,
ignored: true,
}
export const SingleFrame: Story = {
args: {
frames: [frame],
},
}
export const MultipleFrames: Story = {
args: {
frames: [
frame,
{
...frame,
originalStackFrame: {
...frame.originalStackFrame,
methodName: 'ParentComponent',
lineNumber: 5,
},
},
...Array(5).fill(ignoredFrame),
{
...frame,
originalStackFrame: {
...frame.originalStackFrame,
methodName: 'GrandparentComponent',
lineNumber: 1,
},
},
...Array(5).fill(ignoredFrame),
],
},
}
import type { OriginalStackFrame } from '../../../../shared/stack-frame'
import { useMemo, useState, useRef } from 'react'
import { CallStack } from '../../call-stack/call-stack'
interface CallStackProps {
frames: OriginalStackFrame[]
dialogResizerRef: React.RefObject<HTMLDivElement | null>
}
export function ErrorOverlayCallStack({
frames,
dialogResizerRef,
}: CallStackProps) {
const initialDialogHeight = useRef<number>(NaN)
const [isIgnoreListOpen, setIsIgnoreListOpen] = useState(false)
const ignoredFramesTally = useMemo(() => {
return frames.reduce((tally, frame) => tally + (frame.ignored ? 1 : 0), 0)
}, [frames])
function onToggleIgnoreList() {
const dialog = dialogResizerRef?.current as HTMLElement
if (!dialog) {
return
}
const { height: currentHeight } = dialog?.getBoundingClientRect()
if (!initialDialogHeight.current) {
initialDialogHeight.current = currentHeight
}
if (isIgnoreListOpen) {
function onTransitionEnd() {
dialog.removeEventListener('transitionend', onTransitionEnd)
setIsIgnoreListOpen(false)
}
dialog.style.height = `${initialDialogHeight.current}px`
dialog.addEventListener('transitionend', onTransitionEnd)
} else {
setIsIgnoreListOpen(true)
}
}
return (
<CallStack
frames={frames}
isIgnoreListOpen={isIgnoreListOpen}
onToggleIgnoreList={onToggleIgnoreList}
ignoredFramesTally={ignoredFramesTally}
/>
)
}
...@@ -28,7 +28,6 @@ import { ...@@ -28,7 +28,6 @@ import {
DIALOG_HEADER_STYLES, DIALOG_HEADER_STYLES,
} from '../dialog/header' } from '../dialog/header'
import { ErrorOverlayDialogBody, DIALOG_BODY_STYLES } from '../dialog/body' import { ErrorOverlayDialogBody, DIALOG_BODY_STYLES } from '../dialog/body'
import { CALL_STACK_STYLES } from '../call-stack/call-stack'
import { OVERLAY_STYLES, ErrorOverlayOverlay } from '../overlay/overlay' import { OVERLAY_STYLES, ErrorOverlayOverlay } from '../overlay/overlay'
import { ErrorOverlayBottomStack } from '../error-overlay-bottom-stack' import { ErrorOverlayBottomStack } from '../error-overlay-bottom-stack'
import type { ErrorBaseProps } from '../error-overlay/error-overlay' import type { ErrorBaseProps } from '../error-overlay/error-overlay'
...@@ -186,7 +185,6 @@ export const styles = ` ...@@ -186,7 +185,6 @@ export const styles = `
${errorTypeLabelStyles} ${errorTypeLabelStyles}
${errorMessageStyles} ${errorMessageStyles}
${toolbarStyles} ${toolbarStyles}
${CALL_STACK_STYLES}
[data-nextjs-error-label-group] { [data-nextjs-error-label-group] {
display: flex; display: flex;
......
import { useMemo } from 'react' import { useMemo } from 'react'
import { CodeFrame } from '../../components/code-frame/code-frame' import { CodeFrame } from '../../components/code-frame/code-frame'
import { CallStack } from '../../components/errors/call-stack/call-stack' import { ErrorOverlayCallStack } from '../../components/errors/error-overlay-call-stack/error-overlay-call-stack'
import { PSEUDO_HTML_DIFF_STYLES } from './component-stack-pseudo-html' import { PSEUDO_HTML_DIFF_STYLES } from './component-stack-pseudo-html'
import { import {
useFrames, useFrames,
...@@ -36,7 +36,10 @@ export function RuntimeError({ error, dialogResizerRef }: RuntimeErrorProps) { ...@@ -36,7 +36,10 @@ export function RuntimeError({ error, dialogResizerRef }: RuntimeErrorProps) {
)} )}
{frames.length > 0 && ( {frames.length > 0 && (
<CallStack dialogResizerRef={dialogResizerRef} frames={frames} /> <ErrorOverlayCallStack
dialogResizerRef={dialogResizerRef}
frames={frames}
/>
)} )}
</> </>
) )
......
export function ChevronUpDownIcon() {
return (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M8.70722 2.39641C8.3167 2.00588 7.68353 2.00588 7.29301 2.39641L4.46978 5.21963L3.93945 5.74996L5.00011 6.81062L5.53044 6.28029L8.00011 3.81062L10.4698 6.28029L11.0001 6.81062L12.0608 5.74996L11.5304 5.21963L8.70722 2.39641ZM5.53044 9.71963L5.00011 9.1893L3.93945 10.25L4.46978 10.7803L7.29301 13.6035C7.68353 13.994 8.3167 13.994 8.70722 13.6035L11.5304 10.7803L12.0608 10.25L11.0001 9.1893L10.4698 9.71963L8.00011 12.1893L5.53044 9.71963Z"
fill="currentColor"
/>
</svg>
)
}
...@@ -28,6 +28,7 @@ import { DEVTOOLS_PANEL_STYLES } from '../components/devtools-panel/devtools-pan ...@@ -28,6 +28,7 @@ import { DEVTOOLS_PANEL_STYLES } from '../components/devtools-panel/devtools-pan
import { DEVTOOLS_PANEL_FOOTER_STYLES } from '../components/devtools-panel/devtools-panel-footer' import { DEVTOOLS_PANEL_FOOTER_STYLES } from '../components/devtools-panel/devtools-panel-footer'
import { DEVTOOLS_PANEL_VERSION_INFO_STYLES } from '../components/devtools-panel/devtools-panel-version-info' import { DEVTOOLS_PANEL_VERSION_INFO_STYLES } from '../components/devtools-panel/devtools-panel-version-info'
import { DEVTOOLS_PANEL_TAB_SETTINGS_STYLES } from '../components/devtools-panel/devtools-panel-tab/settings-tab' import { DEVTOOLS_PANEL_TAB_SETTINGS_STYLES } from '../components/devtools-panel/devtools-panel-tab/settings-tab'
import { CALL_STACK_STYLES } from '../components/call-stack/call-stack'
export function ComponentStyles() { export function ComponentStyles() {
return ( return (
...@@ -35,6 +36,7 @@ export function ComponentStyles() { ...@@ -35,6 +36,7 @@ export function ComponentStyles() {
{css` {css`
${COPY_BUTTON_STYLES} ${COPY_BUTTON_STYLES}
${CALL_STACK_FRAME_STYLES} ${CALL_STACK_FRAME_STYLES}
${CALL_STACK_STYLES}
${ENVIRONMENT_NAME_LABEL_STYLES} ${ENVIRONMENT_NAME_LABEL_STYLES}
${overlay} ${overlay}
${toast} ${toast}
......
...@@ -1678,12 +1678,18 @@ export async function getStackFramesContent(browser) { ...@@ -1678,12 +1678,18 @@ export async function getStackFramesContent(browser) {
} }
export async function toggleCollapseCallStackFrames(browser: Playwright) { export async function toggleCollapseCallStackFrames(browser: Playwright) {
const button = await browser.elementByCss('[data-expand-ignore-button]') const button = await browser.elementByCss(
const lastExpanded = await button.getAttribute('data-expand-ignore-button') '[data-nextjs-call-stack-ignored-list-toggle-button]'
)
const lastExpanded = await button.getAttribute(
'data-nextjs-call-stack-ignored-list-toggle-button'
)
await button.click() await button.click()
await retry(async () => { await retry(async () => {
const currExpanded = await button.getAttribute('data-expand-ignore-button') const currExpanded = await button.getAttribute(
'data-nextjs-call-stack-ignored-list-toggle-button'
)
expect(currExpanded).not.toBe(lastExpanded) expect(currExpanded).not.toBe(lastExpanded)
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment