| 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; |