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, useContext, useMemo} from 'react';
    
  12. import {StoreContext} from 'react-devtools-shared/src/devtools/views/context';
    
  13. import {useSubscription} from 'react-devtools-shared/src/devtools/views/hooks';
    
  14. import {NativeStyleContext} from './context';
    
  15. import LayoutViewer from './LayoutViewer';
    
  16. import StyleEditor from './StyleEditor';
    
  17. import {TreeStateContext} from '../TreeContext';
    
  18. 
    
  19. type Props = {};
    
  20. 
    
  21. export default function NativeStyleEditorWrapper(_: Props): React.Node {
    
  22.   const store = useContext(StoreContext);
    
  23. 
    
  24.   const subscription = useMemo(
    
  25.     () => ({
    
  26.       getCurrentValue: () => store.supportsNativeStyleEditor,
    
  27.       subscribe: (callback: Function) => {
    
  28.         store.addListener('supportsNativeStyleEditor', callback);
    
  29.         return () => {
    
  30.           store.removeListener('supportsNativeStyleEditor', callback);
    
  31.         };
    
  32.       },
    
  33.     }),
    
  34.     [store],
    
  35.   );
    
  36.   const supportsNativeStyleEditor = useSubscription<boolean>(subscription);
    
  37. 
    
  38.   if (!supportsNativeStyleEditor) {
    
  39.     return null;
    
  40.   }
    
  41. 
    
  42.   return <NativeStyleEditor />;
    
  43. }
    
  44. 
    
  45. function NativeStyleEditor(_: Props) {
    
  46.   const {getStyleAndLayout} = useContext(NativeStyleContext);
    
  47. 
    
  48.   const {inspectedElementID} = useContext(TreeStateContext);
    
  49.   if (inspectedElementID === null) {
    
  50.     return null;
    
  51.   }
    
  52. 
    
  53.   const maybeStyleAndLayout = getStyleAndLayout(inspectedElementID);
    
  54.   if (maybeStyleAndLayout === null) {
    
  55.     return null;
    
  56.   }
    
  57. 
    
  58.   const {layout, style} = maybeStyleAndLayout;
    
  59. 
    
  60.   return (
    
  61.     <Fragment>
    
  62.       {layout !== null && (
    
  63.         <LayoutViewer id={inspectedElementID} layout={layout} />
    
  64.       )}
    
  65.       {style !== null && (
    
  66.         <StyleEditor
    
  67.           id={inspectedElementID}
    
  68.           style={style !== null ? style : {}}
    
  69.         />
    
  70.       )}
    
  71.     </Fragment>
    
  72.   );
    
  73. }