main
js 93 lines 2.44 KB
Raw
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 Button from 'react-devtools-shared/src/devtools/views/Button';
13 import ButtonIcon from 'react-devtools-shared/src/devtools/views/ButtonIcon';
14 import ButtonLabel from 'react-devtools-shared/src/devtools/views/ButtonLabel';
15
16 import type {SourceSelection} from './EditorPane';
17 import type {ReactFunctionLocation} from 'shared/ReactTypes';
18
19 import {checkConditions} from './utils';
20
21 type Props = {
22 editorURL: string,
23 source: ?SourceSelection,
24 className?: string,
25 };
26
27 function ActualOpenInEditorButton({
28 editorURL,
29 source,
30 className,
31 }: Props): React.Node {
32 let disable;
33 if (source == null) {
34 disable = true;
35 } else {
36 const staleLocation: ReactFunctionLocation = [
37 '',
38 source.url,
39 // This is not live but we just use any line/column to validate whether this can be opened.
40 // We'll call checkConditions again when we click it to get the latest line number.
41 source.selectionRef.line,
42 source.selectionRef.column,
43 ];
44 disable = checkConditions(editorURL, staleLocation).shouldDisableButton;
45 }
46 return (
47 <Button
48 disabled={disable}
49 className={className}
50 onClick={() => {
51 if (source == null) {
52 return;
53 }
54 const latestLocation: ReactFunctionLocation = [
55 '',
56 source.url,
57 // These might have changed since we last read it.
58 source.selectionRef.line,
59 source.selectionRef.column,
60 ];
61 const {url, shouldDisableButton} = checkConditions(
62 editorURL,
63 latestLocation,
64 );
65 if (!shouldDisableButton) {
66 window.open(url);
67 }
68 }}>
69 <ButtonIcon type="editor" />
70 <ButtonLabel>Open in editor</ButtonLabel>
71 </Button>
72 );
73 }
74
75 function OpenInEditorButton({editorURL, source, className}: Props): React.Node {
76 return (
77 <React.Suspense
78 fallback={
79 <Button disabled={true} className={className}>
80 <ButtonIcon type="editor" />
81 <ButtonLabel>Loading source maps...</ButtonLabel>
82 </Button>
83 }>
84 <ActualOpenInEditorButton
85 editorURL={editorURL}
86 source={source}
87 className={className}
88 />
89 </React.Suspense>
90 );
91 }
92
93 export default OpenInEditorButton;