Unverified Commit 4ab6eba5 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[dev-overlay] Move Redbox open/close into dispatcher (#79698)

parent 826da1f7
import type { OverlayState } from '../shared' import {
ACTION_ERROR_OVERLAY_OPEN,
type OverlayDispatch,
type OverlayState,
} from '../shared'
import type { GlobalErrorComponent } from '../../global-error' import type { GlobalErrorComponent } from '../../global-error'
import { useCallback, useEffect, useState } from 'react' import { useCallback, useEffect } from 'react'
import { AppDevOverlayErrorBoundary } from './app-dev-overlay-error-boundary' import { AppDevOverlayErrorBoundary } from './app-dev-overlay-error-boundary'
import { FontStyles } from '../font/font-styles' import { FontStyles } from '../font/font-styles'
import { DevOverlay } from '../ui/dev-overlay' import { DevOverlay } from '../ui/dev-overlay'
...@@ -70,17 +74,18 @@ function ReplaySsrOnlyErrors({ ...@@ -70,17 +74,18 @@ function ReplaySsrOnlyErrors({
export function AppDevOverlay({ export function AppDevOverlay({
state, state,
dispatch,
globalError, globalError,
children, children,
}: { }: {
state: OverlayState state: OverlayState
dispatch: OverlayDispatch
globalError: [GlobalErrorComponent, React.ReactNode] globalError: [GlobalErrorComponent, React.ReactNode]
children: React.ReactNode children: React.ReactNode
}) { }) {
const [isErrorOverlayOpen, setIsErrorOverlayOpen] = useState(false)
const openOverlay = useCallback(() => { const openOverlay = useCallback(() => {
setIsErrorOverlayOpen(true) dispatch({ type: ACTION_ERROR_OVERLAY_OPEN })
}, []) }, [dispatch])
return ( return (
<> <>
...@@ -94,11 +99,7 @@ export function AppDevOverlay({ ...@@ -94,11 +99,7 @@ export function AppDevOverlay({
<> <>
{/* Fonts can only be loaded outside the Shadow DOM. */} {/* Fonts can only be loaded outside the Shadow DOM. */}
<FontStyles /> <FontStyles />
<DevOverlay <DevOverlay state={state} dispatch={dispatch} />
state={state}
isErrorOverlayOpen={isErrorOverlayOpen}
setIsErrorOverlayOpen={setIsErrorOverlayOpen}
/>
</> </>
</> </>
) )
......
...@@ -624,7 +624,7 @@ export default function HotReload({ ...@@ -624,7 +624,7 @@ export default function HotReload({
]) ])
return ( return (
<AppDevOverlay state={state} globalError={globalError}> <AppDevOverlay state={state} dispatch={dispatch} globalError={globalError}>
{children} {children}
</AppDevOverlay> </AppDevOverlay>
) )
......
...@@ -30,5 +30,6 @@ export const usePagesDevOverlay = () => { ...@@ -30,5 +30,6 @@ export const usePagesDevOverlay = () => {
return { return {
state, state,
onComponentError, onComponentError,
dispatch,
} }
} }
import { useState } from 'react'
import { PagesDevOverlayErrorBoundary } from './pages-dev-overlay-error-boundary' import { PagesDevOverlayErrorBoundary } from './pages-dev-overlay-error-boundary'
import { usePagesDevOverlay } from './hooks' import { usePagesDevOverlay } from './hooks'
import { FontStyles } from '../font/font-styles' import { FontStyles } from '../font/font-styles'
...@@ -13,9 +12,7 @@ interface PagesDevOverlayProps { ...@@ -13,9 +12,7 @@ interface PagesDevOverlayProps {
} }
export function PagesDevOverlay({ children }: PagesDevOverlayProps) { export function PagesDevOverlay({ children }: PagesDevOverlayProps) {
const { state, onComponentError } = usePagesDevOverlay() const { state, dispatch, onComponentError } = usePagesDevOverlay()
const [isErrorOverlayOpen, setIsErrorOverlayOpen] = useState(true)
return ( return (
<> <>
...@@ -25,11 +22,7 @@ export function PagesDevOverlay({ children }: PagesDevOverlayProps) { ...@@ -25,11 +22,7 @@ export function PagesDevOverlay({ children }: PagesDevOverlayProps) {
{/* Fonts can only be loaded outside the Shadow DOM. */} {/* Fonts can only be loaded outside the Shadow DOM. */}
<FontStyles /> <FontStyles />
<DevOverlay <DevOverlay state={state} dispatch={dispatch} />
state={state}
isErrorOverlayOpen={isErrorOverlayOpen}
setIsErrorOverlayOpen={setIsErrorOverlayOpen}
/>
</> </>
) )
} }
...@@ -27,7 +27,9 @@ export interface OverlayState { ...@@ -27,7 +27,9 @@ export interface OverlayState {
disableDevIndicator: boolean disableDevIndicator: boolean
debugInfo: DebugInfo debugInfo: DebugInfo
routerType: 'pages' | 'app' routerType: 'pages' | 'app'
isErrorOverlayOpen: boolean
} }
export type OverlayDispatch = React.Dispatch<BusEvent>
export const ACTION_STATIC_INDICATOR = 'static-indicator' export const ACTION_STATIC_INDICATOR = 'static-indicator'
export const ACTION_BUILD_OK = 'build-ok' export const ACTION_BUILD_OK = 'build-ok'
...@@ -39,6 +41,9 @@ export const ACTION_UNHANDLED_ERROR = 'unhandled-error' ...@@ -39,6 +41,9 @@ export const ACTION_UNHANDLED_ERROR = 'unhandled-error'
export const ACTION_UNHANDLED_REJECTION = 'unhandled-rejection' export const ACTION_UNHANDLED_REJECTION = 'unhandled-rejection'
export const ACTION_DEBUG_INFO = 'debug-info' export const ACTION_DEBUG_INFO = 'debug-info'
export const ACTION_DEV_INDICATOR = 'dev-indicator' export const ACTION_DEV_INDICATOR = 'dev-indicator'
export const ACTION_ERROR_OVERLAY_OPEN = 'error-overlay-open'
export const ACTION_ERROR_OVERLAY_CLOSE = 'error-overlay-close'
export const ACTION_ERROR_OVERLAY_TOGGLE = 'error-overlay-toggle'
export const STORAGE_KEY_THEME = '__nextjs-dev-tools-theme' export const STORAGE_KEY_THEME = '__nextjs-dev-tools-theme'
export const STORAGE_KEY_POSITION = '__nextjs-dev-tools-position' export const STORAGE_KEY_POSITION = '__nextjs-dev-tools-position'
...@@ -90,6 +95,16 @@ interface DevIndicatorAction { ...@@ -90,6 +95,16 @@ interface DevIndicatorAction {
devIndicator: DevIndicatorServerState devIndicator: DevIndicatorServerState
} }
export interface ErrorOverlayOpenAction {
type: typeof ACTION_ERROR_OVERLAY_OPEN
}
export interface ErrorOverlayCloseAction {
type: typeof ACTION_ERROR_OVERLAY_CLOSE
}
export interface ErrorOverlayToggleAction {
type: typeof ACTION_ERROR_OVERLAY_TOGGLE
}
export type BusEvent = export type BusEvent =
| BuildOkAction | BuildOkAction
| BuildErrorAction | BuildErrorAction
...@@ -101,6 +116,9 @@ export type BusEvent = ...@@ -101,6 +116,9 @@ export type BusEvent =
| StaticIndicatorAction | StaticIndicatorAction
| DebugInfoAction | DebugInfoAction
| DevIndicatorAction | DevIndicatorAction
| ErrorOverlayOpenAction
| ErrorOverlayCloseAction
| ErrorOverlayToggleAction
const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX = const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX =
// 1st group: v8 // 1st group: v8
...@@ -136,7 +154,10 @@ function pushErrorFilterDuplicates( ...@@ -136,7 +154,10 @@ function pushErrorFilterDuplicates(
const shouldDisableDevIndicator = const shouldDisableDevIndicator =
process.env.__NEXT_DEV_INDICATOR?.toString() === 'false' process.env.__NEXT_DEV_INDICATOR?.toString() === 'false'
export const INITIAL_OVERLAY_STATE: Omit<OverlayState, 'routerType'> = { export const INITIAL_OVERLAY_STATE: Omit<
OverlayState,
'isErrorOverlayOpen' | 'routerType'
> = {
nextId: 1, nextId: 1,
buildError: null, buildError: null,
errors: [], errors: [],
...@@ -159,6 +180,10 @@ function getInitialState( ...@@ -159,6 +180,10 @@ function getInitialState(
): OverlayState & { routerType: 'pages' | 'app' } { ): OverlayState & { routerType: 'pages' | 'app' } {
return { return {
...INITIAL_OVERLAY_STATE, ...INITIAL_OVERLAY_STATE,
// Pages Router only listenes to thrown errors which
// always open the overlay.
// TODO: Should be the same default as App Router once we surface console.error in Pages Router.
isErrorOverlayOpen: routerType === 'pages',
routerType, routerType,
} }
} }
...@@ -239,6 +264,15 @@ export function useErrorOverlayReducer(routerType: 'pages' | 'app') { ...@@ -239,6 +264,15 @@ export function useErrorOverlayReducer(routerType: 'pages' | 'app') {
shouldDisableDevIndicator || !!action.devIndicator.disabledUntil, shouldDisableDevIndicator || !!action.devIndicator.disabledUntil,
} }
} }
case ACTION_ERROR_OVERLAY_OPEN: {
return { ...state, isErrorOverlayOpen: true }
}
case ACTION_ERROR_OVERLAY_CLOSE: {
return { ...state, isErrorOverlayOpen: false }
}
case ACTION_ERROR_OVERLAY_TOGGLE: {
return { ...state, isErrorOverlayOpen: !state.isErrorOverlayOpen }
}
default: { default: {
return state return state
} }
......
...@@ -56,13 +56,14 @@ const state: OverlayState = { ...@@ -56,13 +56,14 @@ const state: OverlayState = {
notFound: false, notFound: false,
staticIndicator: true, staticIndicator: true,
debugInfo: { devtoolsFrontendUrl: undefined }, debugInfo: { devtoolsFrontendUrl: undefined },
isErrorOverlayOpen: false,
} }
export const StaticRoute: Story = { export const StaticRoute: Story = {
args: { args: {
errorCount: 0, errorCount: 0,
state, state,
setIsErrorOverlayOpen: () => {}, dispatch: () => {},
}, },
} }
...@@ -73,7 +74,7 @@ export const DynamicRoute: Story = { ...@@ -73,7 +74,7 @@ export const DynamicRoute: Story = {
...state, ...state,
staticIndicator: false, staticIndicator: false,
}, },
setIsErrorOverlayOpen: () => {}, dispatch: () => {},
}, },
} }
...@@ -81,7 +82,7 @@ export const SingleError: Story = { ...@@ -81,7 +82,7 @@ export const SingleError: Story = {
args: { args: {
errorCount: 1, errorCount: 1,
state, state,
setIsErrorOverlayOpen: () => {}, dispatch: () => {},
}, },
} }
...@@ -89,6 +90,6 @@ export const MultipleErrors: Story = { ...@@ -89,6 +90,6 @@ export const MultipleErrors: Story = {
args: { args: {
errorCount: 3, errorCount: 3,
state, state,
setIsErrorOverlayOpen: () => {}, dispatch: () => {},
}, },
} }
import type { CSSProperties, Dispatch, SetStateAction } from 'react' import type { CSSProperties, Dispatch, SetStateAction } from 'react'
import { STORAGE_KEY_POSITION, type OverlayState } from '../../../../shared' import {
ACTION_ERROR_OVERLAY_OPEN,
ACTION_ERROR_OVERLAY_TOGGLE,
STORAGE_KEY_POSITION,
type OverlayDispatch,
type OverlayState,
} from '../../../../shared'
import { useState, useEffect, useRef, createContext, useContext } from 'react' import { useState, useEffect, useRef, createContext, useContext } from 'react'
import { Toast } from '../../toast' import { Toast } from '../../toast'
...@@ -30,15 +36,13 @@ export function DevToolsIndicator({ ...@@ -30,15 +36,13 @@ export function DevToolsIndicator({
state, state,
errorCount, errorCount,
isBuildError, isBuildError,
setIsErrorOverlayOpen,
...props ...props
}: { }: {
state: OverlayState state: OverlayState
dispatch: OverlayDispatch
errorCount: number errorCount: number
isBuildError: boolean isBuildError: boolean
setIsErrorOverlayOpen: (
isErrorOverlayOpen: boolean | ((prev: boolean) => boolean)
) => void
scale: DevToolsScale scale: DevToolsScale
setScale: (value: DevToolsScale) => void setScale: (value: DevToolsScale) => void
}) { }) {
...@@ -57,7 +61,6 @@ export function DevToolsIndicator({ ...@@ -57,7 +61,6 @@ export function DevToolsIndicator({
method: 'POST', method: 'POST',
}) })
}} }}
setIsErrorOverlayOpen={setIsErrorOverlayOpen}
isTurbopack={!!process.env.TURBOPACK} isTurbopack={!!process.env.TURBOPACK}
disabled={state.disableDevIndicator || !isDevToolsIndicatorVisible} disabled={state.disableDevIndicator || !isDevToolsIndicatorVisible}
isBuildError={isBuildError} isBuildError={isBuildError}
...@@ -96,7 +99,7 @@ function DevToolsPopover({ ...@@ -96,7 +99,7 @@ function DevToolsPopover({
isTurbopack, isTurbopack,
isBuildError, isBuildError,
hide, hide,
setIsErrorOverlayOpen, dispatch,
scale, scale,
setScale, setScale,
}: { }: {
...@@ -108,9 +111,7 @@ function DevToolsPopover({ ...@@ -108,9 +111,7 @@ function DevToolsPopover({
isTurbopack: boolean isTurbopack: boolean
isBuildError: boolean isBuildError: boolean
hide: () => void hide: () => void
setIsErrorOverlayOpen: ( dispatch: OverlayDispatch
isOverlayOpen: boolean | ((prev: boolean) => boolean)
) => void
scale: DevToolsScale scale: DevToolsScale
setScale: (value: DevToolsScale) => void setScale: (value: DevToolsScale) => void
}) { }) {
...@@ -210,12 +211,12 @@ function DevToolsPopover({ ...@@ -210,12 +211,12 @@ function DevToolsPopover({
function openErrorOverlay() { function openErrorOverlay() {
setOpen(null) setOpen(null)
if (issueCount > 0) { if (issueCount > 0) {
setIsErrorOverlayOpen(true) dispatch({ type: ACTION_ERROR_OVERLAY_OPEN })
} }
} }
function toggleErrorOverlay() { function toggleErrorOverlay() {
setIsErrorOverlayOpen((prev) => !prev) dispatch({ type: ACTION_ERROR_OVERLAY_TOGGLE })
} }
function openRootMenu() { function openRootMenu() {
......
import type { OverlayState } from '../../../../shared' import {
ACTION_ERROR_OVERLAY_CLOSE,
type OverlayDispatch,
type OverlayState,
} from '../../../../shared'
import { Suspense } from 'react' import { Suspense } from 'react'
import { BuildError } from '../../../container/build-error' import { BuildError } from '../../../container/build-error'
...@@ -18,21 +22,19 @@ export interface ErrorBaseProps { ...@@ -18,21 +22,19 @@ export interface ErrorBaseProps {
export function ErrorOverlay({ export function ErrorOverlay({
state, state,
dispatch,
runtimeErrors, runtimeErrors,
isErrorOverlayOpen,
setIsErrorOverlayOpen,
errorCount, errorCount,
}: { }: {
state: OverlayState state: OverlayState
dispatch: OverlayDispatch
runtimeErrors: ReadyRuntimeError[] runtimeErrors: ReadyRuntimeError[]
isErrorOverlayOpen: boolean
setIsErrorOverlayOpen: (value: boolean) => void
errorCount: number errorCount: number
}) { }) {
const isTurbopack = !!process.env.TURBOPACK const isTurbopack = !!process.env.TURBOPACK
// This hook lets us do an exit animation before unmounting the component // This hook lets us do an exit animation before unmounting the component
const { mounted, rendered } = useDelayedRender(isErrorOverlayOpen, { const { mounted, rendered } = useDelayedRender(state.isErrorOverlayOpen, {
exitDelay: transitionDurationMs, exitDelay: transitionDurationMs,
}) })
...@@ -74,7 +76,7 @@ export function ErrorOverlay({ ...@@ -74,7 +76,7 @@ export function ErrorOverlay({
debugInfo={state.debugInfo} debugInfo={state.debugInfo}
runtimeErrors={runtimeErrors} runtimeErrors={runtimeErrors}
onClose={() => { onClose={() => {
setIsErrorOverlayOpen(false) dispatch({ type: ACTION_ERROR_OVERLAY_CLOSE })
}} }}
/> />
) )
......
import type { Meta, StoryObj } from '@storybook/react' import type { Meta, StoryObj } from '@storybook/react'
import type { OverlayState } from '../shared' import type { BusEvent, OverlayState } from '../shared'
// @ts-expect-error // @ts-expect-error
import imgApp from './app.png' import imgApp from './app.png'
import { useState } from 'react' import { useReducer } from 'react'
import { DevOverlay } from './dev-overlay' import { DevOverlay } from './dev-overlay'
import { ACTION_UNHANDLED_ERROR } from '../shared' import {
ACTION_ERROR_OVERLAY_CLOSE,
ACTION_ERROR_OVERLAY_OPEN,
ACTION_ERROR_OVERLAY_TOGGLE,
ACTION_UNHANDLED_ERROR,
} from '../shared'
const meta: Meta<typeof DevOverlay> = { const meta: Meta<typeof DevOverlay> = {
component: DevOverlay, component: DevOverlay,
...@@ -18,7 +23,7 @@ const meta: Meta<typeof DevOverlay> = { ...@@ -18,7 +23,7 @@ const meta: Meta<typeof DevOverlay> = {
export default meta export default meta
type Story = StoryObj<typeof DevOverlay> type Story = StoryObj<typeof DevOverlay>
const state: OverlayState = { const initialState: OverlayState = {
nextId: 0, nextId: 0,
routerType: 'app', routerType: 'app',
buildError: null, buildError: null,
...@@ -81,11 +86,32 @@ const state: OverlayState = { ...@@ -81,11 +86,32 @@ const state: OverlayState = {
installed: '15.2.0', installed: '15.2.0',
staleness: 'fresh', staleness: 'fresh',
}, },
isErrorOverlayOpen: true,
}
function useOverlayReducer() {
return useReducer<OverlayState, [BusEvent]>((state, action): OverlayState => {
switch (action.type) {
case ACTION_ERROR_OVERLAY_CLOSE: {
return { ...state, isErrorOverlayOpen: false }
}
case ACTION_ERROR_OVERLAY_OPEN: {
return { ...state, isErrorOverlayOpen: true }
}
case ACTION_ERROR_OVERLAY_TOGGLE: {
return { ...state, isErrorOverlayOpen: !state.isErrorOverlayOpen }
}
default: {
return state
}
}
return state
}, initialState)
} }
export const Default: Story = { export const Default: Story = {
render: function DevOverlayStory() { render: function DevOverlayStory() {
const [isErrorOverlayOpen, setIsErrorOverlayOpen] = useState(true) const [state, dispatch] = useOverlayReducer()
return ( return (
<> <>
<img <img
...@@ -96,11 +122,7 @@ export const Default: Story = { ...@@ -96,11 +122,7 @@ export const Default: Story = {
objectFit: 'contain', objectFit: 'contain',
}} }}
/> />
<DevOverlay <DevOverlay state={state} dispatch={dispatch} />
state={state}
isErrorOverlayOpen={isErrorOverlayOpen}
setIsErrorOverlayOpen={setIsErrorOverlayOpen}
/>
</> </>
) )
}, },
......
import type { OverlayState } from '../shared' import type { OverlayDispatch, OverlayState } from '../shared'
import { ShadowPortal } from './components/shadow-portal' import { ShadowPortal } from './components/shadow-portal'
import { Base } from './styles/base' import { Base } from './styles/base'
...@@ -13,14 +13,10 @@ import { useDevToolsScale } from './components/errors/dev-tools-indicator/dev-to ...@@ -13,14 +13,10 @@ import { useDevToolsScale } from './components/errors/dev-tools-indicator/dev-to
export function DevOverlay({ export function DevOverlay({
state, state,
isErrorOverlayOpen, dispatch,
setIsErrorOverlayOpen,
}: { }: {
state: OverlayState state: OverlayState
isErrorOverlayOpen: boolean dispatch: OverlayDispatch
setIsErrorOverlayOpen: (
isErrorOverlayOpen: boolean | ((prev: boolean) => boolean)
) => void
}) { }) {
const [scale, setScale] = useDevToolsScale() const [scale, setScale] = useDevToolsScale()
return ( return (
...@@ -41,18 +37,17 @@ export function DevOverlay({ ...@@ -41,18 +37,17 @@ export function DevOverlay({
scale={scale} scale={scale}
setScale={setScale} setScale={setScale}
state={state} state={state}
dispatch={dispatch}
errorCount={totalErrorCount} errorCount={totalErrorCount}
isBuildError={isBuildError} isBuildError={isBuildError}
setIsErrorOverlayOpen={setIsErrorOverlayOpen}
/> />
)} )}
<ErrorOverlay <ErrorOverlay
state={state} state={state}
dispatch={dispatch}
runtimeErrors={runtimeErrors} runtimeErrors={runtimeErrors}
errorCount={totalErrorCount} errorCount={totalErrorCount}
isErrorOverlayOpen={isErrorOverlayOpen}
setIsErrorOverlayOpen={setIsErrorOverlayOpen}
/> />
</> </>
) )
......
...@@ -209,7 +209,7 @@ describe('Error overlay for hydration errors in App router', () => { ...@@ -209,7 +209,7 @@ describe('Error overlay for hydration errors in App router', () => {
{ {
"componentStack": "... "componentStack": "...
<HotReload assetPrefix="" globalError={[...]}> <HotReload assetPrefix="" globalError={[...]}>
<AppDevOverlay state={{nextId:1, ...}} globalError={[...]}> <AppDevOverlay state={{nextId:1, ...}} dispatch={function bound dispatchReducerAction} globalError={[...]}>
<AppDevOverlayErrorBoundary globalError={[...]} onError={function}> <AppDevOverlayErrorBoundary globalError={[...]} onError={function}>
<ReplaySsrOnlyErrors> <ReplaySsrOnlyErrors>
<DevRootHTTPAccessFallbackBoundary> <DevRootHTTPAccessFallbackBoundary>
...@@ -245,7 +245,7 @@ describe('Error overlay for hydration errors in App router', () => { ...@@ -245,7 +245,7 @@ describe('Error overlay for hydration errors in App router', () => {
{ {
"componentStack": "... "componentStack": "...
<HotReload assetPrefix="" globalError={[...]}> <HotReload assetPrefix="" globalError={[...]}>
<AppDevOverlay state={{nextId:1, ...}} globalError={[...]}> <AppDevOverlay state={{nextId:1, ...}} dispatch={function bound dispatchReducerAction} globalError={[...]}>
<AppDevOverlayErrorBoundary globalError={[...]} onError={function}> <AppDevOverlayErrorBoundary globalError={[...]} onError={function}>
<ReplaySsrOnlyErrors> <ReplaySsrOnlyErrors>
<DevRootHTTPAccessFallbackBoundary> <DevRootHTTPAccessFallbackBoundary>
...@@ -1045,7 +1045,7 @@ describe('Error overlay for hydration errors in App router', () => { ...@@ -1045,7 +1045,7 @@ describe('Error overlay for hydration errors in App router', () => {
{ {
"componentStack": "... "componentStack": "...
<HotReload assetPrefix="" globalError={[...]}> <HotReload assetPrefix="" globalError={[...]}>
<AppDevOverlay state={{nextId:1, ...}} globalError={[...]}> <AppDevOverlay state={{nextId:1, ...}} dispatch={function bound dispatchReducerAction} globalError={[...]}>
<AppDevOverlayErrorBoundary globalError={[...]} onError={function}> <AppDevOverlayErrorBoundary globalError={[...]} onError={function}>
<ReplaySsrOnlyErrors> <ReplaySsrOnlyErrors>
<DevRootHTTPAccessFallbackBoundary> <DevRootHTTPAccessFallbackBoundary>
......
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