|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its 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, useState} from 'react'; |
| 12 | +import {unstable_batchedUpdates as batchedUpdates} from 'react-dom'; |
| 13 | +import {ModalDialogContext} from './ModalDialog'; |
| 14 | +import {StoreContext} from './context'; |
| 15 | +import {currentBridgeProtocol} from 'react-devtools-shared/src/bridge'; |
| 16 | +import Button from './Button'; |
| 17 | +import ButtonIcon from './ButtonIcon'; |
| 18 | +import {copy} from 'clipboard-js'; |
| 19 | +import styles from './UnsupportedProtocolDialog.css'; |
| 20 | + |
| 21 | +import type {BridgeProtocol} from 'react-devtools-shared/src/bridge'; |
| 22 | + |
| 23 | +type DAILOG_STATE = 'dialog-not-shown' | 'show-dialog' | 'dialog-shown'; |
| 24 | + |
| 25 | +const DEVTOOLS_VERSION = process.env.DEVTOOLS_VERSION; |
| 26 | + |
| 27 | +export default function UnsupportedProtocolDialog(_: {||}) { |
| 28 | + const {dispatch} = useContext(ModalDialogContext); |
| 29 | + const store = useContext(StoreContext); |
| 30 | + const [state, setState] = useState<DAILOG_STATE>('dialog-not-shown'); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + if (state === 'dialog-not-shown') { |
| 34 | + const showDialog = () => { |
| 35 | + batchedUpdates(() => { |
| 36 | + setState('show-dialog'); |
| 37 | + dispatch({ |
| 38 | + canBeDismissed: false, |
| 39 | + type: 'SHOW', |
| 40 | + content: ( |
| 41 | + <DialogContent |
| 42 | + unsupportedBridgeProtocol={store.unsupportedBridgeProtocol} |
| 43 | + /> |
| 44 | + ), |
| 45 | + }); |
| 46 | + }); |
| 47 | + }; |
| 48 | + |
| 49 | + if (store.unsupportedBridgeProtocol !== null) { |
| 50 | + showDialog(); |
| 51 | + } else { |
| 52 | + store.addListener('unsupportedBridgeProtocolDetected', showDialog); |
| 53 | + return () => { |
| 54 | + store.removeListener('unsupportedBridgeProtocolDetected', showDialog); |
| 55 | + }; |
| 56 | + } |
| 57 | + } |
| 58 | + }, [state, store]); |
| 59 | + |
| 60 | + return null; |
| 61 | +} |
| 62 | + |
| 63 | +function DialogContent({ |
| 64 | + unsupportedBridgeProtocol, |
| 65 | +}: {| |
| 66 | + unsupportedBridgeProtocol: BridgeProtocol, |
| 67 | +|}) { |
| 68 | + const {version, minNpmVersion, maxNpmVersion} = unsupportedBridgeProtocol; |
| 69 | + |
| 70 | + let instructions; |
| 71 | + if (maxNpmVersion === null) { |
| 72 | + const upgradeInstructions = `npm i -g react-devtools@^${minNpmVersion}`; |
| 73 | + instructions = ( |
| 74 | + <p className={styles.Instructions}> |
| 75 | + To fix this, upgrade the DevTools NPM package: |
| 76 | + <pre className={styles.NpmCommand}> |
| 77 | + {upgradeInstructions} |
| 78 | + <Button |
| 79 | + onClick={() => copy(upgradeInstructions)} |
| 80 | + title="Copy upgrade command to clipboard"> |
| 81 | + <ButtonIcon type="copy" /> |
| 82 | + </Button> |
| 83 | + </pre> |
| 84 | + </p> |
| 85 | + ); |
| 86 | + } else { |
| 87 | + const downgradeInstructions = `npm i -g react-devtools@${maxNpmVersion}`; |
| 88 | + instructions = ( |
| 89 | + <p className={styles.Instructions}> |
| 90 | + To fix this, downgrade the DevTools NPM package: |
| 91 | + <pre className={styles.NpmCommand}> |
| 92 | + {downgradeInstructions} |
| 93 | + <Button |
| 94 | + onClick={() => copy(downgradeInstructions)} |
| 95 | + title="Copy downgrade command to clipboard"> |
| 96 | + <ButtonIcon type="copy" /> |
| 97 | + </Button> |
| 98 | + </pre> |
| 99 | + </p> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <Fragment> |
| 105 | + <div className={styles.Row}> |
| 106 | + <div> |
| 107 | + <div className={styles.Title}> |
| 108 | + Unsupported DevTools backend version |
| 109 | + </div> |
| 110 | + <p> |
| 111 | + You are running <code>react-devtools</code> version{' '} |
| 112 | + <span className={styles.Version}>{DEVTOOLS_VERSION}</span>. |
| 113 | + </p> |
| 114 | + <p> |
| 115 | + This requires bridge protocol{' '} |
| 116 | + <span className={styles.Version}> |
| 117 | + version {currentBridgeProtocol.version} |
| 118 | + </span> |
| 119 | + . However the current backend version uses bridge protocol{' '} |
| 120 | + <span className={styles.Version}>version {version}</span>. |
| 121 | + </p> |
| 122 | + {instructions} |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </Fragment> |
| 126 | + ); |
| 127 | +} |
0 commit comments