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(): React.Node {
    
  18.   const [isInspecting, setIsInspecting] = useState(false);
    
  19.   const bridge = useContext(BridgeContext);
    
  20. 
    
  21.   const handleChange = useCallback(
    
  22.     (isChecked: boolean) => {
    
  23.       setIsInspecting(isChecked);
    
  24. 
    
  25.       if (isChecked) {
    
  26.         logEvent({event_name: 'inspect-element-button-clicked'});
    
  27.         bridge.send('startInspectingNative');
    
  28.       } else {
    
  29.         bridge.send('stopInspectingNative', false);
    
  30.       }
    
  31.     },
    
  32.     [bridge],
    
  33.   );
    
  34. 
    
  35.   useEffect(() => {
    
  36.     const onStopInspectingNative = () => setIsInspecting(false);
    
  37.     bridge.addListener('stopInspectingNative', onStopInspectingNative);
    
  38.     return () =>
    
  39.       bridge.removeListener('stopInspectingNative', onStopInspectingNative);
    
  40.   }, [bridge]);
    
  41. 
    
  42.   return (
    
  43.     <Toggle
    
  44.       onChange={handleChange}
    
  45.       isChecked={isInspecting}
    
  46.       title="Select an element in the page to inspect it">
    
  47.       <ButtonIcon type="search" />
    
  48.     </Toggle>
    
  49.   );
    
  50. }