| 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} from 'react'; |
| 12 | import {TreeDispatcherContext} from './TreeContext'; |
| 13 | import Button from '../Button'; |
| 14 | import ElementBadges from './ElementBadges'; |
| 15 | import {useHighlightHostInstance} from '../hooks'; |
| 16 | import {logEvent} from 'react-devtools-shared/src/Logger'; |
| 17 | |
| 18 | import styles from './OwnerView.css'; |
| 19 | |
| 20 | type OwnerViewProps = { |
| 21 | displayName: string, |
| 22 | hocDisplayNames: Array<string> | null, |
| 23 | environmentName: string | null, |
| 24 | compiledWithForget: boolean, |
| 25 | id: number, |
| 26 | isInStore: boolean, |
| 27 | }; |
| 28 | |
| 29 | export default function OwnerView({ |
| 30 | displayName, |
| 31 | environmentName, |
| 32 | hocDisplayNames, |
| 33 | compiledWithForget, |
| 34 | id, |
| 35 | isInStore, |
| 36 | }: OwnerViewProps): React.Node { |
| 37 | const dispatch = useContext(TreeDispatcherContext); |
| 38 | const {highlightHostInstance, clearHighlightHostInstance} = |
| 39 | useHighlightHostInstance(); |
| 40 | |
| 41 | const handleClick = useCallback(() => { |
| 42 | logEvent({ |
| 43 | event_name: 'select-element', |
| 44 | metadata: {source: 'owner-view'}, |
| 45 | }); |
| 46 | dispatch({ |
| 47 | type: 'SELECT_ELEMENT_BY_ID', |
| 48 | payload: id, |
| 49 | }); |
| 50 | }, [dispatch, id]); |
| 51 | |
| 52 | return ( |
| 53 | <Button |
| 54 | key={id} |
| 55 | className={styles.OwnerButton} |
| 56 | disabled={!isInStore} |
| 57 | onClick={handleClick} |
| 58 | onMouseEnter={() => highlightHostInstance(id)} |
| 59 | onMouseLeave={clearHighlightHostInstance}> |
| 60 | <span className={styles.OwnerContent}> |
| 61 | <span |
| 62 | className={`${styles.Owner} ${isInStore ? '' : styles.NotInStore}`} |
| 63 | title={displayName} |
| 64 | data-testname="OwnerView"> |
| 65 | {'<' + displayName + '>'} |
| 66 | </span> |
| 67 | |
| 68 | <ElementBadges |
| 69 | hocDisplayNames={hocDisplayNames} |
| 70 | compiledWithForget={compiledWithForget} |
| 71 | environmentName={environmentName} |
| 72 | /> |
| 73 | </span> |
| 74 | </Button> |
| 75 | ); |
| 76 | } |