|
| 1 | +import PropTypes from 'prop-types' |
| 2 | +import React from 'react' |
| 3 | +import CSSModules from 'browser/lib/CSSModules' |
| 4 | +import styles from './ConfigTab.styl' |
| 5 | +import ConfigManager from 'browser/main/lib/ConfigManager' |
| 6 | +import { store } from 'browser/main/store' |
| 7 | +import _ from 'lodash' |
| 8 | +import i18n from 'browser/lib/i18n' |
| 9 | +import { sync as commandExists } from 'command-exists' |
| 10 | +const electron = require('electron') |
| 11 | +const ipc = electron.ipcRenderer |
| 12 | +const { remote } = electron |
| 13 | +const { dialog } = remote |
| 14 | +class PluginsTab extends React.Component { |
| 15 | + constructor(props) { |
| 16 | + super(props) |
| 17 | + |
| 18 | + this.state = { |
| 19 | + config: props.config |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + componentDidMount() { |
| 24 | + this.handleSettingDone = () => { |
| 25 | + this.setState({ |
| 26 | + pluginsAlert: { |
| 27 | + type: 'success', |
| 28 | + message: i18n.__('Successfully applied!') |
| 29 | + } |
| 30 | + }) |
| 31 | + } |
| 32 | + this.handleSettingError = err => { |
| 33 | + this.setState({ |
| 34 | + pluginsAlert: { |
| 35 | + type: 'error', |
| 36 | + message: |
| 37 | + err.message != null ? err.message : i18n.__('An error occurred!') |
| 38 | + } |
| 39 | + }) |
| 40 | + } |
| 41 | + this.oldWakatimeConfig = this.state.config.wakatime |
| 42 | + ipc.addListener('APP_SETTING_DONE', this.handleSettingDone) |
| 43 | + ipc.addListener('APP_SETTING_ERROR', this.handleSettingError) |
| 44 | + } |
| 45 | + |
| 46 | + componentWillUnmount() { |
| 47 | + ipc.removeListener('APP_SETTING_DONE', this.handleSettingDone) |
| 48 | + ipc.removeListener('APP_SETTING_ERROR', this.handleSettingError) |
| 49 | + } |
| 50 | + |
| 51 | + checkWakatimePluginRequirement() { |
| 52 | + const { wakatime } = this.state.config |
| 53 | + if (wakatime.isActive && !commandExists('wakatime')) { |
| 54 | + this.setState({ |
| 55 | + wakatimePluginAlert: { |
| 56 | + type: i18n.__('Warning'), |
| 57 | + message: i18n.__('Missing wakatime cli') |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + const alertConfig = { |
| 62 | + type: 'warning', |
| 63 | + message: i18n.__('Missing Wakatime CLI'), |
| 64 | + detail: i18n.__( |
| 65 | + `Please install Wakatime CLI to use Wakatime tracker feature.` |
| 66 | + ), |
| 67 | + buttons: [i18n.__('OK')] |
| 68 | + } |
| 69 | + dialog.showMessageBox(remote.getCurrentWindow(), alertConfig) |
| 70 | + } else { |
| 71 | + this.setState({ |
| 72 | + wakatimePluginAlert: null |
| 73 | + }) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + handleSaveButtonClick(e) { |
| 78 | + const newConfig = { |
| 79 | + wakatime: { |
| 80 | + isActive: this.state.config.wakatime.isActive, |
| 81 | + key: this.state.config.wakatime.key |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + ConfigManager.set(newConfig) |
| 86 | + |
| 87 | + store.dispatch({ |
| 88 | + type: 'SET_CONFIG', |
| 89 | + config: newConfig |
| 90 | + }) |
| 91 | + this.clearMessage() |
| 92 | + this.props.haveToSave() |
| 93 | + this.checkWakatimePluginRequirement() |
| 94 | + } |
| 95 | + |
| 96 | + handleIsWakatimePluginActiveChange(e) { |
| 97 | + const { config } = this.state |
| 98 | + config.wakatime.isActive = !config.wakatime.isActive |
| 99 | + this.setState({ |
| 100 | + config |
| 101 | + }) |
| 102 | + if (_.isEqual(this.oldWakatimeConfig.isActive, config.wakatime.isActive)) { |
| 103 | + this.props.haveToSave() |
| 104 | + } else { |
| 105 | + this.props.haveToSave({ |
| 106 | + tab: 'Plugins', |
| 107 | + type: 'warning', |
| 108 | + message: i18n.__('Unsaved Changes!') |
| 109 | + }) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + handleWakatimeKeyChange(e) { |
| 114 | + const { config } = this.state |
| 115 | + config.wakatime = { |
| 116 | + isActive: true, |
| 117 | + key: this.refs.wakatimeKey.value |
| 118 | + } |
| 119 | + this.setState({ |
| 120 | + config |
| 121 | + }) |
| 122 | + if (_.isEqual(this.oldWakatimeConfig.key, config.wakatime.key)) { |
| 123 | + this.props.haveToSave() |
| 124 | + } else { |
| 125 | + this.props.haveToSave({ |
| 126 | + tab: 'Plugins', |
| 127 | + type: 'warning', |
| 128 | + message: i18n.__('Unsaved Changes!') |
| 129 | + }) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + clearMessage() { |
| 134 | + _.debounce(() => { |
| 135 | + this.setState({ |
| 136 | + pluginsAlert: null |
| 137 | + }) |
| 138 | + }, 2000)() |
| 139 | + } |
| 140 | + |
| 141 | + render() { |
| 142 | + const pluginsAlert = this.state.pluginsAlert |
| 143 | + const pluginsAlertElement = |
| 144 | + pluginsAlert != null ? ( |
| 145 | + <p className={`alert ${pluginsAlert.type}`}>{pluginsAlert.message}</p> |
| 146 | + ) : null |
| 147 | + |
| 148 | + const wakatimeAlert = this.state.wakatimePluginAlert |
| 149 | + const wakatimePluginAlertElement = |
| 150 | + wakatimeAlert != null ? ( |
| 151 | + <p className={`alert ${wakatimeAlert.type}`}>{wakatimeAlert.message}</p> |
| 152 | + ) : null |
| 153 | + |
| 154 | + const { config } = this.state |
| 155 | + |
| 156 | + return ( |
| 157 | + <div styleName='root'> |
| 158 | + <div styleName='group'> |
| 159 | + <div styleName='group-header'>{i18n.__('Plugins')}</div> |
| 160 | + <div styleName='group-header2'>{i18n.__('Wakatime')}</div> |
| 161 | + <div styleName='group-checkBoxSection'> |
| 162 | + <label> |
| 163 | + <input |
| 164 | + onChange={e => this.handleIsWakatimePluginActiveChange(e)} |
| 165 | + checked={config.wakatime.isActive} |
| 166 | + ref='wakatimeIsActive' |
| 167 | + type='checkbox' |
| 168 | + /> |
| 169 | + |
| 170 | + {i18n.__('Enable Wakatime')} |
| 171 | + </label> |
| 172 | + </div> |
| 173 | + <div styleName='group-section'> |
| 174 | + <div styleName='group-section-label'>{i18n.__('Wakatime key')}</div> |
| 175 | + <div styleName='group-section-control'> |
| 176 | + <input |
| 177 | + styleName='group-section-control-input' |
| 178 | + onChange={e => this.handleWakatimeKeyChange(e)} |
| 179 | + disabled={!config.wakatime.isActive} |
| 180 | + ref='wakatimeKey' |
| 181 | + value={config.wakatime.key} |
| 182 | + type='text' |
| 183 | + /> |
| 184 | + {wakatimePluginAlertElement} |
| 185 | + </div> |
| 186 | + </div> |
| 187 | + <div styleName='group-control'> |
| 188 | + <button |
| 189 | + styleName='group-control-rightButton' |
| 190 | + onClick={e => this.handleSaveButtonClick(e)} |
| 191 | + > |
| 192 | + {i18n.__('Save')} |
| 193 | + </button> |
| 194 | + {pluginsAlertElement} |
| 195 | + </div> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + ) |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +PluginsTab.propTypes = { |
| 203 | + dispatch: PropTypes.func, |
| 204 | + haveToSave: PropTypes.func |
| 205 | +} |
| 206 | + |
| 207 | +export default CSSModules(PluginsTab, styles) |
0 commit comments