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

[dev-overlay] Remove unused code (#80279)

Best reviewed without whitespace changes.
parent f63088d2
type NullableText = string | null | undefined
// https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js used as a reference
const htmlTagsWarnings = new Set([
'Warning: In HTML, %s cannot be a child of <%s>.%s\nThis will cause a hydration error.%s',
'Warning: In HTML, %s cannot be a descendant of <%s>.\nThis will cause a hydration error.%s',
'Warning: In HTML, text nodes cannot be a child of <%s>.\nThis will cause a hydration error.',
"Warning: In HTML, whitespace text nodes cannot be a child of <%s>. Make sure you don't have any extra whitespace between tags on each line of your source code.\nThis will cause a hydration error.",
'Warning: Expected server HTML to contain a matching <%s> in <%s>.%s',
'Warning: Did not expect server HTML to contain a <%s> in <%s>.%s',
])
const textAndTagsMismatchWarnings = new Set([
'Warning: Expected server HTML to contain a matching text node for "%s" in <%s>.%s',
'Warning: Did not expect server HTML to contain the text node "%s" in <%s>.%s',
])
export const getHydrationWarningType = (
message: NullableText
): 'tag' | 'text' | 'text-in-tag' => {
if (typeof message !== 'string') {
// TODO: Doesn't make sense to treat no message as a hydration error message.
// We should bail out somewhere earlier.
return 'text'
}
const normalizedMessage = message.startsWith('Warning: ')
? message
: `Warning: ${message}`
if (isHtmlTagsWarning(normalizedMessage)) return 'tag'
if (isTextInTagsMismatchWarning(normalizedMessage)) return 'text-in-tag'
return 'text'
}
const isHtmlTagsWarning = (message: string) => htmlTagsWarnings.has(message)
const isTextInTagsMismatchWarning = (msg: string) =>
textAndTagsMismatchWarnings.has(msg)
import type { BusEvent } from '../shared'
export type BusEventHandler = (ev: BusEvent) => void
let handlers: Set<BusEventHandler> = new Set()
let queue: BusEvent[] = []
function drain() {
// Draining should never happen synchronously in case multiple handlers are
// registered.
setTimeout(function () {
while (
// Until we are out of events:
Boolean(queue.length) &&
// Or, if all handlers removed themselves as a result of handling the
// event(s)
Boolean(handlers.size)
) {
const ev = queue.shift()!
handlers.forEach((handler) => handler(ev))
}
}, 1)
}
export function emit(ev: BusEvent): void {
queue.push(Object.freeze({ ...ev }))
drain()
}
export function on(fn: BusEventHandler): boolean {
if (handlers.has(fn)) {
return false
}
handlers.add(fn)
drain()
return true
}
export function off(fn: BusEventHandler): boolean {
if (handlers.has(fn)) {
handlers.delete(fn)
return true
}
return false
}
...@@ -30,7 +30,7 @@ export interface OverlayState { ...@@ -30,7 +30,7 @@ export interface OverlayState {
routerType: 'pages' | 'app' routerType: 'pages' | 'app'
isErrorOverlayOpen: boolean isErrorOverlayOpen: boolean
} }
export type OverlayDispatch = React.Dispatch<BusEvent> export type OverlayDispatch = React.Dispatch<DispatcherEvent>
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'
...@@ -121,7 +121,7 @@ export interface RenderingIndicatorHideAction { ...@@ -121,7 +121,7 @@ export interface RenderingIndicatorHideAction {
type: typeof ACTION_RENDERING_INDICATOR_HIDE type: typeof ACTION_RENDERING_INDICATOR_HIDE
} }
export type BusEvent = export type DispatcherEvent =
| BuildOkAction | BuildOkAction
| BuildErrorAction | BuildErrorAction
| BeforeFastRefreshAction | BeforeFastRefreshAction
...@@ -240,7 +240,8 @@ export function useErrorOverlayReducer( ...@@ -240,7 +240,8 @@ export function useErrorOverlayReducer(
return events return events
} }
return useReducer((state: OverlayState, action: BusEvent): OverlayState => { return useReducer(
(state: OverlayState, action: DispatcherEvent): OverlayState => {
switch (action.type) { switch (action.type) {
case ACTION_DEBUG_INFO: { case ACTION_DEBUG_INFO: {
return { ...state, debugInfo: action.debugInfo } return { ...state, debugInfo: action.debugInfo }
...@@ -342,5 +343,7 @@ export function useErrorOverlayReducer( ...@@ -342,5 +343,7 @@ export function useErrorOverlayReducer(
return state return state
} }
} }
}, getInitialState(routerType)) },
getInitialState(routerType)
)
} }
import type { Meta, StoryObj } from '@storybook/react' import type { Meta, StoryObj } from '@storybook/react'
import type { BusEvent, OverlayState } from '../shared' import type { DispatcherEvent, OverlayState } from '../shared'
// @ts-expect-error // @ts-expect-error
import imgApp from './app.png' import imgApp from './app.png'
...@@ -97,7 +97,8 @@ const initialState: OverlayState = { ...@@ -97,7 +97,8 @@ const initialState: OverlayState = {
} }
function useOverlayReducer() { function useOverlayReducer() {
return useReducer<OverlayState, [BusEvent]>((state, action): OverlayState => { return useReducer<OverlayState, [DispatcherEvent]>(
(state, action): OverlayState => {
switch (action.type) { switch (action.type) {
case ACTION_ERROR_OVERLAY_CLOSE: { case ACTION_ERROR_OVERLAY_CLOSE: {
return { ...state, isErrorOverlayOpen: false } return { ...state, isErrorOverlayOpen: false }
...@@ -113,7 +114,9 @@ function useOverlayReducer() { ...@@ -113,7 +114,9 @@ function useOverlayReducer() {
} }
} }
return state return state
}, initialState) },
initialState
)
} }
function getNoSquashedHydrationErrorDetails() { function getNoSquashedHydrationErrorDetails() {
......
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