| 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 | import * as React from 'react'; |
| 11 | import {createRoot} from 'react-dom/client'; |
| 12 | import Bridge from 'react-devtools-shared/src/bridge'; |
| 13 | import Store from 'react-devtools-shared/src/devtools/store'; |
| 14 | import {subscribeToStoreErrors} from 'react-devtools-shared/src/devtools/storeErrorLogger'; |
| 15 | import DevTools from 'react-devtools-shared/src/devtools/views/DevTools'; |
| 16 | |
| 17 | import type { |
| 18 | BrowserTheme, |
| 19 | Wall, |
| 20 | } from 'react-devtools-shared/src/frontend/types'; |
| 21 | import type {FrontendBridge} from 'react-devtools-shared/src/bridge'; |
| 22 | import type { |
| 23 | CanViewElementSource, |
| 24 | TabID, |
| 25 | ViewAttributeSource, |
| 26 | ViewElementSource, |
| 27 | } from 'react-devtools-shared/src/devtools/views/DevTools'; |
| 28 | import type {FetchFileWithCaching} from 'react-devtools-shared/src/devtools/views/Components/FetchFileWithCachingContext'; |
| 29 | import type {Config} from 'react-devtools-shared/src/devtools/store'; |
| 30 | |
| 31 | export function createBridge(wall?: Wall): FrontendBridge { |
| 32 | if (wall != null) { |
| 33 | return new Bridge(wall); |
| 34 | } |
| 35 | |
| 36 | return new Bridge({listen: () => () => {}, send: () => {}}); |
| 37 | } |
| 38 | |
| 39 | export function createStore(bridge: FrontendBridge, config?: Config): Store { |
| 40 | const store = new Store(bridge, { |
| 41 | checkBridgeProtocolCompatibility: true, |
| 42 | supportsTraceUpdates: true, |
| 43 | supportsClickToInspect: true, |
| 44 | ...config, |
| 45 | }); |
| 46 | subscribeToStoreErrors(store, bridge); |
| 47 | return store; |
| 48 | } |
| 49 | |
| 50 | type InitializationOptions = { |
| 51 | bridge: FrontendBridge, |
| 52 | store: Store, |
| 53 | theme?: BrowserTheme, |
| 54 | viewAttributeSourceFunction?: ViewAttributeSource, |
| 55 | viewElementSourceFunction?: ViewElementSource, |
| 56 | canViewElementSourceFunction?: CanViewElementSource, |
| 57 | fetchFileWithCaching?: FetchFileWithCaching, |
| 58 | }; |
| 59 | |
| 60 | function initializeTab( |
| 61 | tab: TabID, |
| 62 | contentWindow: Element | Document, |
| 63 | options: InitializationOptions, |
| 64 | ) { |
| 65 | const { |
| 66 | bridge, |
| 67 | store, |
| 68 | theme = 'light', |
| 69 | viewAttributeSourceFunction, |
| 70 | viewElementSourceFunction, |
| 71 | canViewElementSourceFunction, |
| 72 | fetchFileWithCaching, |
| 73 | } = options; |
| 74 | const root = createRoot(contentWindow); |
| 75 | |
| 76 | root.render( |
| 77 | <DevTools |
| 78 | bridge={bridge} |
| 79 | browserTheme={theme} |
| 80 | store={store} |
| 81 | showTabBar={false} |
| 82 | overrideTab={tab} |
| 83 | warnIfLegacyBackendDetected={true} |
| 84 | enabledInspectedElementContextMenu={true} |
| 85 | viewAttributeSourceFunction={viewAttributeSourceFunction} |
| 86 | viewElementSourceFunction={viewElementSourceFunction} |
| 87 | canViewElementSourceFunction={canViewElementSourceFunction} |
| 88 | fetchFileWithCaching={fetchFileWithCaching} |
| 89 | />, |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | export function initializeComponents( |
| 94 | contentWindow: Element | Document, |
| 95 | options: InitializationOptions, |
| 96 | ): void { |
| 97 | initializeTab('components', contentWindow, options); |
| 98 | } |
| 99 | |
| 100 | export function initializeProfiler( |
| 101 | contentWindow: Element | Document, |
| 102 | options: InitializationOptions, |
| 103 | ): void { |
| 104 | initializeTab('profiler', contentWindow, options); |
| 105 | } |