ServiceWorkerAdditionalActions.js (5151B)
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 const { 14 connect, 15 } = require("resource://devtools/client/shared/vendor/react-redux.js"); 16 17 const FluentReact = require("resource://devtools/client/shared/vendor/fluent-react.js"); 18 const Localized = createFactory(FluentReact.Localized); 19 20 const { 21 getCurrentRuntimeDetails, 22 } = require("resource://devtools/client/aboutdebugging/src/modules/runtimes-state-helper.js"); 23 24 const Actions = require("resource://devtools/client/aboutdebugging/src/actions/index.js"); 25 const Types = require("resource://devtools/client/aboutdebugging/src/types/index.js"); 26 const { 27 SERVICE_WORKER_STATUSES, 28 } = require("resource://devtools/client/aboutdebugging/src/constants.js"); 29 30 /** 31 * The main purpose of this component is to expose a meaningful prop 32 * disabledTitle that can be used with fluent localization. 33 */ 34 class _ActionButton extends PureComponent { 35 static get propTypes() { 36 return { 37 children: PropTypes.node, 38 className: PropTypes.string.isRequired, 39 disabled: PropTypes.bool, 40 disabledTitle: PropTypes.string, 41 onClick: PropTypes.func.isRequired, 42 }; 43 } 44 45 render() { 46 const { className, disabled, disabledTitle, onClick } = this.props; 47 return dom.button( 48 { 49 className, 50 disabled, 51 onClick: () => onClick(), 52 title: disabled && disabledTitle ? disabledTitle : undefined, 53 }, 54 this.props.children 55 ); 56 } 57 } 58 const ActionButtonFactory = createFactory(_ActionButton); 59 60 /** 61 * This component displays buttons for service worker. 62 */ 63 class ServiceWorkerAdditionalActions extends PureComponent { 64 static get propTypes() { 65 return { 66 dispatch: PropTypes.func.isRequired, 67 // Provided by wrapping the component with FluentReact.withLocalization. 68 getString: PropTypes.func.isRequired, 69 // Provided by redux state 70 runtimeDetails: Types.runtimeDetails.isRequired, 71 target: Types.debugTarget.isRequired, 72 }; 73 } 74 75 push() { 76 const { dispatch, target } = this.props; 77 dispatch( 78 Actions.pushServiceWorker(target.id, target.details.registrationFront) 79 ); 80 } 81 82 start() { 83 const { dispatch, target } = this.props; 84 dispatch(Actions.startServiceWorker(target.details.registrationFront)); 85 } 86 87 unregister() { 88 const { dispatch, target } = this.props; 89 dispatch(Actions.unregisterServiceWorker(target.details.registrationFront)); 90 } 91 92 _renderButton({ className, disabled, key, labelId, onClick }) { 93 return Localized( 94 { 95 id: labelId, 96 attrs: { 97 disabledTitle: !!disabled, 98 }, 99 key, 100 }, 101 ActionButtonFactory( 102 { 103 className, 104 disabled, 105 onClick: () => onClick(), 106 }, 107 labelId 108 ) 109 ); 110 } 111 112 _renderPushButton() { 113 return this._renderButton({ 114 className: "default-button default-button--micro qa-push-button", 115 disabled: !this.props.runtimeDetails.canDebugServiceWorkers, 116 key: "service-worker-push-button", 117 labelId: "about-debugging-worker-action-push2", 118 onClick: this.push.bind(this), 119 }); 120 } 121 122 _renderStartButton() { 123 return this._renderButton({ 124 className: "default-button default-button--micro qa-start-button", 125 disabled: !this.props.runtimeDetails.canDebugServiceWorkers, 126 key: "service-worker-start-button", 127 labelId: "about-debugging-worker-action-start2", 128 onClick: this.start.bind(this), 129 }); 130 } 131 132 _renderUnregisterButton() { 133 return this._renderButton({ 134 className: "default-button default-button--micro qa-unregister-button", 135 key: "service-worker-unregister-button", 136 labelId: "about-debugging-worker-action-unregister", 137 disabled: false, 138 onClick: this.unregister.bind(this), 139 }); 140 } 141 142 _renderActionButtons() { 143 const { status } = this.props.target.details; 144 145 switch (status) { 146 case SERVICE_WORKER_STATUSES.RUNNING: 147 return [this._renderUnregisterButton(), this._renderPushButton()]; 148 case SERVICE_WORKER_STATUSES.REGISTERING: 149 return null; 150 case SERVICE_WORKER_STATUSES.STOPPED: 151 return [this._renderUnregisterButton(), this._renderStartButton()]; 152 default: 153 console.error("Unexpected service worker status: " + status); 154 return null; 155 } 156 } 157 158 render() { 159 return dom.div( 160 { 161 className: "toolbar toolbar--right-align", 162 }, 163 this._renderActionButtons() 164 ); 165 } 166 } 167 168 const mapStateToProps = state => { 169 return { 170 runtimeDetails: getCurrentRuntimeDetails(state.runtimes), 171 }; 172 }; 173 174 module.exports = FluentReact.withLocalization( 175 connect(mapStateToProps)(ServiceWorkerAdditionalActions) 176 );