/
Settings.tsx
382 lines (357 loc) · 10.7 KB
/
Settings.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import React, { useState, useEffect, useContext, useRef } from 'react'
import { DeltaBackend } from '../../delta-remote'
import { C } from 'deltachat-node/dist/constants'
import { Elevation, H5, Card, Classes, Switch, Label } from '@blueprintjs/core'
const { ipcRenderer } = window.electron_functions
import { SettingsContext, useTranslationFunction } from '../../contexts'
import { DesktopSettings } from '../../../shared/shared-types'
import { DialogProps } from './DialogController'
import SettingsAutodelete from './Settings-Autodelete'
import SettingsManageKeys from './Settings-ManageKeys'
import SettingsEncryption from './Settings-Encryption'
import {
DeltaDialogBase,
DeltaDialogHeader,
DeltaDialogBody,
DeltaDialogCloseFooter,
} from './DeltaDialog'
import SettingsBackup from './Settings-Backup'
import SettingsAccount from './Settings-Account'
import SettingsAppearance from './Settings-Appearance'
import SettingsProfile, { SettingsEditProfile } from './Settings-Profile'
import { getLogger } from '../../../shared/logger'
import SettingsCommunication from './Settings-Communication'
const log = getLogger('renderer/dialogs/Settings')
function flipDeltaBoolean(value: string) {
return value === '1' ? '0' : '1'
}
export function SettingsButton(props: any) {
const { onClick, children, ...otherProps } = props
return (
<div className='SettingsButton' onClick={onClick}>
<button {...otherProps}>{children}</button>
</div>
)
}
export function SettingsSelector(props: any) {
const { onClick, currentValue, children, ...otherProps } = props
return (
<div className='SettingsSelector' onClick={onClick}>
<button {...otherProps}>{children}</button>
<div className='CurrentValue'>{currentValue}</div>
</div>
)
}
function DeltaSettingsInput({
configKey,
label,
style,
}: {
configKey: string
label: string
style?: React.CSSProperties
}) {
const input = useRef<HTMLInputElement>(null)
useEffect(() => {
if (input.current) {
input.current.disabled = true
}
DeltaBackend.call('settings.getConfigFor', [configKey]).then(res => {
input.current.value = res[configKey]
input.current.disabled = false
})
}, [configKey])
return (
<Label>
{label}
<input
ref={input}
style={style}
className={Classes.INPUT}
onChange={ev => {
const value = ev.target.value
DeltaBackend.call('settings.setConfig', configKey, value).catch(
log.warn.bind(
null,
'settings.setConfig returned false for:',
configKey,
value
)
)
}}
/>
</Label>
)
}
export default function Settings(props: DialogProps) {
useEffect(() => {
if (window.__settingsOpened) {
throw new Error(
'Settings window was already open - this should not happen, please file a bug'
)
}
window.__settingsOpened = true
return () => {
window.__settingsOpened = false
}
})
const [state, _setState] = useState<{
showSettingsDialog: boolean
settings: any
show: string
selfContact: todo
}>({
showSettingsDialog: false,
settings: {},
show: 'main',
selfContact: {},
})
const setState = (updatedState: any) => {
_setState((prevState: any) => {
return { ...prevState, ...updatedState }
})
}
const setShow = (show: string) => setState({ show })
const { desktopSettings, setDesktopSetting } = useContext(SettingsContext)
const tx = useTranslationFunction()
const loadSettings = async () => {
const settings = await DeltaBackend.call('settings.getConfigFor', [
'inbox_watch',
'sentbox_watch',
'mvbox_watch',
'mvbox_move',
'e2ee_enabled',
'displayname',
'selfstatus',
'mdns_enabled',
'show_emails',
'bcc_self',
'delete_device_after',
'delete_server_after',
'webrtc_instance',
])
setState({ settings })
}
/*
* Saves settings for the Deltachat Desktop
* persisted in ~/.config/DeltaChat/deltachat.json
*/
const handleDesktopSettingsChange = async (
key: keyof DesktopSettings,
value: string | boolean | number
) => {
if (
(await DeltaBackend.call('settings.setDesktopSetting', key, value)) ===
true
) {
setDesktopSetting(key, value)
}
}
/** Saves settings to deltachat core */
const handleDeltaSettingsChange = async (
key: string,
value: string | boolean
) => {
if ((await DeltaBackend.call('settings.setConfig', key, value)) === true) {
_setState((settings: any) => {
return {
...settings,
settings: {
...settings.settings,
[key]: String(value),
},
}
})
return
}
log.warn('settings.setConfig returned false for: ', key, value)
}
/*
* render switch for Desktop Setings
*/
const renderDTSettingSwitch = (
configKey: keyof DesktopSettings,
label: string
) => {
return (
<Switch
checked={desktopSettings[configKey] === true}
className={desktopSettings[configKey] ? 'active' : 'inactive'}
label={label}
disabled={
configKey === 'showNotificationContent' &&
!desktopSettings['notifications']
}
onChange={() =>
handleDesktopSettingsChange(configKey, !desktopSettings[configKey])
}
alignIndicator='right'
/>
)
}
const renderDeltaSwitch = (configKey: string, label: string) => {
const configValue = state.settings[configKey]
return (
<Switch
checked={configValue === '1'}
className={configValue === '1' ? 'active' : 'inactive'}
label={label}
onChange={() =>
handleDeltaSettingsChange(configKey, flipDeltaBoolean(configValue))
}
alignIndicator='right'
/>
)
}
const renderDialogContent = () => {
const { account } = props
const { settings } = state
if (Object.keys(settings).length === 0) {
return null
}
if (state.show === 'main') {
return (
<>
<DeltaDialogBody>
<SettingsProfile
show={state.show}
setShow={setShow}
onClose={props.onClose}
account={account}
state={state}
/>
<Card elevation={Elevation.ONE}>
<SettingsCommunication
{...{
handleDeltaSettingsChange: handleDeltaSettingsChange,
settings,
}}
/>
<br />
<H5>{tx('pref_privacy')}</H5>
{renderDeltaSwitch('mdns_enabled', tx('pref_read_receipts'))}
<br />
<SettingsAutodelete
{...{
handleDeltaSettingsChange: handleDeltaSettingsChange,
settings,
}}
/>
</Card>
<SettingsAppearance
handleDesktopSettingsChange={handleDesktopSettingsChange}
/>
<SettingsEncryption renderDeltaSwitch={renderDeltaSwitch} />
<Card elevation={Elevation.ONE}>
<H5>{tx('pref_chats_and_media')}</H5>
{renderDTSettingSwitch(
'enterKeySends',
tx('pref_enter_sends_explain')
)}
{renderDTSettingSwitch(
'notifications',
tx('pref_notifications_explain')
)}
{renderDTSettingSwitch(
'showNotificationContent',
tx('pref_show_notification_content_explain')
)}
</Card>
<Card elevation={Elevation.ONE}>
<H5>{tx('pref_experimental_features')}</H5>
{renderDTSettingSwitch(
'enableOnDemandLocationStreaming',
tx('pref_on_demand_location_streaming')
)}
{renderDTSettingSwitch(
'minimizeToTray',
tx('pref_show_tray_icon')
)}
{renderDTSettingSwitch('enableAVCalls', tx('videochat'))}
{desktopSettings['enableAVCalls'] === true && (
<>
<DeltaSettingsInput
configKey='webrtc_instance'
label={tx('videochat_instance')}
style={{ width: '100%' }}
/>
<div className='bp3-callout'>
{tx('videochat_instance_explain')}
</div>
</>
)}
<br />
{renderDTSettingSwitch(
'disableFontLigatures',
tx('pref_disable_fontligatures')
)}
<H5>{tx('pref_imap_folder_handling')}</H5>
{renderDeltaSwitch('inbox_watch', tx('pref_watch_inbox_folder'))}
{renderDeltaSwitch('sentbox_watch', tx('pref_watch_sent_folder'))}
{renderDeltaSwitch('mvbox_watch', tx('pref_watch_mvbox_folder'))}
{renderDeltaSwitch('bcc_self', tx('pref_send_copy_to_self'))}
{renderDeltaSwitch('mvbox_move', tx('pref_auto_folder_moves'))}
</Card>
<SettingsManageKeys />
<SettingsBackup />
</DeltaDialogBody>
<DeltaDialogCloseFooter onClose={onClose} />
</>
)
} else if (state.show === 'edit-profile') {
return (
<SettingsEditProfile
show={state.show}
setShow={setShow}
onClose={props.onClose}
state={state}
handleDeltaSettingsChange={handleDeltaSettingsChange}
/>
)
} else if (state.show === 'login') {
return (
<SettingsAccount
show={state.show}
setShow={setShow}
onClose={props.onClose}
/>
)
} else {
throw new Error('Invalid state name: ' + state.show)
}
}
useEffect(() => {
;(async () => {
await loadSettings()
const selfContact = await DeltaBackend.call(
'contacts.getContact',
C.DC_CONTACT_ID_SELF
)
setState({ selfContact })
})()
return () => ipcRenderer.removeAllListeners('DC_EVENT_IMEX_FILE_WRITTEN')
}, [])
const { onClose } = props
let title
if (state.show === 'main') {
title = tx('menu_settings')
} else if (state.show === 'login') {
title = tx('pref_password_and_account_settings')
} else if (state.show === 'edit-profile') {
title = tx('pref_edit_profile')
}
return (
<DeltaDialogBase
isOpen={props.isOpen}
onClose={() => {
setState({ showSettingsDialog: false })
props.onClose()
}}
className='SettingsDialog'
fixed
>
<DeltaDialogHeader title={title} />
{renderDialogContent()}
</DeltaDialogBase>
)
}