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 {Fragment, useCallback, useContext} from 'react';
    
  12. import {ProfilerContext} from './ProfilerContext';
    
  13. 
    
  14. import styles from './RootSelector.css';
    
  15. 
    
  16. export default function RootSelector(_: {}): React.Node {
    
  17.   const {profilingData, rootID, setRootID} = useContext(ProfilerContext);
    
  18. 
    
  19.   const options = [];
    
  20.   if (profilingData !== null) {
    
  21.     profilingData.dataForRoots.forEach((dataForRoot, id) => {
    
  22.       options.push(
    
  23.         <option key={id} value={id}>
    
  24.           {dataForRoot.displayName}
    
  25.         </option>,
    
  26.       );
    
  27.     });
    
  28.   }
    
  29. 
    
  30.   const handleChange = useCallback(
    
  31.     ({currentTarget}: $FlowFixMe) => {
    
  32.       setRootID(parseInt(currentTarget.value, 10));
    
  33.     },
    
  34.     [setRootID],
    
  35.   );
    
  36. 
    
  37.   if (profilingData === null || profilingData.dataForRoots.size <= 1) {
    
  38.     // Don't take up visual space if there's only one root.
    
  39.     return null;
    
  40.   }
    
  41. 
    
  42.   return (
    
  43.     <Fragment>
    
  44.       <div className={styles.Spacer} />
    
  45.       <select value={rootID} onChange={handleChange}>
    
  46.         {options}
    
  47.       </select>
    
  48.     </Fragment>
    
  49.   );
    
  50. }