Skip to content

Commit 3850135

Browse files
author
Matthias Rütten
committed
make it possible to disable notifications for WIP MRs (resolves #55)
1 parent 899b20a commit 3850135

File tree

6 files changed

+50
-18
lines changed

6 files changed

+50
-18
lines changed

src/renderer/components/NotificationsEmiter.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useState, useEffect } from 'react'
22
import { shell } from 'electron'
3+
import * as log from 'electron-log'
34

45
import { useBackend } from '../hooks/merge-requests/backend'
56
import { MergeRequestWithProject } from '../hooks/merge-requests/types'
@@ -15,13 +16,18 @@ export const NotificationsEmiter = () => {
1516
const newMergeRequests = getNewMergeRequests(mergeRequestWithProjects, previousMergeRequests)
1617
if (newMergeRequests.length > 0 && config.generalConfig.useNotifications) {
1718
newMergeRequests.forEach(newMergeRequest => {
19+
if (config.generalConfig.disableWipNotifications && newMergeRequest.work_in_progress) {
20+
log.debug(`Ignoring the WIP merge request "${newMergeRequest.title}"`)
21+
return
22+
}
1823
const body = newMergeRequest.title
1924
const icon = newMergeRequest.author.avatar_url
2025
const data = {
2126
url: newMergeRequest.web_url,
2227
}
2328

24-
const notification = new Notification('New Merge Request', { body, icon, data })
29+
const title = newMergeRequest.work_in_progress ? 'New WIP Merge Request' : 'New Merge Request'
30+
const notification = new Notification(title, { body, icon, data })
2531
notification.onclick = event => {
2632
const url = (event?.currentTarget as any)?.data?.url
2733
if (url) {

src/renderer/components/form/CheckboxInput.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ interface FormCheckboxProps {
88
name: string
99
info?: string | JSX.Element
1010
error?: string
11+
indented?: number
1112
[htmlAttribute: string]: any
1213
}
1314

14-
export const CheckboxInput: React.FunctionComponent<FormCheckboxProps> = ({ id, label, error, info, ...props }) => (
15-
<Box mb={3}>
15+
export const CheckboxInput: React.FunctionComponent<FormCheckboxProps> = ({ id, label, info, error, indented, ...props }) => (
16+
<Box mb={3} ml={indented || 0}>
1617
<Label fontWeight='bold' fontSize={1} htmlFor={id}>
1718
<Checkbox id={id} name={name} {...props} />
1819
<Text p='3.5px'>{label}</Text>

src/renderer/components/settings/ConnectionSettings.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ export const ConnectionSettings: React.FunctionComponent = () => {
7575
}
7676

7777
const handleProjectsChange = (group: string) => (event: React.ChangeEvent<HTMLInputElement>) => {
78-
console.log('handleProjectsChange', group, values)
7978
setValues({ ...values, projects: { ...values.projects, [group]: event.target.value } })
8079
}
8180

@@ -108,8 +107,6 @@ export const ConnectionSettings: React.FunctionComponent = () => {
108107
}
109108

110109
if (values.url && values.token && values.groups) {
111-
console.log('projects', values.projects)
112-
113110
const projects = Object.keys(values.projects).reduce((previousValue, current) => {
114111
previousValue[current] = splitStringByComma(values.projects[current])
115112

src/renderer/components/settings/GeneralSettings.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { CheckboxInput } from '../form/CheckboxInput'
55

66
interface FormData {
77
useNotifications: boolean
8+
disableWipNotifications: boolean
89
darkMode: boolean
910
}
1011

1112
export const GeneralSettings: React.FunctionComponent = () => {
1213
const { config, updateGeneralConfig } = useConfig()
1314

1415
const [values, setValues] = React.useState<FormData>({
15-
useNotifications: config.generalConfig.useNotifications,
16-
darkMode: config.generalConfig.darkMode,
16+
...config.generalConfig,
1717
})
1818

1919
const updateGeneralSettings = (name: keyof FormData) => async (event: React.ChangeEvent<HTMLInputElement>) => {
@@ -26,12 +26,22 @@ export const GeneralSettings: React.FunctionComponent = () => {
2626
<Box p={2}>
2727
<form autoComplete='off'>
2828
<CheckboxInput
29-
label='Send me a notification for new MRs'
29+
label='Notify about new merge requests'
3030
id='useNotifications'
3131
name='useNotifications'
3232
defaultChecked={values.useNotifications}
3333
onChange={updateGeneralSettings('useNotifications')}
3434
/>
35+
{values.useNotifications && (
36+
<CheckboxInput
37+
label='Ignore WIP merge requests'
38+
id='ignoreWip'
39+
name='ignoreWip'
40+
indented={3}
41+
defaultChecked={values.disableWipNotifications}
42+
onChange={updateGeneralSettings('disableWipNotifications')}
43+
/>
44+
)}
3545
<CheckboxInput label='Dark mode' id='darkMode' name='darkMode' defaultChecked={values.darkMode} onChange={updateGeneralSettings('darkMode')} />
3646
</form>
3747
</Box>

src/renderer/hooks/config.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@ export interface ConnectionConfig {
1919

2020
export interface GeneralConfig {
2121
useNotifications: boolean
22+
disableWipNotifications: boolean
2223
darkMode: boolean
2324
}
2425

2526
interface ConfigContext {
2627
config: Config
2728
removeConfig: () => void
28-
updateConfig: (newConfig: Config) => void
2929
updateConnectionConfig: (newConnectionConfig: ConnectionConfig) => void
3030
updateGeneralConfig: (newGeneralConfig: GeneralConfig) => void
3131
}
3232

3333
const defaultConfig: Config = {
3434
generalConfig: {
3535
useNotifications: true,
36+
disableWipNotifications: true,
3637
darkMode: remote.nativeTheme.shouldUseDarkColors,
3738
},
3839
}
@@ -70,13 +71,30 @@ const configPreviousVersion = (oldKey: string, newKey: string): Config | null =>
7071
return null
7172
}
7273

73-
const configFromPreviousVersionOrDefault = (): Config | null => {
74+
const configFromPreviousVersionOrDefault = (): Config => {
7475
return configPreviousVersion('config', 'config.v3') || configPreviousVersion('config.v2', 'config.v3') || defaultConfig
7576
}
7677

77-
export const ConfigProvider = ({ ...props }) => {
78+
const loadConfig = (): Config => {
7879
const localStorageValue = window.localStorage.getItem('config.v3')
79-
const [config, setConfig] = React.useState<Config>(localStorageValue ? JSON.parse(localStorageValue) : configFromPreviousVersionOrDefault())
80+
81+
if (localStorageValue) {
82+
const savedConfig = JSON.parse(localStorageValue)
83+
84+
return {
85+
connectionConfig: savedConfig.connectionConfig,
86+
generalConfig: {
87+
...defaultConfig.generalConfig,
88+
...savedConfig.generalConfig,
89+
},
90+
}
91+
}
92+
93+
return configFromPreviousVersionOrDefault()
94+
}
95+
96+
export const ConfigProvider = ({ ...props }) => {
97+
const [config, setConfig] = React.useState<Config>(loadConfig())
8098

8199
const removeConfig = () => {
82100
setConfig(defaultConfig)
@@ -104,5 +122,5 @@ export const ConfigProvider = ({ ...props }) => {
104122
updateConfig(newConfig)
105123
}
106124

107-
return <Context.Provider value={{ config, updateConfig, removeConfig, updateConnectionConfig, updateGeneralConfig }} {...props} />
125+
return <Context.Provider value={{ config, removeConfig, updateConnectionConfig, updateGeneralConfig }} {...props} />
108126
}

src/renderer/hooks/merge-requests/testData.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const users: User[] = [
2727

2828
let mrId = 0
2929

30-
const createMr = (title: string, projectId: number): MergeRequest => {
30+
const createMr = (title: string, projectId: number, wip = false): MergeRequest => {
3131
mrId++
3232
const timestamp = moment()
3333
.subtract(1, 'hours')
@@ -49,7 +49,7 @@ const createMr = (title: string, projectId: number): MergeRequest => {
4949
author: randomArrayEntry(users),
5050
assignee: randomArrayEntry(users),
5151
source_project_id: projectId,
52-
work_in_progress: false,
52+
work_in_progress: wip,
5353
user_notes: {
5454
all: randomArrayEntry([3, 5]),
5555
resolved: randomArrayEntry([1, 2, 3]),
@@ -119,11 +119,11 @@ const testData = (): Data => {
119119
if (count % 2 === 0) {
120120
groupedMergeRequests.push({
121121
project: {
122-
id: 3,
122+
id: 4,
123123
name: 'Some other cool project',
124124
name_with_namespace: 'codecentric / Some other cool project',
125125
},
126-
mergeRequests: [createMr('Support Emojis 🚀', 3)],
126+
mergeRequests: [createMr('Support Emojis 🚀', 4)],
127127
})
128128
}
129129

0 commit comments

Comments
 (0)