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 {OptionsContext} from '../context';
    
  12. import EditableValue from './EditableValue';
    
  13. import Store from '../../store';
    
  14. import {ElementTypeSuspense} from 'react-devtools-shared/src/frontend/types';
    
  15. import styles from './InspectedElementSharedStyles.css';
    
  16. 
    
  17. import type {InspectedElement} from 'react-devtools-shared/src/frontend/types';
    
  18. import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
    
  19. 
    
  20. type Props = {
    
  21.   bridge: FrontendBridge,
    
  22.   inspectedElement: InspectedElement,
    
  23.   store: Store,
    
  24. };
    
  25. 
    
  26. export default function InspectedElementSuspenseToggle({
    
  27.   bridge,
    
  28.   inspectedElement,
    
  29.   store,
    
  30. }: Props): React.Node {
    
  31.   const {readOnly} = React.useContext(OptionsContext);
    
  32. 
    
  33.   const {id, state, type} = inspectedElement;
    
  34.   const canToggleSuspense = !readOnly && inspectedElement.canToggleSuspense;
    
  35. 
    
  36.   if (type !== ElementTypeSuspense) {
    
  37.     return null;
    
  38.   }
    
  39. 
    
  40.   const isSuspended = state !== null;
    
  41. 
    
  42.   const toggleSuspense = (path: any, value: boolean) => {
    
  43.     const rendererID = store.getRendererIDForElement(id);
    
  44.     if (rendererID !== null) {
    
  45.       bridge.send('overrideSuspense', {
    
  46.         id,
    
  47.         rendererID,
    
  48.         forceFallback: value,
    
  49.       });
    
  50.     }
    
  51.   };
    
  52. 
    
  53.   return (
    
  54.     <div className={styles.InspectedElementTree}>
    
  55.       <div className={styles.HeaderRow}>
    
  56.         <div className={styles.Header}>suspense</div>
    
  57.       </div>
    
  58.       <div className={styles.ToggleSuspenseRow}>
    
  59.         <span className={styles.Name}>Suspended</span>
    
  60.         {canToggleSuspense ? (
    
  61.           // key is required to keep <EditableValue> and header row toggle button in sync
    
  62.           <EditableValue
    
  63.             key={isSuspended}
    
  64.             overrideValue={toggleSuspense}
    
  65.             path={['suspense', 'Suspended']}
    
  66.             value={isSuspended}
    
  67.           />
    
  68.         ) : (
    
  69.           <span className={styles.Value}>{isSuspended ? 'true' : 'false'}</span>
    
  70.         )}
    
  71.       </div>
    
  72.     </div>
    
  73.   );
    
  74. }