mirror of
https://github.com/facebook/react.git
synced 2026-02-26 17:55:04 +00:00
Stacked on #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. #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.
40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
/**
|
|
* 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;
|