| 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 | |
| 12 | import ButtonIcon from '../ButtonIcon'; |
| 13 | import Button from '../Button'; |
| 14 | |
| 15 | import type {ReactFunctionLocation} from 'shared/ReactTypes'; |
| 16 | import type {SourceMappedLocation} from 'react-devtools-shared/src/symbolicateSource'; |
| 17 | |
| 18 | import useOpenResource from '../useOpenResource'; |
| 19 | |
| 20 | type Props = { |
| 21 | source: null | ReactFunctionLocation, |
| 22 | symbolicatedSourcePromise: Promise<SourceMappedLocation | null> | null, |
| 23 | }; |
| 24 | |
| 25 | function InspectedElementViewSourceButton({ |
| 26 | source, |
| 27 | symbolicatedSourcePromise, |
| 28 | }: Props): React.Node { |
| 29 | return ( |
| 30 | <React.Suspense |
| 31 | fallback={ |
| 32 | <Button disabled={true} title="Loading source maps..."> |
| 33 | <ButtonIcon type="view-source" /> |
| 34 | </Button> |
| 35 | }> |
| 36 | <ActualSourceButton |
| 37 | source={source} |
| 38 | symbolicatedSourcePromise={symbolicatedSourcePromise} |
| 39 | /> |
| 40 | </React.Suspense> |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | type ActualSourceButtonProps = { |
| 45 | source: null | ReactFunctionLocation, |
| 46 | symbolicatedSourcePromise: Promise<SourceMappedLocation | null> | null, |
| 47 | }; |
| 48 | function ActualSourceButton({ |
| 49 | source, |
| 50 | symbolicatedSourcePromise, |
| 51 | }: ActualSourceButtonProps): React.Node { |
| 52 | const symbolicatedSource = |
| 53 | symbolicatedSourcePromise == null |
| 54 | ? null |
| 55 | : React.use(symbolicatedSourcePromise); |
| 56 | |
| 57 | const [buttonIsEnabled, viewSource] = useOpenResource( |
| 58 | source, |
| 59 | symbolicatedSource == null ? null : symbolicatedSource.location, |
| 60 | ); |
| 61 | return ( |
| 62 | <Button |
| 63 | disabled={!buttonIsEnabled} |
| 64 | onClick={viewSource} |
| 65 | title="View source for this element"> |
| 66 | <ButtonIcon type="view-source" /> |
| 67 | </Button> |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | export default InspectedElementViewSourceButton; |