From 18dff7990a81b3ff1e716dd2cf113c6027bdcc06 Mon Sep 17 00:00:00 2001 From: Mengdi Chen Date: Mon, 7 Nov 2022 15:23:17 -0500 Subject: [PATCH] [DevTools] add support for HostSingleton & HostResource (#25616) ## Summary This is to support two new reconciler work tags `HostSingleton` and `HostResource` introduced in PRs #25243 #25426. The behavior is described below. I also renamed an option in components settings from an internal concept "host" to more understood "dom nodes" ## How did you test this change? Tested on the latest Vercel playground app https://github.com/vercel/app-playground/ Before the change, devtools cannot show correct display name for these new elements. Also, some unnecessary internal details are exposed to users. image After the change, the display names are correctly shown and the "state" would always be hidden in the detail view. image These elements will also be hidden just like other native dom elements (e.g. `
`) image --- .../src/backend/renderer.js | 18 ++++ .../src/backend/types.js | 2 + .../Components/InspectedElementStateTree.js | 82 ++++++++++--------- .../views/Settings/ComponentsSettings.js | 2 +- 4 files changed, 64 insertions(+), 40 deletions(-) diff --git a/packages/react-devtools-shared/src/backend/renderer.js b/packages/react-devtools-shared/src/backend/renderer.js index d08cd8a5ddede..cb3a8f2f407c7 100644 --- a/packages/react-devtools-shared/src/backend/renderer.js +++ b/packages/react-devtools-shared/src/backend/renderer.js @@ -237,6 +237,8 @@ export function getInternalReactConstants( HostComponent: 5, HostPortal: 4, HostRoot: 3, + HostResource: 26, // In reality, 18.2+. But doesn't hurt to include it here + HostSingleton: 27, // Same as above HostText: 6, IncompleteClassComponent: 17, IndeterminateComponent: 2, @@ -269,6 +271,8 @@ export function getInternalReactConstants( HostComponent: 5, HostPortal: 4, HostRoot: 3, + HostResource: -1, // Doesn't exist yet + HostSingleton: -1, // Doesn't exist yet HostText: 6, IncompleteClassComponent: 17, IndeterminateComponent: 2, @@ -300,6 +304,8 @@ export function getInternalReactConstants( HostComponent: 5, HostPortal: 4, HostRoot: 3, + HostResource: -1, // Doesn't exist yet + HostSingleton: -1, // Doesn't exist yet HostText: 6, IncompleteClassComponent: 17, IndeterminateComponent: 2, @@ -331,6 +337,8 @@ export function getInternalReactConstants( HostComponent: 7, HostPortal: 6, HostRoot: 5, + HostResource: -1, // Doesn't exist yet + HostSingleton: -1, // Doesn't exist yet HostText: 8, IncompleteClassComponent: -1, // Doesn't exist yet IndeterminateComponent: 4, @@ -362,6 +370,8 @@ export function getInternalReactConstants( HostComponent: 5, HostPortal: 4, HostRoot: 3, + HostResource: -1, // Doesn't exist yet + HostSingleton: -1, // Doesn't exist yet HostText: 6, IncompleteClassComponent: -1, // Doesn't exist yet IndeterminateComponent: 0, @@ -401,6 +411,8 @@ export function getInternalReactConstants( IndeterminateComponent, ForwardRef, HostRoot, + HostResource, + HostSingleton, HostComponent, HostPortal, HostText, @@ -466,6 +478,8 @@ export function getInternalReactConstants( } return null; case HostComponent: + case HostSingleton: + case HostResource: return type; case HostPortal: case HostText: @@ -600,6 +614,8 @@ export function attach( Fragment, FunctionComponent, HostRoot, + HostResource, + HostSingleton, HostPortal, HostComponent, HostText, @@ -1044,6 +1060,8 @@ export function attach( case HostRoot: return ElementTypeRoot; case HostComponent: + case HostResource: + case HostSingleton: return ElementTypeHostComponent; case HostPortal: case HostText: diff --git a/packages/react-devtools-shared/src/backend/types.js b/packages/react-devtools-shared/src/backend/types.js index 0392768613764..ef03c5717a505 100644 --- a/packages/react-devtools-shared/src/backend/types.js +++ b/packages/react-devtools-shared/src/backend/types.js @@ -40,6 +40,8 @@ export type WorkTagMap = { HostComponent: WorkTag, HostPortal: WorkTag, HostRoot: WorkTag, + HostResource: WorkTag, + HostSingleton: WorkTag, HostText: WorkTag, IncompleteClassComponent: WorkTag, IndeterminateComponent: WorkTag, diff --git a/packages/react-devtools-shared/src/devtools/views/Components/InspectedElementStateTree.js b/packages/react-devtools-shared/src/devtools/views/Components/InspectedElementStateTree.js index 6557fddbef985..f3a83da5ea628 100644 --- a/packages/react-devtools-shared/src/devtools/views/Components/InspectedElementStateTree.js +++ b/packages/react-devtools-shared/src/devtools/views/Components/InspectedElementStateTree.js @@ -9,6 +9,7 @@ import {copy} from 'clipboard-js'; import * as React from 'react'; +import {ElementTypeHostComponent} from 'react-devtools-shared/src/types'; import Button from '../Button'; import ButtonIcon from '../ButtonIcon'; import KeyValue from './KeyValue'; @@ -33,52 +34,55 @@ export default function InspectedElementStateTree({ inspectedElement, store, }: Props): React.Node { - const {state} = inspectedElement; + const {state, type} = inspectedElement; + + // HostSingleton and HostResource may have state that we don't want to expose to users + const isHostComponent = type === ElementTypeHostComponent; const entries = state != null ? Object.entries(state) : null; + const isEmpty = entries === null || entries.length === 0; + + if (isEmpty || isHostComponent) { + return null; + } + if (entries !== null) { entries.sort(alphaSortEntries); } - const isEmpty = entries === null || entries.length === 0; - const handleCopy = () => copy(serializeDataForCopy(((state: any): Object))); - if (isEmpty) { - return null; - } else { - return ( -
-
-
state
- {!isEmpty && ( - - )} -
- {isEmpty &&
None
} - {!isEmpty && - (entries: any).map(([name, value]) => ( -