Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ import Toggle from '../Toggle';
import {ElementTypeSuspense} from 'react-devtools-shared/src/frontend/types';
import InspectedElementView from './InspectedElementView';
import {InspectedElementContext} from './InspectedElementContext';
import {getOpenInEditorURL, getAlwaysOpenInEditor} from '../../../utils';
import {
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR,
} from '../../../constants';
import {getAlwaysOpenInEditor} from '../../../utils';
import {LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR} from '../../../constants';
import FetchFileWithCachingContext from './FetchFileWithCachingContext';
import {symbolicateSourceWithCache} from 'react-devtools-shared/src/symbolicateSource';
import OpenInEditorButton from './OpenInEditorButton';
import InspectedElementViewSourceButton from './InspectedElementViewSourceButton';
import Skeleton from './Skeleton';
import useEditorURL from '../useEditorURL';

import styles from './InspectedElement.css';

Expand Down Expand Up @@ -134,12 +132,7 @@ export default function InspectedElementWrapper(_: Props): React.Node {
getAlwaysOpenInEditor,
);

const editorURL = useSyncExternalStore(function subscribe(callback) {
window.addEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
return function unsubscribe() {
window.removeEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
};
}, getOpenInEditorURL);
const editorURL = useEditorURL();

const toggleErrored = useCallback(() => {
if (inspectedElement == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import * as React from 'react';
import {useSyncExternalStore, useState, startTransition} from 'react';
import {useState, startTransition} from 'react';

import portaledContent from '../portaledContent';

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

import OpenInEditorButton from './OpenInEditorButton';
import {getOpenInEditorURL} from '../../../utils';
import {LOCAL_STORAGE_OPEN_IN_EDITOR_URL} from '../../../constants';
import useEditorURL from '../useEditorURL';

import EditorSettings from './EditorSettings';
import CodeEditorByDefault from '../Settings/CodeEditorByDefault';
Expand All @@ -38,17 +37,7 @@ export type Props = {selectedSource: ?SourceSelection};
function EditorPane({selectedSource}: Props) {
const [showSettings, setShowSettings] = useState(false);

const editorURL = useSyncExternalStore(
function subscribe(callback) {
window.addEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
return function unsubscribe() {
window.removeEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
};
},
function getState() {
return getOpenInEditorURL();
},
);
const editorURL = useEditorURL();

let editorToolbar;
if (showSettings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ import {
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
} from '../../../constants';
import {useLocalStorage} from '../hooks';
import {getDefaultOpenInEditorURL} from 'react-devtools-shared/src/utils';
import {
getDefaultPreset,
getDefaultOpenInEditorURL,
} from 'react-devtools-shared/src/utils';

import styles from './SettingsShared.css';

const vscodeFilepath = 'vscode://file/{path}:{line}:{column}';

export default function CodeEditorOptions({
environmentNames,
}: {
environmentNames: Promise<Array<string>>,
}): React.Node {
const [openInEditorURLPreset, setOpenInEditorURLPreset] = useLocalStorage<
'vscode' | 'custom',
>(LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET, 'custom');
>(LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET, getDefaultPreset());

const [openInEditorURL, setOpenInEditorURL] = useLocalStorage<string>(
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
Expand All @@ -40,11 +41,6 @@ export default function CodeEditorOptions({
onChange={({currentTarget}) => {
const selectedValue = currentTarget.value;
setOpenInEditorURLPreset(selectedValue);
if (selectedValue === 'vscode') {
setOpenInEditorURL(vscodeFilepath);
} else if (selectedValue === 'custom') {
setOpenInEditorURL('');
}
}}>
<option value="vscode">VS Code</option>
<option value="custom">Custom</option>
Expand All @@ -53,7 +49,7 @@ export default function CodeEditorOptions({
<input
className={styles.Input}
type="text"
placeholder={process.env.EDITOR_URL ? process.env.EDITOR_URL : ''}
placeholder={getDefaultOpenInEditorURL()}
value={openInEditorURL}
onChange={event => {
setOpenInEditorURL(event.target.value);
Expand Down
39 changes: 39 additions & 0 deletions packages/react-devtools-shared/src/devtools/views/useEditorURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import {useCallback, useSyncExternalStore} from 'react';

import {getOpenInEditorURL} from '../../utils';
import {
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
} from '../../constants';

const useEditorURL = (): string => {
const editorURL = useSyncExternalStore(
useCallback(function subscribe(callback) {
window.addEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
window.addEventListener(
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
callback,
);
return function unsubscribe() {
window.removeEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
window.removeEventListener(
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
callback,
);
};
}, []),
getOpenInEditorURL,
);
return editorURL;
};

export default useEditorURL;
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import {useCallback, useContext, useSyncExternalStore} from 'react';

import ViewElementSourceContext from './Components/ViewElementSourceContext';

import {getOpenInEditorURL, getAlwaysOpenInEditor} from '../../utils';
import {
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR,
} from '../../constants';
import {getAlwaysOpenInEditor} from '../../utils';
import useEditorURL from './useEditorURL';
import {LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR} from '../../constants';

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

Expand All @@ -32,15 +30,7 @@ const useOpenResource = (
ViewElementSourceContext,
);

const editorURL = useSyncExternalStore(
useCallback(function subscribe(callback) {
window.addEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
return function unsubscribe() {
window.removeEventListener(LOCAL_STORAGE_OPEN_IN_EDITOR_URL, callback);
};
}, []),
getOpenInEditorURL,
);
const editorURL = useEditorURL();

const alwaysOpenInEditor = useSyncExternalStore(
useCallback(function subscribe(callback) {
Expand Down
16 changes: 15 additions & 1 deletion packages/react-devtools-shared/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
TREE_OPERATION_UPDATE_TREE_BASE_DURATION,
LOCAL_STORAGE_COMPONENT_FILTER_PREFERENCES_KEY,
LOCAL_STORAGE_OPEN_IN_EDITOR_URL,
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR,
SESSION_STORAGE_RELOAD_AND_PROFILE_KEY,
SESSION_STORAGE_RECORD_CHANGE_DESCRIPTIONS_KEY,
Expand Down Expand Up @@ -385,14 +386,27 @@ export function filterOutLocationComponentFilters(
return componentFilters.filter(f => f.type !== ComponentFilterLocation);
}

const vscodeFilepath = 'vscode://file/{path}:{line}:{column}';

export function getDefaultPreset(): 'custom' | 'vscode' {
return typeof process.env.EDITOR_URL === 'string' ? 'custom' : 'vscode';
}

export function getDefaultOpenInEditorURL(): string {
return typeof process.env.EDITOR_URL === 'string'
? process.env.EDITOR_URL
: '';
: vscodeFilepath;
}

export function getOpenInEditorURL(): string {
try {
const rawPreset = localStorageGetItem(
LOCAL_STORAGE_OPEN_IN_EDITOR_URL_PRESET,
);
switch (rawPreset) {
case '"vscode"':
return vscodeFilepath;
}
const raw = localStorageGetItem(LOCAL_STORAGE_OPEN_IN_EDITOR_URL);
if (raw != null) {
return JSON.parse(raw);
Expand Down
Loading