| 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 {Fragment, useContext, useEffect} from 'react'; |
| 12 | import {BridgeContext} from './context'; |
| 13 | import {ModalDialogContext} from './ModalDialog'; |
| 14 | |
| 15 | import styles from './WarnIfLegacyBackendDetected.css'; |
| 16 | |
| 17 | export default function WarnIfLegacyBackendDetected(_: {}): null { |
| 18 | const bridge = useContext(BridgeContext); |
| 19 | const {dispatch} = useContext(ModalDialogContext); |
| 20 | |
| 21 | // Detect pairing with legacy v3 backend. |
| 22 | // We do this by listening to a message that it broadcasts but the v4 backend doesn't. |
| 23 | // In this case the frontend should show upgrade instructions. |
| 24 | useEffect(() => { |
| 25 | let unlisten: (() => void) | null = null; |
| 26 | unlisten = bridge.wall.listen(message => { |
| 27 | if (message === null || typeof message !== 'object') { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | switch (message.type) { |
| 32 | case 'call': |
| 33 | case 'event': |
| 34 | case 'many-events': |
| 35 | // Any of these types indicate the v3 backend. |
| 36 | dispatch({ |
| 37 | canBeDismissed: false, |
| 38 | id: 'WarnIfLegacyBackendDetected', |
| 39 | type: 'SHOW', |
| 40 | title: 'DevTools v4 is incompatible with this version of React', |
| 41 | content: <InvalidBackendDetected />, |
| 42 | }); |
| 43 | |
| 44 | // Once we've identified the backend version, it's safe to unsubscribe. |
| 45 | if (unlisten !== null) { |
| 46 | unlisten(); |
| 47 | unlisten = null; |
| 48 | } |
| 49 | break; |
| 50 | default: |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | switch (message.event) { |
| 55 | case 'isBackendStorageAPISupported': |
| 56 | case 'isNativeStyleEditorSupported': |
| 57 | case 'operations': |
| 58 | case 'overrideComponentFilters': |
| 59 | // Any of these is sufficient to indicate a v4 backend. |
| 60 | // Once we've identified the backend version, it's safe to unsubscribe. |
| 61 | if (unlisten !== null) { |
| 62 | unlisten(); |
| 63 | unlisten = null; |
| 64 | } |
| 65 | break; |
| 66 | default: |
| 67 | break; |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | return () => { |
| 72 | if (unlisten !== null) { |
| 73 | unlisten(); |
| 74 | unlisten = null; |
| 75 | } |
| 76 | }; |
| 77 | }, [bridge, dispatch]); |
| 78 | |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | function InvalidBackendDetected(_: {}) { |
| 83 | return ( |
| 84 | <Fragment> |
| 85 | <p>Either upgrade React or install React DevTools v3:</p> |
| 86 | <code className={styles.Command}>npm install -d react-devtools@^3</code> |
| 87 | </Fragment> |
| 88 | ); |
| 89 | } |