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 {copy} from 'clipboard-js';
    
  11. import * as React from 'react';
    
  12. import {ElementTypeHostComponent} from 'react-devtools-shared/src/frontend/types';
    
  13. import Button from '../Button';
    
  14. import ButtonIcon from '../ButtonIcon';
    
  15. import KeyValue from './KeyValue';
    
  16. import {alphaSortEntries, serializeDataForCopy} from '../utils';
    
  17. import Store from '../../store';
    
  18. import styles from './InspectedElementSharedStyles.css';
    
  19. 
    
  20. import type {InspectedElement} from 'react-devtools-shared/src/frontend/types';
    
  21. import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
    
  22. import type {Element} from 'react-devtools-shared/src/frontend/types';
    
  23. 
    
  24. type Props = {
    
  25.   bridge: FrontendBridge,
    
  26.   element: Element,
    
  27.   inspectedElement: InspectedElement,
    
  28.   store: Store,
    
  29. };
    
  30. 
    
  31. export default function InspectedElementStateTree({
    
  32.   bridge,
    
  33.   element,
    
  34.   inspectedElement,
    
  35.   store,
    
  36. }: Props): React.Node {
    
  37.   const {state, type} = inspectedElement;
    
  38. 
    
  39.   // HostSingleton and HostHoistable may have state that we don't want to expose to users
    
  40.   const isHostComponent = type === ElementTypeHostComponent;
    
  41. 
    
  42.   const entries = state != null ? Object.entries(state) : null;
    
  43.   const isEmpty = entries === null || entries.length === 0;
    
  44. 
    
  45.   if (isEmpty || isHostComponent) {
    
  46.     return null;
    
  47.   }
    
  48. 
    
  49.   if (entries !== null) {
    
  50.     entries.sort(alphaSortEntries);
    
  51.   }
    
  52. 
    
  53.   const handleCopy = () => copy(serializeDataForCopy(((state: any): Object)));
    
  54. 
    
  55.   return (
    
  56.     <div className={styles.InspectedElementTree}>
    
  57.       <div className={styles.HeaderRow}>
    
  58.         <div className={styles.Header}>state</div>
    
  59.         {!isEmpty && (
    
  60.           <Button onClick={handleCopy} title="Copy to clipboard">
    
  61.             <ButtonIcon type="copy" />
    
  62.           </Button>
    
  63.         )}
    
  64.       </div>
    
  65.       {isEmpty && <div className={styles.Empty}>None</div>}
    
  66.       {!isEmpty &&
    
  67.         (entries: any).map(([name, value]) => (
    
  68.           <KeyValue
    
  69.             key={name}
    
  70.             alphaSort={true}
    
  71.             bridge={bridge}
    
  72.             canDeletePaths={true}
    
  73.             canEditValues={true}
    
  74.             canRenamePaths={true}
    
  75.             depth={1}
    
  76.             element={element}
    
  77.             hidden={false}
    
  78.             inspectedElement={inspectedElement}
    
  79.             name={name}
    
  80.             path={[name]}
    
  81.             pathRoot="state"
    
  82.             store={store}
    
  83.             value={value}
    
  84.           />
    
  85.         ))}
    
  86.     </div>
    
  87.   );
    
  88. }