Skip to content

Commit 39a98e7

Browse files
committed
Add preferences plugins wakatime key #2810
1 parent 57705cf commit 39a98e7

File tree

4 files changed

+166
-11
lines changed

4 files changed

+166
-11
lines changed

browser/lib/wakatime-plugin.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import config from 'browser/main/lib/ConfigManager'
12
const exec = require('child_process').exec
23
const path = require('path')
34
let lastHeartbeat = 0
45

56
function sendWakatimeHeartBeat (storagePath, noteKey, storageName, isWrite, hasFileChanges, isFileChange) {
6-
77
if (new Date().getTime() - lastHeartbeat > 120000 || isFileChange) {
88
const notePath = path.join(storagePath, 'notes', noteKey + '.cson')
99

@@ -12,14 +12,19 @@ function sendWakatimeHeartBeat (storagePath, noteKey, storageName, isWrite, hasF
1212
}
1313

1414
lastHeartbeat = new Date()
15-
// TODO: add --key sdasdsa-sdsad-asdasd-asdsa-asdasdadas from configuration UI or use ~/.wakatime.conf
16-
exec(`wakatime --file ${notePath} --project ${storageName} --plugin Boostnote-wakatime`, (error, stdOut, stdErr) => {
17-
if (error) {
18-
console.log(error)
19-
} else {
20-
console.log('wakatime', 'isWrite', isWrite, 'hasChanges', hasFileChanges)
21-
}
22-
})
15+
const wakatimeKey = config.get().wakatime.key
16+
if (wakatimeKey) {
17+
exec(`wakatime --file ${notePath} --project '${storageName}' --key ${wakatimeKey} --plugin Boostnote-wakatime`, (error, stdOut, stdErr) => {
18+
if (error) {
19+
console.log(error)
20+
lastHeartbeat = 0
21+
} else {
22+
console.log('wakatime', 'isWrite', isWrite, 'hasChanges', hasFileChanges, 'isFileChange', isFileChange)
23+
}
24+
})
25+
}
26+
} else {
27+
console.log('nada :(')
2328
}
2429
}
2530

browser/main/lib/ConfigManager.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export const DEFAULT_CONFIG = {
8989
username: '',
9090
password: ''
9191
},
92-
coloredTags: {}
92+
coloredTags: {},
93+
wakatime: {
94+
key: null
95+
}
9396
}
9497

9598
function validate (config) {
@@ -198,6 +201,7 @@ function set (updates) {
198201
function assignConfigValues (originalConfig, rcConfig) {
199202
const config = Object.assign({}, DEFAULT_CONFIG, originalConfig, rcConfig)
200203
config.hotkey = Object.assign({}, DEFAULT_CONFIG.hotkey, originalConfig.hotkey, rcConfig.hotkey)
204+
config.wakatime = Object.assign({}, DEFAULT_CONFIG.wakatime, originalConfig.wakatime, rcConfig.wakatime)
201205
config.blog = Object.assign({}, DEFAULT_CONFIG.blog, originalConfig.blog, rcConfig.blog)
202206
config.ui = Object.assign({}, DEFAULT_CONFIG.ui, originalConfig.ui, rcConfig.ui)
203207
config.editor = Object.assign({}, DEFAULT_CONFIG.editor, originalConfig.editor, rcConfig.editor)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
10+
const electron = require('electron')
11+
const ipc = electron.ipcRenderer
12+
13+
class PluginsTab extends React.Component {
14+
constructor (props) {
15+
super(props)
16+
17+
this.state = {
18+
config: props.config
19+
}
20+
}
21+
22+
componentDidMount () {
23+
this.handleSettingDone = () => {
24+
this.setState({pluginsAlert: {
25+
type: 'success',
26+
message: i18n.__('Successfully applied!')
27+
}})
28+
}
29+
this.handleSettingError = (err) => {
30+
if (
31+
this.state.config.wakatime.key === '' ||
32+
this.state.config.wakatime.key === null
33+
) {
34+
this.setState({pluginsAlert: {
35+
type: 'success',
36+
message: i18n.__('Successfully applied!')
37+
}})
38+
} else {
39+
this.setState({pluginsAlert: {
40+
type: 'error',
41+
message: err.message != null ? err.message : i18n.__('An error occurred!')
42+
}})
43+
}
44+
}
45+
this.oldWakatimekey = this.state.config.wakatime
46+
ipc.addListener('APP_SETTING_DONE', this.handleSettingDone)
47+
ipc.addListener('APP_SETTING_ERROR', this.handleSettingError)
48+
}
49+
50+
componentWillUnmount () {
51+
ipc.removeListener('APP_SETTING_DONE', this.handleSettingDone)
52+
ipc.removeListener('APP_SETTING_ERROR', this.handleSettingError)
53+
}
54+
55+
handleSaveButtonClick (e) {
56+
const newConfig = {
57+
wakatime: this.state.config.wakatime
58+
}
59+
60+
ConfigManager.set(newConfig)
61+
62+
store.dispatch({
63+
type: 'SET_CONFIG',
64+
config: newConfig
65+
})
66+
this.clearMessage()
67+
this.props.haveToSave()
68+
}
69+
70+
handleWakatimeKeyChange (e) {
71+
const { config } = this.state
72+
config.wakatime = { key: this.refs.key.value }
73+
this.setState({
74+
config
75+
})
76+
if (_.isEqual(this.oldWakatimekey, config.wakatime)) {
77+
this.props.haveToSave()
78+
} else {
79+
this.props.haveToSave({
80+
tab: 'Plugins',
81+
type: 'warning',
82+
message: i18n.__('Unsaved Changes!')
83+
})
84+
}
85+
}
86+
87+
clearMessage () {
88+
_.debounce(() => {
89+
this.setState({
90+
pluginsAlert: null
91+
})
92+
}, 2000)()
93+
}
94+
95+
render () {
96+
const pluginsAlert = this.state.pluginsAlert
97+
const pluginsAlertElement = pluginsAlert != null
98+
? <p className={`alert ${pluginsAlert.type}`}>
99+
{pluginsAlert.message}
100+
</p>
101+
: null
102+
const { config } = this.state
103+
104+
return (
105+
<div styleName='root'>
106+
<div styleName='group'>
107+
<div styleName='group-header'>{i18n.__('Plugins')}</div>
108+
<div styleName='group-section'>
109+
<div styleName='group-section-label'>{i18n.__('Wakatime key')}</div>
110+
<div styleName='group-section-control'>
111+
<input styleName='group-section-control-input'
112+
onChange={(e) => this.handleWakatimeKeyChange(e)}
113+
ref='key'
114+
value={config.wakatime.key}
115+
type='text'
116+
/>
117+
</div>
118+
</div>
119+
<div styleName='group-control'>
120+
<button styleName='group-control-rightButton'
121+
onClick={(e) => this.handleSaveButtonClick(e)}>{i18n.__('Save')}
122+
</button>
123+
{pluginsAlertElement}
124+
</div>
125+
</div>
126+
</div>
127+
)
128+
}
129+
}
130+
131+
PluginsTab.propTypes = {
132+
dispatch: PropTypes.func,
133+
haveToSave: PropTypes.func
134+
}
135+
136+
export default CSSModules(PluginsTab, styles)

browser/main/modals/PreferencesModal/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import InfoTab from './InfoTab'
77
import Crowdfunding from './Crowdfunding'
88
import StoragesTab from './StoragesTab'
99
import SnippetTab from './SnippetTab'
10+
import PluginsTab from './PluginsTab'
1011
import Blog from './Blog'
1112
import ModalEscButton from 'browser/components/ModalEscButton'
1213
import CSSModules from 'browser/lib/CSSModules'
@@ -95,6 +96,14 @@ class Preferences extends React.Component {
9596
data={data}
9697
/>
9798
)
99+
case 'PLUGINS':
100+
return (
101+
<PluginsTab
102+
dispatch={dispatch}
103+
config={config}
104+
haveToSave={alert => this.setState({PluginsAlert: alert})}
105+
/>
106+
)
98107
case 'STORAGES':
99108
default:
100109
return (
@@ -133,7 +142,8 @@ class Preferences extends React.Component {
133142
{target: 'INFO', label: i18n.__('About')},
134143
{target: 'CROWDFUNDING', label: i18n.__('Crowdfunding')},
135144
{target: 'BLOG', label: i18n.__('Blog'), Blog: this.state.BlogAlert},
136-
{target: 'SNIPPET', label: i18n.__('Snippets')}
145+
{target: 'SNIPPET', label: i18n.__('Snippets')},
146+
{target: 'PLUGINS', label: i18n.__('Plugins')}
137147
]
138148

139149
const navButtons = tabs.map((tab) => {

0 commit comments

Comments
 (0)