| 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 {useState, startTransition} from 'react'; |
| 12 | |
| 13 | import portaledContent from '../portaledContent'; |
| 14 | |
| 15 | import styles from './EditorPane.css'; |
| 16 | |
| 17 | import Button from 'react-devtools-shared/src/devtools/views/Button'; |
| 18 | import ButtonIcon from 'react-devtools-shared/src/devtools/views/ButtonIcon'; |
| 19 | |
| 20 | import OpenInEditorButton from './OpenInEditorButton'; |
| 21 | import useEditorURL from '../useEditorURL'; |
| 22 | |
| 23 | import EditorSettings from './EditorSettings'; |
| 24 | import CodeEditorByDefault from '../Settings/CodeEditorByDefault'; |
| 25 | |
| 26 | export type SourceSelection = { |
| 27 | url: string, |
| 28 | // The selection is a ref so that we don't have to rerender every keystroke. |
| 29 | selectionRef: { |
| 30 | line: number, |
| 31 | column: number, |
| 32 | }, |
| 33 | }; |
| 34 | |
| 35 | export type Props = {selectedSource: ?SourceSelection}; |
| 36 | |
| 37 | function EditorPane({selectedSource}: Props) { |
| 38 | const [showSettings, setShowSettings] = useState(false); |
| 39 | const [showLinkInfo, setShowLinkInfo] = useState(false); |
| 40 | |
| 41 | const editorURL = useEditorURL(); |
| 42 | |
| 43 | if (showLinkInfo) { |
| 44 | return ( |
| 45 | <div className={styles.EditorPane}> |
| 46 | <div className={styles.EditorToolbar}> |
| 47 | <div style={{display: 'flex', flex: '1 1 auto'}}> |
| 48 | To enable link handling in your browser's DevTools settings, look |
| 49 | for the option Extension -> Link Handling. Select "React Developer |
| 50 | Tools". |
| 51 | </div> |
| 52 | <div className={styles.VRule} /> |
| 53 | <Button |
| 54 | onClick={() => |
| 55 | startTransition(() => { |
| 56 | setShowLinkInfo(false); |
| 57 | setShowSettings(false); |
| 58 | }) |
| 59 | }> |
| 60 | <ButtonIcon type="close" /> |
| 61 | </Button> |
| 62 | </div> |
| 63 | </div> |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | let editorToolbar; |
| 68 | if (showSettings) { |
| 69 | editorToolbar = ( |
| 70 | <div className={styles.EditorToolbar}> |
| 71 | <EditorSettings /> |
| 72 | <div className={styles.VRule} /> |
| 73 | <Button onClick={() => startTransition(() => setShowSettings(false))}> |
| 74 | <ButtonIcon type="close" /> |
| 75 | </Button> |
| 76 | </div> |
| 77 | ); |
| 78 | } else { |
| 79 | editorToolbar = ( |
| 80 | <div className={styles.EditorToolbar}> |
| 81 | <OpenInEditorButton |
| 82 | className={styles.WideButton} |
| 83 | editorURL={editorURL} |
| 84 | source={selectedSource} |
| 85 | /> |
| 86 | <div className={styles.VRule} /> |
| 87 | <Button |
| 88 | onClick={() => startTransition(() => setShowSettings(true))} |
| 89 | // We don't use the title here because we don't have enough space to show it. |
| 90 | // Once we expand this pane we can add it. |
| 91 | // title="Configure code editor" |
| 92 | > |
| 93 | <ButtonIcon type="settings" /> |
| 94 | </Button> |
| 95 | </div> |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | return ( |
| 100 | <div className={styles.EditorPane}> |
| 101 | {editorToolbar} |
| 102 | <div className={styles.EditorInfo}> |
| 103 | {editorURL ? ( |
| 104 | <CodeEditorByDefault |
| 105 | onChange={alwaysOpenInEditor => { |
| 106 | if (alwaysOpenInEditor) { |
| 107 | startTransition(() => setShowLinkInfo(true)); |
| 108 | } |
| 109 | }} |
| 110 | /> |
| 111 | ) : ( |
| 112 | 'Configure an external editor to open local files.' |
| 113 | )} |
| 114 | </div> |
| 115 | </div> |
| 116 | ); |
| 117 | } |
| 118 | export default portaledContent(EditorPane) as component(); |