| 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 {useCallback, useContext, useEffect, useState} from 'react'; |
| 12 | import {BridgeContext} from '../context'; |
| 13 | import Toggle from '../Toggle'; |
| 14 | import ButtonIcon from '../ButtonIcon'; |
| 15 | import {logEvent} from 'react-devtools-shared/src/Logger'; |
| 16 | |
| 17 | export default function InspectHostNodesToggle({ |
| 18 | onlySuspenseNodes, |
| 19 | }: { |
| 20 | onlySuspenseNodes?: boolean, |
| 21 | }): React.Node { |
| 22 | const [isInspecting, setIsInspecting] = useState(false); |
| 23 | const bridge = useContext(BridgeContext); |
| 24 | |
| 25 | const handleChange = useCallback( |
| 26 | (isChecked: boolean) => { |
| 27 | setIsInspecting(isChecked); |
| 28 | |
| 29 | if (isChecked) { |
| 30 | logEvent({event_name: 'inspect-element-button-clicked'}); |
| 31 | bridge.send('startInspectingHost', !!onlySuspenseNodes); |
| 32 | } else { |
| 33 | bridge.send('stopInspectingHost'); |
| 34 | } |
| 35 | }, |
| 36 | [bridge], |
| 37 | ); |
| 38 | |
| 39 | useEffect(() => { |
| 40 | const onStopInspectingHost = () => setIsInspecting(false); |
| 41 | bridge.addListener('stopInspectingHost', onStopInspectingHost); |
| 42 | return () => |
| 43 | bridge.removeListener('stopInspectingHost', onStopInspectingHost); |
| 44 | }, [bridge]); |
| 45 | |
| 46 | return ( |
| 47 | <Toggle |
| 48 | onChange={handleChange} |
| 49 | isChecked={isInspecting} |
| 50 | title="Select an element in the page to inspect it"> |
| 51 | <ButtonIcon type="search" /> |
| 52 | </Toggle> |
| 53 | ); |
| 54 | } |