Skip to content

Commit 65950f7

Browse files
authored
wrap devtools stories in context providers (#82345)
Wraps stories in context providers, and replaces any old arguments that were being passed before the props -> context refactor in devtools Closes NEXT-4642
1 parent cc5e9e6 commit 65950f7

File tree

5 files changed

+170
-97
lines changed

5 files changed

+170
-97
lines changed

packages/next/.storybook/preview.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Preview } from '@storybook/react'
22
import { useInsertionEffect } from 'react'
3+
import { withDevOverlayContexts } from '../src/next-devtools/dev-overlay/storybook/with-dev-overlay-contexts'
34

45
function CreatePortalNode() {
56
useInsertionEffect(() => {
@@ -63,6 +64,7 @@ const preview: Preview = {
6364
},
6465
},
6566
decorators: [
67+
withDevOverlayContexts(),
6668
(Story) => (
6769
<>
6870
<CreatePortalNode />

packages/next/src/next-devtools/dev-overlay/components/devtools-indicator/devtools-indicator.stories.tsx

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Meta, StoryObj } from '@storybook/react'
2-
import type { OverlayState } from '../../shared'
32

43
import { DevToolsIndicator } from './devtools-indicator'
5-
import { INITIAL_OVERLAY_STATE } from '../../shared'
64
import { withShadowPortal } from '../../storybook/with-shadow-portal'
5+
import { withDevOverlayContexts } from '../../storybook/with-dev-overlay-contexts'
6+
import { INITIAL_OVERLAY_STATE, type OverlayState } from '../../shared'
77

88
const meta: Meta<typeof DevToolsIndicator> = {
99
component: DevToolsIndicator,
@@ -35,35 +35,31 @@ const meta: Meta<typeof DevToolsIndicator> = {
3535
),
3636
],
3737
}
38-
39-
export default meta
40-
type Story = StoryObj<typeof DevToolsIndicator>
41-
4238
const state: OverlayState = {
4339
...INITIAL_OVERLAY_STATE,
4440
routerType: 'app',
4541
isErrorOverlayOpen: false,
4642
}
4743

48-
export const Default: Story = {
49-
args: {
50-
state,
51-
dispatch: () => {},
52-
},
53-
}
44+
export default meta
45+
type Story = StoryObj<typeof DevToolsIndicator>
46+
47+
export const Default: Story = {}
5448

5549
export const SingleError: Story = {
56-
args: {
57-
errorCount: 1,
58-
state,
59-
dispatch: () => {},
60-
},
50+
decorators: [
51+
withDevOverlayContexts({
52+
state,
53+
totalErrorCount: 1,
54+
}),
55+
],
6156
}
6257

6358
export const MultipleErrors: Story = {
64-
args: {
65-
errorCount: 3,
66-
state,
67-
dispatch: () => {},
68-
},
59+
decorators: [
60+
withDevOverlayContexts({
61+
state,
62+
totalErrorCount: 3,
63+
}),
64+
],
6965
}
Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import type { Meta, StoryObj } from '@storybook/react'
22
import { NextLogo } from './next-logo'
33
import { withShadowPortal } from '../../storybook/with-shadow-portal'
4-
// TODO: NEXT-4642
5-
const StoryBookNextLogo = (
6-
_: {
7-
issueCount: number
8-
isDevBuilding: boolean
9-
isDevRendering: boolean
10-
} & Record<string, unknown>
11-
) => {
12-
return <NextLogo onTriggerClick={() => {}} />
13-
}
4+
import { withDevOverlayContexts } from '../../storybook/with-dev-overlay-contexts'
145

15-
const meta: Meta<typeof StoryBookNextLogo> = {
16-
component: StoryBookNextLogo,
6+
const meta: Meta<typeof NextLogo> = {
7+
component: NextLogo,
178
parameters: {
189
layout: 'centered',
1910
},
@@ -24,68 +15,100 @@ const meta: Meta<typeof StoryBookNextLogo> = {
2415
}
2516

2617
export default meta
27-
type Story = StoryObj<typeof StoryBookNextLogo>
18+
type Story = StoryObj<typeof NextLogo>
2819

2920
export const NoIssues: Story = {
30-
args: {
31-
issueCount: 0,
32-
isDevBuilding: false,
33-
isDevRendering: false,
34-
},
21+
decorators: [
22+
withDevOverlayContexts({
23+
totalErrorCount: 0,
24+
state: {
25+
buildingIndicator: false,
26+
renderingIndicator: false,
27+
},
28+
}),
29+
],
3530
}
3631

3732
export const SingleIssue: Story = {
38-
args: {
39-
issueCount: 1,
40-
isDevBuilding: false,
41-
isDevRendering: false,
42-
},
33+
decorators: [
34+
withDevOverlayContexts({
35+
totalErrorCount: 1,
36+
state: {
37+
buildingIndicator: false,
38+
renderingIndicator: false,
39+
},
40+
}),
41+
],
4342
}
4443

4544
export const MultipleIssues: Story = {
46-
args: {
47-
issueCount: 5,
48-
isDevBuilding: false,
49-
isDevRendering: false,
50-
},
45+
decorators: [
46+
withDevOverlayContexts({
47+
totalErrorCount: 5,
48+
state: {
49+
buildingIndicator: false,
50+
renderingIndicator: false,
51+
},
52+
}),
53+
],
5154
}
5255

5356
export const ManyIssues: Story = {
54-
args: {
55-
issueCount: 99,
56-
isDevBuilding: false,
57-
isDevRendering: false,
58-
},
57+
decorators: [
58+
withDevOverlayContexts({
59+
totalErrorCount: 99,
60+
state: {
61+
buildingIndicator: false,
62+
renderingIndicator: false,
63+
},
64+
}),
65+
],
5966
}
6067

6168
export const Building: Story = {
62-
args: {
63-
issueCount: 0,
64-
isDevBuilding: true,
65-
isDevRendering: false,
66-
},
69+
decorators: [
70+
withDevOverlayContexts({
71+
totalErrorCount: 0,
72+
state: {
73+
buildingIndicator: true,
74+
renderingIndicator: false,
75+
},
76+
}),
77+
],
6778
}
6879

6980
export const BuildingWithError: Story = {
70-
args: {
71-
issueCount: 1,
72-
isDevBuilding: true,
73-
isDevRendering: false,
74-
},
81+
decorators: [
82+
withDevOverlayContexts({
83+
totalErrorCount: 1,
84+
state: {
85+
buildingIndicator: true,
86+
renderingIndicator: false,
87+
},
88+
}),
89+
],
7590
}
7691

7792
export const Rendering: Story = {
78-
args: {
79-
issueCount: 0,
80-
isDevBuilding: false,
81-
isDevRendering: true,
82-
},
93+
decorators: [
94+
withDevOverlayContexts({
95+
totalErrorCount: 0,
96+
state: {
97+
buildingIndicator: false,
98+
renderingIndicator: true,
99+
},
100+
}),
101+
],
83102
}
84103

85104
export const RenderingWithError: Story = {
86-
args: {
87-
issueCount: 1,
88-
isDevBuilding: false,
89-
isDevRendering: true,
90-
},
105+
decorators: [
106+
withDevOverlayContexts({
107+
totalErrorCount: 1,
108+
state: {
109+
buildingIndicator: false,
110+
renderingIndicator: true,
111+
},
112+
}),
113+
],
91114
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { useRef, useState, type Dispatch, type SetStateAction } from 'react'
2+
import { DevOverlayContext } from '../../dev-overlay.browser'
3+
import { RenderErrorContext } from '../dev-overlay'
4+
import { PanelRouterContext, type PanelStateKind } from '../menu/context'
5+
import { INITIAL_OVERLAY_STATE } from '../shared'
6+
import type { OverlayState, DispatcherEvent } from '../shared'
7+
import type { ReadyRuntimeError } from '../utils/get-error-by-type'
8+
9+
interface WithDevOverlayContextsProps {
10+
state?: Partial<OverlayState>
11+
dispatch?: (action: DispatcherEvent) => void
12+
runtimeErrors?: ReadyRuntimeError[]
13+
totalErrorCount?: number
14+
panel?: PanelStateKind | null
15+
setPanel?: Dispatch<SetStateAction<PanelStateKind | null>>
16+
selectedIndex?: number
17+
setSelectedIndex?: Dispatch<SetStateAction<number>>
18+
}
19+
20+
export const withDevOverlayContexts =
21+
(props?: WithDevOverlayContextsProps) => (Story: any) => {
22+
const [panel, setPanel] = useState<PanelStateKind | null>(
23+
props?.panel ?? null
24+
)
25+
const [selectedIndex, setSelectedIndex] = useState(
26+
props?.selectedIndex ?? -1
27+
)
28+
const triggerRef = useRef<HTMLButtonElement>(null)
29+
30+
const defaultState: OverlayState = {
31+
...INITIAL_OVERLAY_STATE,
32+
routerType: 'app',
33+
isErrorOverlayOpen: false,
34+
...props?.state,
35+
}
36+
37+
const defaultDispatch = props?.dispatch || (() => {})
38+
39+
return (
40+
<DevOverlayContext.Provider
41+
value={{
42+
state: defaultState,
43+
dispatch: defaultDispatch,
44+
getSquashedHydrationErrorDetails: () => null,
45+
}}
46+
>
47+
<RenderErrorContext.Provider
48+
value={{
49+
runtimeErrors: props?.runtimeErrors ?? [],
50+
totalErrorCount: props?.totalErrorCount ?? 0,
51+
}}
52+
>
53+
<PanelRouterContext.Provider
54+
value={{
55+
panel,
56+
setPanel: props?.setPanel ?? setPanel,
57+
triggerRef,
58+
selectedIndex,
59+
setSelectedIndex: props?.setSelectedIndex ?? setSelectedIndex,
60+
}}
61+
>
62+
<Story />
63+
</PanelRouterContext.Provider>
64+
</RenderErrorContext.Provider>
65+
</DevOverlayContext.Provider>
66+
)
67+
}

0 commit comments

Comments
 (0)