1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. /**
    
  11.  * WARNING:
    
  12.  * This file contains types that are designed for React DevTools UI and how it interacts with the backend.
    
  13.  * They might be used in different versions of DevTools backends.
    
  14.  * Be mindful of backwards compatibility when making changes.
    
  15.  */
    
  16. 
    
  17. import type {Source} from 'shared/ReactElementType';
    
  18. import type {
    
  19.   Dehydrated,
    
  20.   Unserializable,
    
  21. } from 'react-devtools-shared/src/hydration';
    
  22. 
    
  23. export type BrowserTheme = 'dark' | 'light';
    
  24. 
    
  25. export type Wall = {
    
  26.   // `listen` returns the "unlisten" function.
    
  27.   listen: (fn: Function) => Function,
    
  28.   send: (event: string, payload: any, transferable?: Array<any>) => void,
    
  29. };
    
  30. 
    
  31. // WARNING
    
  32. // The values below are referenced by ComponentFilters (which are saved via localStorage).
    
  33. // Do not change them or it will break previously saved user customizations.
    
  34. // If new element types are added, use new numbers rather than re-ordering existing ones.
    
  35. //
    
  36. // Changing these types is also a backwards breaking change for the standalone shell,
    
  37. // since the frontend and backend must share the same values-
    
  38. // and the backend is embedded in certain environments (like React Native).
    
  39. export const ElementTypeClass = 1;
    
  40. export const ElementTypeContext = 2;
    
  41. export const ElementTypeFunction = 5;
    
  42. export const ElementTypeForwardRef = 6;
    
  43. export const ElementTypeHostComponent = 7;
    
  44. export const ElementTypeMemo = 8;
    
  45. export const ElementTypeOtherOrUnknown = 9;
    
  46. export const ElementTypeProfiler = 10;
    
  47. export const ElementTypeRoot = 11;
    
  48. export const ElementTypeSuspense = 12;
    
  49. export const ElementTypeSuspenseList = 13;
    
  50. export const ElementTypeTracingMarker = 14;
    
  51. 
    
  52. // Different types of elements displayed in the Elements tree.
    
  53. // These types may be used to visually distinguish types,
    
  54. // or to enable/disable certain functionality.
    
  55. export type ElementType = 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14;
    
  56. 
    
  57. // WARNING
    
  58. // The values below are referenced by ComponentFilters (which are saved via localStorage).
    
  59. // Do not change them or it will break previously saved user customizations.
    
  60. // If new filter types are added, use new numbers rather than re-ordering existing ones.
    
  61. export const ComponentFilterElementType = 1;
    
  62. export const ComponentFilterDisplayName = 2;
    
  63. export const ComponentFilterLocation = 3;
    
  64. export const ComponentFilterHOC = 4;
    
  65. 
    
  66. export type ComponentFilterType = 1 | 2 | 3 | 4;
    
  67. 
    
  68. // Hide all elements of types in this Set.
    
  69. // We hide host components only by default.
    
  70. export type ElementTypeComponentFilter = {
    
  71.   isEnabled: boolean,
    
  72.   type: 1,
    
  73.   value: ElementType,
    
  74. };
    
  75. 
    
  76. // Hide all elements with displayNames or paths matching one or more of the RegExps in this Set.
    
  77. // Path filters are only used when elements include debug source location.
    
  78. export type RegExpComponentFilter = {
    
  79.   isEnabled: boolean,
    
  80.   isValid: boolean,
    
  81.   type: 2 | 3,
    
  82.   value: string,
    
  83. };
    
  84. 
    
  85. export type BooleanComponentFilter = {
    
  86.   isEnabled: boolean,
    
  87.   isValid: boolean,
    
  88.   type: 4,
    
  89. };
    
  90. 
    
  91. export type ComponentFilter =
    
  92.   | BooleanComponentFilter
    
  93.   | ElementTypeComponentFilter
    
  94.   | RegExpComponentFilter;
    
  95. 
    
  96. export type HookName = string | null;
    
  97. // Map of hook source ("<filename>:<line-number>:<column-number>") to name.
    
  98. // Hook source is used instead of the hook itself because the latter is not stable between element inspections.
    
  99. // We use a Map rather than an Array because of nested hooks and traversal ordering.
    
  100. export type HookSourceLocationKey = string;
    
  101. export type HookNames = Map<HookSourceLocationKey, HookName>;
    
  102. 
    
  103. export type LRUCache<K, V> = {
    
  104.   del: (key: K) => void,
    
  105.   get: (key: K) => V,
    
  106.   has: (key: K) => boolean,
    
  107.   reset: () => void,
    
  108.   set: (key: K, value: V) => void,
    
  109. };
    
  110. 
    
  111. export type StyleXPlugin = {
    
  112.   sources: Array<string>,
    
  113.   resolvedStyles: Object,
    
  114. };
    
  115. 
    
  116. export type Plugins = {
    
  117.   stylex: StyleXPlugin | null,
    
  118. };
    
  119. 
    
  120. export const StrictMode = 1;
    
  121. 
    
  122. // Each element on the frontend corresponds to a Fiber on the backend.
    
  123. // Some of its information (e.g. id, type, displayName) come from the backend.
    
  124. // Other bits (e.g. weight and depth) are computed on the frontend for windowing and display purposes.
    
  125. // Elements are updated on a push basis– meaning the backend pushes updates to the frontend when needed.
    
  126. export type Element = {
    
  127.   id: number,
    
  128.   parentID: number,
    
  129.   children: Array<number>,
    
  130.   type: ElementType,
    
  131.   displayName: string | null,
    
  132.   key: number | string | null,
    
  133. 
    
  134.   hocDisplayNames: null | Array<string>,
    
  135. 
    
  136.   // Should the elements children be visible in the tree?
    
  137.   isCollapsed: boolean,
    
  138. 
    
  139.   // Owner (if available)
    
  140.   ownerID: number,
    
  141. 
    
  142.   // How many levels deep within the tree is this element?
    
  143.   // This determines how much indentation (left padding) should be used in the Elements tree.
    
  144.   depth: number,
    
  145. 
    
  146.   // How many nodes (including itself) are below this Element within the tree.
    
  147.   // This property is used to quickly determine the total number of Elements,
    
  148.   // and the Element at any given index (for windowing purposes).
    
  149.   weight: number,
    
  150. 
    
  151.   // This element is not in a StrictMode compliant subtree.
    
  152.   // Only true for React versions supporting StrictMode.
    
  153.   isStrictModeNonCompliant: boolean,
    
  154. };
    
  155. 
    
  156. export type SerializedElement = {
    
  157.   displayName: string | null,
    
  158.   id: number,
    
  159.   key: number | string | null,
    
  160.   hocDisplayNames: Array<string> | null,
    
  161.   type: ElementType,
    
  162. };
    
  163. 
    
  164. export type OwnersList = {
    
  165.   id: number,
    
  166.   owners: Array<SerializedElement> | null,
    
  167. };
    
  168. 
    
  169. export type InspectedElementResponseType =
    
  170.   | 'error'
    
  171.   | 'full-data'
    
  172.   | 'hydrated-path'
    
  173.   | 'no-change'
    
  174.   | 'not-found';
    
  175. 
    
  176. export type InspectedElementPath = Array<string | number>;
    
  177. 
    
  178. export type InspectedElement = {
    
  179.   id: number,
    
  180. 
    
  181.   // Does the current renderer support editable hooks and function props?
    
  182.   canEditHooks: boolean,
    
  183.   canEditFunctionProps: boolean,
    
  184. 
    
  185.   // Does the current renderer support advanced editing interface?
    
  186.   canEditHooksAndDeletePaths: boolean,
    
  187.   canEditHooksAndRenamePaths: boolean,
    
  188.   canEditFunctionPropsDeletePaths: boolean,
    
  189.   canEditFunctionPropsRenamePaths: boolean,
    
  190. 
    
  191.   // Is this Error, and can its value be overridden now?
    
  192.   isErrored: boolean,
    
  193.   canToggleError: boolean,
    
  194.   targetErrorBoundaryID: ?number,
    
  195. 
    
  196.   // Is this Suspense, and can its value be overridden now?
    
  197.   canToggleSuspense: boolean,
    
  198. 
    
  199.   // Can view component source location.
    
  200.   canViewSource: boolean,
    
  201. 
    
  202.   // Does the component have legacy context attached to it.
    
  203.   hasLegacyContext: boolean,
    
  204. 
    
  205.   // Inspectable properties.
    
  206.   context: Object | null,
    
  207.   hooks: Object | null,
    
  208.   props: Object | null,
    
  209.   state: Object | null,
    
  210.   key: number | string | null,
    
  211.   errors: Array<[string, number]>,
    
  212.   warnings: Array<[string, number]>,
    
  213. 
    
  214.   // List of owners
    
  215.   owners: Array<SerializedElement> | null,
    
  216. 
    
  217.   // Location of component in source code.
    
  218.   source: Source | null,
    
  219. 
    
  220.   type: ElementType,
    
  221. 
    
  222.   // Meta information about the root this element belongs to.
    
  223.   rootType: string | null,
    
  224. 
    
  225.   // Meta information about the renderer that created this element.
    
  226.   rendererPackageName: string | null,
    
  227.   rendererVersion: string | null,
    
  228. 
    
  229.   // UI plugins/visualizations for the inspected element.
    
  230.   plugins: Plugins,
    
  231. };
    
  232. 
    
  233. // TODO: Add profiling type
    
  234. 
    
  235. type Data =
    
  236.   | string
    
  237.   | Dehydrated
    
  238.   | Unserializable
    
  239.   | Array<Dehydrated>
    
  240.   | Array<Unserializable>
    
  241.   | {[string]: Data};
    
  242. 
    
  243. export type DehydratedData = {
    
  244.   cleaned: Array<Array<string | number>>,
    
  245.   data: Data,
    
  246.   unserializable: Array<Array<string | number>>,
    
  247. };