ConnectPage.js (9053B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 createFactory, 9 PureComponent, 10 } = require("resource://devtools/client/shared/vendor/react.mjs"); 11 const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js"); 12 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs"); 13 14 const FluentReact = require("resource://devtools/client/shared/vendor/fluent-react.js"); 15 const Localized = createFactory(FluentReact.Localized); 16 17 const { 18 USB_STATES, 19 } = require("resource://devtools/client/aboutdebugging/src/constants.js"); 20 21 const Actions = require("resource://devtools/client/aboutdebugging/src/actions/index.js"); 22 23 loader.lazyRequireGetter( 24 this, 25 "ADB_ADDON_STATES", 26 "resource://devtools/client/shared/remote-debugging/adb/adb-addon.js", 27 true 28 ); 29 30 const Link = createFactory( 31 require("resource://devtools/client/shared/vendor/react-router-dom.js").Link 32 ); 33 const ConnectSection = createFactory( 34 require("resource://devtools/client/aboutdebugging/src/components/connect/ConnectSection.js") 35 ); 36 const ConnectSteps = createFactory( 37 require("resource://devtools/client/aboutdebugging/src/components/connect/ConnectSteps.js") 38 ); 39 const NetworkLocationsForm = createFactory( 40 require("resource://devtools/client/aboutdebugging/src/components/connect/NetworkLocationsForm.js") 41 ); 42 const NetworkLocationsList = createFactory( 43 require("resource://devtools/client/aboutdebugging/src/components/connect/NetworkLocationsList.js") 44 ); 45 46 const { 47 PAGE_TYPES, 48 RUNTIMES, 49 } = require("resource://devtools/client/aboutdebugging/src/constants.js"); 50 const Types = require("resource://devtools/client/aboutdebugging/src/types/index.js"); 51 52 const USB_ICON_SRC = 53 "chrome://devtools/skin/images/aboutdebugging-usb-icon.svg"; 54 const GLOBE_ICON_SRC = 55 "chrome://devtools/skin/images/aboutdebugging-globe-icon.svg"; 56 57 const TROUBLESHOOT_USB_URL = 58 "https://firefox-source-docs.mozilla.org/devtools-user/about_colon_debugging/index.html#connecting-to-a-remote-device"; 59 const TROUBLESHOOT_NETWORK_URL = 60 "https://firefox-source-docs.mozilla.org/devtools-user/about_colon_debugging/index.html#connecting-over-the-network"; 61 62 class ConnectPage extends PureComponent { 63 static get propTypes() { 64 return { 65 adbAddonStatus: Types.adbAddonStatus, 66 dispatch: PropTypes.func.isRequired, 67 networkLocations: PropTypes.arrayOf(Types.location).isRequired, 68 }; 69 } 70 71 componentDidMount() { 72 this.props.dispatch(Actions.selectPage(PAGE_TYPES.CONNECT)); 73 } 74 75 onToggleUSBClick() { 76 const { adbAddonStatus } = this.props; 77 const isAddonInstalled = adbAddonStatus === ADB_ADDON_STATES.INSTALLED; 78 if (isAddonInstalled) { 79 this.props.dispatch(Actions.uninstallAdbAddon()); 80 } else { 81 this.props.dispatch(Actions.installAdbAddon()); 82 } 83 } 84 85 getUsbStatus() { 86 switch (this.props.adbAddonStatus) { 87 case ADB_ADDON_STATES.INSTALLED: 88 return USB_STATES.ENABLED_USB; 89 case ADB_ADDON_STATES.UNINSTALLED: 90 return USB_STATES.DISABLED_USB; 91 default: 92 return USB_STATES.UPDATING_USB; 93 } 94 } 95 96 renderUsbStatus() { 97 const statusTextId = { 98 [USB_STATES.ENABLED_USB]: "about-debugging-setup-usb-status-enabled", 99 [USB_STATES.DISABLED_USB]: "about-debugging-setup-usb-status-disabled", 100 [USB_STATES.UPDATING_USB]: "about-debugging-setup-usb-status-updating", 101 }[this.getUsbStatus()]; 102 103 return Localized( 104 { 105 id: statusTextId, 106 }, 107 dom.span( 108 { 109 className: "connect-page__usb-section__heading__status", 110 }, 111 statusTextId 112 ) 113 ); 114 } 115 116 renderUsbToggleButton() { 117 const usbStatus = this.getUsbStatus(); 118 119 const localizedStates = { 120 [USB_STATES.ENABLED_USB]: "about-debugging-setup-usb-disable-button", 121 [USB_STATES.DISABLED_USB]: "about-debugging-setup-usb-enable-button", 122 [USB_STATES.UPDATING_USB]: "about-debugging-setup-usb-updating-button", 123 }; 124 const localizedState = localizedStates[usbStatus]; 125 126 // Disable the button while the USB status is updating. 127 const disabled = usbStatus === USB_STATES.UPDATING_USB; 128 129 return Localized( 130 { 131 id: localizedState, 132 }, 133 dom.button( 134 { 135 className: 136 "default-button connect-page__usb-section__heading__toggle " + 137 "qa-connect-usb-toggle-button", 138 disabled, 139 onClick: () => this.onToggleUSBClick(), 140 }, 141 localizedState 142 ) 143 ); 144 } 145 146 renderUsb() { 147 const { adbAddonStatus } = this.props; 148 const isAddonInstalled = adbAddonStatus === ADB_ADDON_STATES.INSTALLED; 149 return ConnectSection( 150 { 151 icon: USB_ICON_SRC, 152 title: dom.div( 153 { 154 className: "connect-page__usb-section__heading", 155 }, 156 Localized( 157 { id: "about-debugging-setup-usb-title" }, 158 dom.span( 159 { 160 className: "connect-page__usb-section__heading__title", 161 }, 162 "USB" 163 ) 164 ), 165 this.renderUsbStatus(), 166 this.renderUsbToggleButton() 167 ), 168 }, 169 isAddonInstalled 170 ? ConnectSteps({ 171 steps: [ 172 { 173 localizationId: 174 "about-debugging-setup-usb-step-enable-dev-menu2", 175 }, 176 { 177 localizationId: "about-debugging-setup-usb-step-enable-debug2", 178 }, 179 { 180 localizationId: 181 "about-debugging-setup-usb-step-enable-file-transfer", 182 }, 183 { 184 localizationId: 185 "about-debugging-setup-usb-step-enable-debug-firefox2", 186 }, 187 { 188 localizationId: "about-debugging-setup-usb-step-plug-device", 189 }, 190 ], 191 }) 192 : Localized( 193 { 194 id: "about-debugging-setup-usb-disabled", 195 }, 196 dom.aside( 197 { 198 className: "qa-connect-usb-disabled-message", 199 }, 200 "Enabling this will download and add the required Android USB debugging " + 201 "components to Firefox." 202 ) 203 ), 204 this.renderTroubleshootText(RUNTIMES.USB) 205 ); 206 } 207 208 renderNetwork() { 209 const { dispatch, networkLocations } = this.props; 210 211 return Localized( 212 { 213 id: "about-debugging-setup-network", 214 attrs: { title: true }, 215 }, 216 ConnectSection({ 217 icon: GLOBE_ICON_SRC, 218 title: "Network Location", 219 extraContent: dom.div( 220 {}, 221 NetworkLocationsList({ dispatch, networkLocations }), 222 NetworkLocationsForm({ dispatch, networkLocations }), 223 this.renderTroubleshootText(RUNTIMES.NETWORK) 224 ), 225 }) 226 ); 227 } 228 229 renderTroubleshootText(connectionType) { 230 const localizationId = 231 connectionType === RUNTIMES.USB 232 ? "about-debugging-setup-usb-troubleshoot" 233 : "about-debugging-setup-network-troubleshoot"; 234 235 const className = 236 "connect-page__troubleshoot connect-page__troubleshoot--" + 237 `${connectionType === RUNTIMES.USB ? "usb" : "network"}`; 238 239 const url = 240 connectionType === RUNTIMES.USB 241 ? TROUBLESHOOT_USB_URL 242 : TROUBLESHOOT_NETWORK_URL; 243 244 return dom.aside( 245 { 246 className, 247 }, 248 Localized( 249 { 250 id: localizationId, 251 a: dom.a({ 252 href: url, 253 target: "_blank", 254 }), 255 }, 256 dom.p({}, localizationId) 257 ) 258 ); 259 } 260 261 render() { 262 return dom.article( 263 { 264 className: "page connect-page qa-connect-page", 265 }, 266 Localized( 267 { 268 id: "about-debugging-setup-title", 269 }, 270 dom.h1( 271 { 272 className: "alt-heading alt-heading--larger", 273 }, 274 "Setup" 275 ) 276 ), 277 Localized( 278 { 279 id: "about-debugging-setup-intro", 280 }, 281 dom.p( 282 {}, 283 "Configure the connection method you wish to remotely debug your device with." 284 ) 285 ), 286 Localized( 287 { 288 id: "about-debugging-setup-this-firefox2", 289 a: Link({ 290 to: `/runtime/${RUNTIMES.THIS_FIREFOX}`, 291 }), 292 }, 293 dom.p({}, "about-debugging-setup-this-firefox") 294 ), 295 dom.section( 296 { 297 className: "connect-page__breather", 298 }, 299 Localized( 300 { 301 id: "about-debugging-setup-connect-heading", 302 }, 303 dom.h2( 304 { 305 className: "alt-heading", 306 }, 307 "Connect a device" 308 ) 309 ), 310 this.renderUsb(), 311 this.renderNetwork() 312 ) 313 ); 314 } 315 } 316 317 module.exports = FluentReact.withLocalization(ConnectPage);