Skip to content

Commit b2c3049

Browse files
authored
[DevTools] Use the hard coded url instead of the local storage url for presets (and make VSCode default) (facebook#33995)
Stacked on facebook#33983. Previously, the source of truth is the url stored in local storage but that means if we change the presets then they don't take effect (e.g. facebook#33994). This PR uses the hardcoded value instead when a preset is selected. This also has the benefit that if you switch between custom and vs code in the selector, then the custom url is preserved instead of getting reset when you checkout other options. Currently the default is custom with empty string, which means that there's no code editor configured at all by default. It doesn't make a lot of sense that we have it not working by default when so many people use VS Code. So this also makes VS Code the default if there's no EDITOR_URL env specified.
1 parent 36c2bf5 commit b2c3049

File tree

6 files changed

+71
-50
lines changed

6 files changed

+71
-50
lines changed

packages/react-devtools-shared/src/devtools/views/Components/InspectedElement.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ import Toggle from '../Toggle';
1818
import {ElementTypeSuspense} from 'react-devtools-shared/src/frontend/types';
1919
import InspectedElementView from './InspectedElementView';
2020
import {InspectedElementContext} from './InspectedElementContext';
21-
import {getOpenInEditorURL, getAlwaysOpenInEditor} from '../../../utils';
22-
import {
23-
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
24-
LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR,
25-
} from '../../../constants';
21+
import {getAlwaysOpenInEditor} from '../../../utils';
22+
import {LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR} from '../../../constants';
2623
import FetchFileWithCachingContext from './FetchFileWithCachingContext';
2724
import {symbolicateSourceWithCache} from 'react-devtools-shared/src/symbolicateSource';
2825
import OpenInEditorButton from './OpenInEditorButton';
2926
import InspectedElementViewSourceButton from './InspectedElementViewSourceButton';
3027
import Skeleton from './Skeleton';
28+
import useEditorURL from '../useEditorURL';
3129

3230
import styles from './InspectedElement.css';
3331

@@ -134,12 +132,7 @@ export default function InspectedElementWrapper(_: Props): React.Node {
134132
getAlwaysOpenInEditor,
135133
);
136134

137-
const editorURL = useSyncExternalStore(function subscribe(callback) {
138-
window.addEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
139-
return function unsubscribe() {
140-
window.removeEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
141-
};
142-
}, getOpenInEditorURL);
135+
const editorURL = useEditorURL();
143136

144137
const toggleErrored = useCallback(() => {
145138
if (inspectedElement == null) {

packages/react-devtools-shared/src/devtools/views/Editor/EditorPane.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import * as React from 'react';
11-
import {useSyncExternalStore, useState, startTransition} from 'react';
11+
import {useState, startTransition} from 'react';
1212

1313
import portaledContent from '../portaledContent';
1414

@@ -18,8 +18,7 @@ import Button from 'react-devtools-shared/src/devtools/views/Button';
1818
import ButtonIcon from 'react-devtools-shared/src/devtools/views/ButtonIcon';
1919

2020
import OpenInEditorButton from './OpenInEditorButton';
21-
import {getOpenInEditorURL} from '../../../utils';
22-
import {LOCAL_STORAGE_OPEN_IN_EDITOR_URL} from '../../../constants';
21+
import useEditorURL from '../useEditorURL';
2322

2423
import EditorSettings from './EditorSettings';
2524
import CodeEditorByDefault from '../Settings/CodeEditorByDefault';
@@ -39,17 +38,7 @@ function EditorPane({selectedSource}: Props) {
3938
const [showSettings, setShowSettings] = useState(false);
4039
const [showLinkInfo, setShowLinkInfo] = useState(false);
4140

42-
const editorURL = useSyncExternalStore(
43-
function subscribe(callback) {
44-
window.addEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
45-
return function unsubscribe() {
46-
window.removeEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
47-
};
48-
},
49-
function getState() {
50-
return getOpenInEditorURL();
51-
},
52-
);
41+
const editorURL = useEditorURL();
5342

5443
if (showLinkInfo) {
5544
return (

packages/react-devtools-shared/src/devtools/views/Settings/CodeEditorOptions.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ import {
1313
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
1414
} from '../../../constants';
1515
import {useLocalStorage} from '../hooks';
16-
import {getDefaultOpenInEditorURL} from 'react-devtools-shared/src/utils';
16+
import {
17+
getDefaultPreset,
18+
getDefaultOpenInEditorURL,
19+
} from 'react-devtools-shared/src/utils';
1720

1821
import styles from './SettingsShared.css';
1922

20-
const vscodeFilepath = 'vscode://file/{path}:{line}:{column}';
21-
2223
export default function CodeEditorOptions({
2324
environmentNames,
2425
}: {
2526
environmentNames: Promise<Array<string>>,
2627
}): React.Node {
2728
const [openInEditorURLPreset, setOpenInEditorURLPreset] = useLocalStorage<
2829
'vscode' | 'custom',
29-
>(LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET, 'custom');
30+
>(LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET, getDefaultPreset());
3031

3132
const [openInEditorURL, setOpenInEditorURL] = useLocalStorage<string>(
3233
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
@@ -40,11 +41,6 @@ export default function CodeEditorOptions({
4041
onChange={({currentTarget}) => {
4142
const selectedValue = currentTarget.value;
4243
setOpenInEditorURLPreset(selectedValue);
43-
if (selectedValue === 'vscode') {
44-
setOpenInEditorURL(vscodeFilepath);
45-
} else if (selectedValue === 'custom') {
46-
setOpenInEditorURL('');
47-
}
4844
}}>
4945
<option value="vscode">VS Code</option>
5046
<option value="custom">Custom</option>
@@ -53,7 +49,7 @@ export default function CodeEditorOptions({
5349
<input
5450
className={styles.Input}
5551
type="text"
56-
placeholder={process.env.EDITOR_URL ? process.env.EDITOR_URL : ''}
52+
placeholder={getDefaultOpenInEditorURL()}
5753
value={openInEditorURL}
5854
onChange={event => {
5955
setOpenInEditorURL(event.target.value);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 {useCallback, useSyncExternalStore} from 'react';
11+
12+
import {getOpenInEditorURL} from '../../utils';
13+
import {
14+
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
15+
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
16+
} from '../../constants';
17+
18+
const useEditorURL = (): string => {
19+
const editorURL = useSyncExternalStore(
20+
useCallback(function subscribe(callback) {
21+
window.addEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
22+
window.addEventListener(
23+
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
24+
callback,
25+
);
26+
return function unsubscribe() {
27+
window.removeEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
28+
window.removeEventListener(
29+
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
30+
callback,
31+
);
32+
};
33+
}, []),
34+
getOpenInEditorURL,
35+
);
36+
return editorURL;
37+
};
38+
39+
export default useEditorURL;

packages/react-devtools-shared/src/devtools/views/useOpenResource.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import {useCallback, useContext, useSyncExternalStore} from 'react';
1313

1414
import ViewElementSourceContext from './Components/ViewElementSourceContext';
1515

16-
import {getOpenInEditorURL, getAlwaysOpenInEditor} from '../../utils';
17-
import {
18-
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
19-
LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR,
20-
} from '../../constants';
16+
import {getAlwaysOpenInEditor} from '../../utils';
17+
import useEditorURL from './useEditorURL';
18+
import {LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR} from '../../constants';
2119

2220
import {checkConditions} from './Editor/utils';
2321

@@ -32,15 +30,7 @@ const useOpenResource = (
3230
ViewElementSourceContext,
3331
);
3432

35-
const editorURL = useSyncExternalStore(
36-
useCallback(function subscribe(callback) {
37-
window.addEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
38-
return function unsubscribe() {
39-
window.removeEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
40-
};
41-
}, []),
42-
getOpenInEditorURL,
43-
);
33+
const editorURL = useEditorURL();
4434

4535
const alwaysOpenInEditor = useSyncExternalStore(
4636
useCallback(function subscribe(callback) {

packages/react-devtools-shared/src/utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
TREE_OPERATION_UPDATE_TREE_BASE_DURATION,
3636
LOCAL_STORAGE_COMPONENT_FILTER_PREFERENCES_KEY,
3737
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
38+
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
3839
LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR,
3940
SESSION_STORAGE_RELOAD_AND_PROFILE_KEY,
4041
SESSION_STORAGE_RECORD_CHANGE_DESCRIPTIONS_KEY,
@@ -385,14 +386,27 @@ export function filterOutLocationComponentFilters(
385386
return componentFilters.filter(f => f.type !== ComponentFilterLocation);
386387
}
387388

389+
const vscodeFilepath = 'vscode://file/{path}:{line}:{column}';
390+
391+
export function getDefaultPreset(): 'custom' | 'vscode' {
392+
return typeof process.env.EDITOR_URL === 'string' ? 'custom' : 'vscode';
393+
}
394+
388395
export function getDefaultOpenInEditorURL(): string {
389396
return typeof process.env.EDITOR_URL === 'string'
390397
? process.env.EDITOR_URL
391-
: '';
398+
: vscodeFilepath;
392399
}
393400

394401
export function getOpenInEditorURL(): string {
395402
try {
403+
const rawPreset = localStorageGetItem(
404+
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
405+
);
406+
switch (rawPreset) {
407+
case '"vscode"':
408+
return vscodeFilepath;
409+
}
396410
const raw = localStorageGetItem(LOCAL_STORAGE_OPEN_IN_EDITOR_URL);
397411
if (raw != null) {
398412
return JSON.parse(raw);

0 commit comments

Comments
 (0)