Repository navigation
Expand file tree
/
Copy pathchat.tsx
More file actions
614 lines (592 loc) · 22.8 KB
/
Copy pathchat.tsx
File metadata and controls
614 lines (592 loc) · 22.8 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
import * as C from '@/constants'
import * as Kb from '@/common-adapters'
import * as T from '@/constants/types'
import * as React from 'react'
import type {NotificationsGroupState} from '@/constants/settings-notifications'
import {Group} from './notifications/render'
const emptyList = new Array<string>()
const Container = () => {
const contactSettingsEnabled = C.useSettingsChatState(s => s.contactSettings.settings?.enabled)
const contactSettingsIndirectFollowees = C.useSettingsChatState(
s => s.contactSettings.settings?.allowFolloweeDegrees === 2
)
const contactSettingsTeams = C.useSettingsChatState(s => s.contactSettings.settings?.teams)
const contactSettingsTeamsEnabled = C.useSettingsChatState(s => s.contactSettings.settings?.allowGoodTeams)
const whitelist = C.useSettingsChatState(s => s.unfurl.unfurlWhitelist)
const unfurlWhitelist = whitelist ?? emptyList
const allowEdit = C.useSettingsNotifState(s => s.allowEdit)
const contactSettingsError = C.useSettingsChatState(s => s.contactSettings.error)
const groups = C.useSettingsNotifState(s => s.groups)
const mobileHasPermissions = C.usePushState(s => s.hasPermissions)
const sound = C.useConfigState(s => s.notifySound) // desktop
const _teamMeta = C.useTeamsState(s => s.teamMeta)
const unfurlError = C.useSettingsChatState(s => s.unfurl.unfurlError)
const unfurlMode = C.useSettingsChatState(s => s.unfurl.unfurlMode)
const contactSettingsSaved = C.useSettingsChatState(s => s.dispatch.contactSettingsSaved)
const contactSettingsRefresh = C.useSettingsChatState(s => s.dispatch.contactSettingsRefresh)
const unfurlSettingsRefresh = C.useSettingsChatState(s => s.dispatch.unfurlSettingsRefresh)
const unfurlSettingsSaved = C.useSettingsChatState(s => s.dispatch.unfurlSettingsSaved)
const notifRefresh = C.useSettingsNotifState(s => s.dispatch.refresh)
const notifToggle = C.useSettingsNotifState(s => s.dispatch.toggle)
const navigateUp = C.useRouterState(s => s.dispatch.navigateUp)
const onBack = C.isMobile
? () => {
navigateUp()
}
: undefined
const onContactSettingsSave = contactSettingsSaved
const loadSettings = C.useSettingsState(s => s.dispatch.loadSettings)
const onRefresh = () => {
// Security: misc
loadSettings()
notifRefresh()
// Security: contact settings
contactSettingsRefresh()
// Link previews
unfurlSettingsRefresh()
}
const onToggle = notifToggle
const onToggleSound = C.useConfigState(s => s.dispatch.setNotifySound)
const onUnfurlSave = unfurlSettingsSaved
const teamMeta = C.Teams.sortTeamsByName(_teamMeta)
const serverSelectedTeams = new Map(contactSettingsTeams?.map(t => [t.teamID, {enabled: t.enabled}]))
const selectedTeams: {[K in T.Teams.TeamID]: boolean} = {}
teamMeta.forEach(t => {
if (serverSelectedTeams.has(t.id)) {
// If there's a server-provided previous choice, use that.
selectedTeams[t.id] = !!serverSelectedTeams.get(t.id)?.enabled
} else {
// Else, default the team to being selected if the team is non-open.
selectedTeams[t.id] = !t.isOpen
}
})
const props = {
allowEdit,
contactSettingsEnabled,
contactSettingsError,
contactSettingsIndirectFollowees,
contactSettingsSelectedTeams: selectedTeams,
contactSettingsTeamsEnabled,
groups,
mobileHasPermissions,
onBack,
onContactSettingsSave,
onRefresh,
onToggle,
onToggleSound,
onUnfurlSave,
sound,
teamMeta,
unfurlError,
unfurlMode,
unfurlWhitelist,
}
return <Chat {...props} />
}
export type Props = {
allowEdit: boolean
contactSettingsEnabled?: boolean
contactSettingsError: string
contactSettingsIndirectFollowees?: boolean
contactSettingsTeamsEnabled?: boolean
contactSettingsSelectedTeams: {[K in T.Teams.TeamID]: boolean}
groups: T.Immutable<ReadonlyMap<string, NotificationsGroupState>>
sound: boolean
unfurlMode?: T.RPCChat.UnfurlMode
unfurlWhitelist?: ReadonlyArray<string>
unfurlError?: string
onBack?: () => void
onContactSettingsSave: (
enabled: boolean,
indirectFollowees: boolean,
teamsEnabled: boolean,
teamsList: {[k in T.Teams.TeamID]: boolean}
) => void
onToggle: (groupName: string, name: string) => void
onToggleSound: (notifySound: boolean) => void
onUnfurlSave: (mode: T.RPCChat.UnfurlMode, whitelist: ReadonlyArray<string>) => void
onRefresh: () => void
teamMeta: ReadonlyArray<T.Teams.TeamMeta>
}
type State = {
contactSettingsEnabled?: boolean
contactSettingsIndirectFollowees?: boolean
contactSettingsSelectedTeams: {[K in T.Teams.TeamID]: boolean}
contactSettingsTeamsEnabled?: boolean
unfurlSelected?: T.RPCChat.UnfurlMode
unfurlWhitelistRemoved: {[K in string]: boolean}
}
class Chat extends React.Component<Props, State> {
state: State = {
contactSettingsEnabled: undefined,
contactSettingsIndirectFollowees: undefined,
contactSettingsSelectedTeams: {},
contactSettingsTeamsEnabled: undefined,
unfurlSelected: undefined,
unfurlWhitelistRemoved: {},
}
_isUnfurlModeSelected() {
return this.state.unfurlSelected !== undefined && this.state.unfurlSelected !== this.props.unfurlMode
}
_isUnfurlWhitelistChanged() {
return (
Object.keys(this.state.unfurlWhitelistRemoved).filter(d => this.state.unfurlWhitelistRemoved[d])
.length > 0
)
}
_getUnfurlMode() {
const unfurlSelected = this.state.unfurlSelected
if (unfurlSelected !== undefined) {
return unfurlSelected
}
const unfurlMode = this.props.unfurlMode
if (unfurlMode !== undefined) {
return unfurlMode
}
return T.RPCChat.UnfurlMode.whitelisted
}
_getUnfurlWhitelist(filtered: boolean) {
return filtered
? (this.props.unfurlWhitelist || []).filter(w => !this.state.unfurlWhitelistRemoved[w])
: this.props.unfurlWhitelist || []
}
_setUnfurlMode(mode: T.RPCChat.UnfurlMode) {
this.setState({unfurlSelected: mode})
}
_toggleUnfurlWhitelist(domain: string) {
this.setState(s => ({
unfurlWhitelistRemoved: {
...s.unfurlWhitelistRemoved,
[domain]: !s.unfurlWhitelistRemoved[domain],
},
}))
}
_isUnfurlWhitelistRemoved(domain: string) {
return this.state.unfurlWhitelistRemoved[domain]
}
_isUnfurlSaveDisabled() {
return (
this.props.unfurlMode === undefined ||
(!this._isUnfurlModeSelected() && !this._isUnfurlWhitelistChanged())
)
}
componentDidMount() {
this.props.onRefresh()
}
componentDidUpdate(prevProps: Props) {
if (
this.props.contactSettingsEnabled !== prevProps.contactSettingsEnabled ||
(this.props.contactSettingsEnabled !== undefined && this.state.contactSettingsEnabled === undefined)
) {
this.setState({contactSettingsEnabled: this.props.contactSettingsEnabled})
}
if (
this.props.contactSettingsIndirectFollowees !== prevProps.contactSettingsIndirectFollowees ||
(this.props.contactSettingsIndirectFollowees !== undefined &&
this.state.contactSettingsIndirectFollowees === undefined)
) {
this.setState({contactSettingsIndirectFollowees: this.props.contactSettingsIndirectFollowees})
}
if (
this.props.contactSettingsTeamsEnabled !== prevProps.contactSettingsTeamsEnabled ||
(this.props.contactSettingsTeamsEnabled !== undefined &&
this.state.contactSettingsTeamsEnabled === undefined)
) {
this.setState({contactSettingsTeamsEnabled: this.props.contactSettingsTeamsEnabled})
}
// Create an initial copy of teams data into state, so it can be mutated there.
if (
Object.keys(this.props.contactSettingsSelectedTeams).length > 0 &&
Object.keys(this.state.contactSettingsSelectedTeams).length === 0
) {
this.setState({contactSettingsSelectedTeams: this.props.contactSettingsSelectedTeams})
}
}
render() {
const showDesktopSound = !C.isMobile && !C.isLinux
const showMisc = C.isMac || C.isIOS
const showMobileSound = !!this.props.groups.get('sound')?.settings.length
return (
<Kb.Box2 direction="vertical" fullWidth={true}>
<Kb.ScrollView>
<Kb.Box2 direction="vertical" fullHeight={true} gap="tiny" style={styles.container}>
<Kb.Box2 direction="vertical" fullWidth={true} style={styles.innerContainer}>
<Kb.Text type="Header">Security</Kb.Text>
</Kb.Box2>
<Kb.Box2 direction="vertical" fullWidth={true} style={styles.innerContainer}>
{!!this.props.groups.get('security')?.settings && (
<Group
allowEdit={this.props.allowEdit}
groupName="security"
onToggle={this.props.onToggle}
settings={this.props.groups.get('security')!.settings}
unsubscribedFromAll={false}
/>
)}
<Kb.Box2 direction="vertical" fullWidth={true}>
<Kb.Checkbox
label="Only let someone message you or add you to a team if..."
onCheck={() =>
this.setState(prevState => ({contactSettingsEnabled: !prevState.contactSettingsEnabled}))
}
checked={!!this.state.contactSettingsEnabled}
disabled={this.props.contactSettingsEnabled === undefined}
/>
{!!this.state.contactSettingsEnabled && (
<>
<Kb.Box2
direction="vertical"
fullWidth={true}
gap={Kb.Styles.isMobile ? 'small' : undefined}
gapStart={Kb.Styles.isMobile}
style={styles.checkboxIndented}
>
<Kb.Checkbox label="You follow them, or..." checked={true} disabled={true} />
<Kb.Checkbox
label="You follow someone who follows them, or..."
onCheck={checked =>
this.setState({
contactSettingsIndirectFollowees: checked,
})
}
checked={!!this.state.contactSettingsIndirectFollowees}
/>
<Kb.Checkbox
label="They're in one of these teams with you:"
onCheck={checked => this.setState({contactSettingsTeamsEnabled: checked})}
checked={!!this.state.contactSettingsTeamsEnabled}
disabled={false}
/>
</Kb.Box2>
{this.state.contactSettingsTeamsEnabled && (
<Kb.Box2
direction="vertical"
fullWidth={true}
gap={Kb.Styles.isMobile ? 'small' : undefined}
gapStart={false}
gapEnd={true}
>
{this.props.teamMeta.map(teamMeta => (
<TeamRow
checked={this.state.contactSettingsSelectedTeams[teamMeta.id] ?? false}
key={teamMeta.id}
isOpen={teamMeta.isOpen}
name={teamMeta.teamname}
onCheck={(checked: boolean) =>
this.setState(prevState => ({
contactSettingsSelectedTeams: {
...prevState.contactSettingsSelectedTeams,
[teamMeta.id]: checked,
},
}))
}
/>
))}
</Kb.Box2>
)}
</>
)}
<Kb.Box2 direction="vertical" gap="tiny" gapStart={true} style={styles.btnContainer}>
<Kb.WaitingButton
onClick={() =>
this.props.onContactSettingsSave(
!!this.state.contactSettingsEnabled,
!!this.state.contactSettingsIndirectFollowees,
!!this.state.contactSettingsTeamsEnabled,
this.state.contactSettingsSelectedTeams
)
}
label="Save"
small={true}
style={styles.save}
waitingKey={C.SettingsChat.contactSettingsSaveWaitingKey}
/>
{!!this.props.contactSettingsError && (
<Kb.Text type="BodySmall" style={styles.error}>
{this.props.contactSettingsError}
</Kb.Text>
)}
</Kb.Box2>
</Kb.Box2>
</Kb.Box2>
<Kb.Divider style={styles.divider} />
<Kb.Box2 direction="vertical" fullWidth={true} gap="xtiny" style={styles.innerContainer}>
<Kb.Text type="Header">Link previews</Kb.Text>
<Kb.Text type="BodySmall">
Your Keybase app will visit the links you share and automatically post previews.
</Kb.Text>
</Kb.Box2>
<Kb.Box2
direction="vertical"
fullWidth={true}
gap="xtiny"
gapStart={true}
style={styles.innerContainer}
>
<Kb.RadioButton
key="rbalways"
label="Always"
onSelect={() => this._setUnfurlMode(T.RPCChat.UnfurlMode.always)}
selected={this._getUnfurlMode() === T.RPCChat.UnfurlMode.always}
disabled={this.props.unfurlMode === undefined}
/>
<Kb.RadioButton
key="rbwhitelist"
label="Only for some websites"
onSelect={() => this._setUnfurlMode(T.RPCChat.UnfurlMode.whitelisted)}
selected={this._getUnfurlMode() === T.RPCChat.UnfurlMode.whitelisted}
disabled={this.props.unfurlMode === undefined}
/>
{this._getUnfurlMode() === T.RPCChat.UnfurlMode.whitelisted && (
<Kb.ScrollView style={styles.whitelist}>
{this._getUnfurlWhitelist(false).map((w, idx) => {
const wlremoved = this._isUnfurlWhitelistRemoved(w)
return (
<Kb.Box key={w} style={styles.whitelistInner}>
{idx === 0 && <Kb.Box style={styles.whitelistOuter} />}
<Kb.Box2
fullWidth={true}
direction="horizontal"
style={Kb.Styles.collapseStyles([
styles.whitelistRowContainer,
wlremoved ? {backgroundColor: Kb.Styles.globalColors.red_20} : undefined,
])}
>
<Kb.Text type="BodySemibold">{w}</Kb.Text>
{wlremoved ? (
<Kb.Text
type="BodyPrimaryLink"
style={styles.removeText}
onClick={() => this._toggleUnfurlWhitelist(w)}
>
Restore
</Kb.Text>
) : (
<Kb.Box style={{position: 'relative'}}>
<Kb.WithTooltip tooltip="Remove">
<Kb.Icon
onClick={() => this._toggleUnfurlWhitelist(w)}
style={styles.removeIcon}
type="iconfont-trash"
/>
</Kb.WithTooltip>
</Kb.Box>
)}
</Kb.Box2>
</Kb.Box>
)
})}
</Kb.ScrollView>
)}
<Kb.RadioButton
key="rbnever"
label="Never"
onSelect={() => this._setUnfurlMode(T.RPCChat.UnfurlMode.never)}
selected={this._getUnfurlMode() === T.RPCChat.UnfurlMode.never}
disabled={this.props.unfurlMode === undefined}
/>
</Kb.Box2>
<Kb.Box2
direction="vertical"
gap="tiny"
style={Kb.Styles.collapseStyles([styles.innerContainer, styles.btnContainer])}
>
<Kb.WaitingButton
onClick={() => this.props.onUnfurlSave(this._getUnfurlMode(), this._getUnfurlWhitelist(true))}
label="Save"
small={true}
style={styles.save}
disabled={this._isUnfurlSaveDisabled()}
waitingKey={C.SettingsChat.chatUnfurlWaitingKey}
/>
{this.props.unfurlError && (
<Kb.Text type="BodySmall" style={styles.error}>
{this.props.unfurlError}
</Kb.Text>
)}
</Kb.Box2>
{(showDesktopSound || showMobileSound) && (
<>
<Kb.Divider style={styles.divider} />
<Kb.Box2 direction="vertical" fullWidth={true} gap="tiny" style={styles.innerContainer}>
<Kb.Text type="Header">Sounds</Kb.Text>
<>
{showDesktopSound && (
<Kb.Checkbox
onCheck={this.props.onToggleSound}
checked={this.props.sound}
label="Play a sound for new messages"
/>
)}
{showMobileSound && (
<Group
allowEdit={this.props.allowEdit}
groupName="sound"
onToggle={this.props.onToggle}
settings={this.props.groups.get('sound')!.settings}
unsubscribedFromAll={false}
/>
)}
</>
</Kb.Box2>
</>
)}
{showMisc && (
<>
<Kb.Divider style={styles.divider} />
<Kb.Box2 direction="vertical" fullHeight={true} gap="tiny" style={styles.innerContainer}>
<Kb.Text type="Header">Misc</Kb.Text>
<>
{!!this.props.groups.get('misc')?.settings && (
<Group
allowEdit={this.props.allowEdit}
groupName="misc"
onToggle={this.props.onToggle}
settings={this.props.groups.get('misc')!.settings}
unsubscribedFromAll={false}
/>
)}
</>
</Kb.Box2>
</>
)}
</Kb.Box2>
</Kb.ScrollView>
</Kb.Box2>
)
}
}
const TeamRow = ({
checked,
isOpen,
name,
onCheck,
}: {
checked: boolean
isOpen: boolean
name: string
onCheck: (c: boolean) => void
}) => (
<Kb.Box2 direction="vertical" fullWidth={true}>
<Kb.Box2 direction="horizontal" fullWidth={true} style={styles.teamRowContainer}>
<Kb.Checkbox checked={checked} onCheck={checked => onCheck(checked)} style={styles.teamCheckbox} />
<Kb.Avatar isTeam={true} size={Kb.Styles.isMobile ? 32 : 24} teamname={name} />
<Kb.Box2 direction="vertical" fullWidth={true} style={styles.teamNameContainer}>
<Kb.Box2 direction="horizontal" fullWidth={true} style={styles.teamText}>
<Kb.Text type="BodySemibold" lineClamp={1}>
{name}
</Kb.Text>
{isOpen && (
<Kb.Meta title="open" style={styles.teamMeta} backgroundColor={Kb.Styles.globalColors.green} />
)}
</Kb.Box2>
</Kb.Box2>
</Kb.Box2>
</Kb.Box2>
)
const styles = Kb.Styles.styleSheetCreate(() => ({
btnContainer: {
alignSelf: 'flex-start',
},
checkboxIndented: Kb.Styles.platformStyles({
isElectron: {paddingLeft: Kb.Styles.globalMargins.medium},
isMobile: {paddingBottom: Kb.Styles.globalMargins.medium, paddingLeft: Kb.Styles.globalMargins.small},
}),
container: Kb.Styles.platformStyles({
common: {
paddingBottom: Kb.Styles.globalMargins.small,
paddingTop: Kb.Styles.globalMargins.small,
width: '100%',
},
}),
divider: {
marginBottom: Kb.Styles.globalMargins.small,
},
error: {
color: Kb.Styles.globalColors.redDark,
},
innerContainer: Kb.Styles.platformStyles({
common: {
paddingLeft: Kb.Styles.globalMargins.small,
paddingRight: Kb.Styles.globalMargins.small,
},
isElectron: {
maxWidth: 600,
},
}),
removeIcon: Kb.Styles.platformStyles({
isElectron: {
position: 'absolute',
right: 0,
top: 4,
},
}),
removeText: {
color: Kb.Styles.globalColors.black,
},
save: {
marginBottom: Kb.Styles.globalMargins.small,
marginTop: Kb.Styles.globalMargins.tiny,
},
teamCheckbox: Kb.Styles.platformStyles({
isElectron: {alignSelf: 'center', marginRight: Kb.Styles.globalMargins.tiny},
isMobile: {marginRight: Kb.Styles.globalMargins.medium},
}),
teamMeta: {
alignSelf: 'center',
marginLeft: Kb.Styles.globalMargins.xtiny,
marginTop: 2,
},
teamNameContainer: {
alignSelf: 'center',
flexShrink: 1,
marginLeft: Kb.Styles.globalMargins.tiny,
marginRight: Kb.Styles.globalMargins.small,
},
teamRowContainer: {
paddingBottom: Kb.Styles.globalMargins.xtiny,
paddingLeft: Kb.Styles.isMobile ? Kb.Styles.globalMargins.large : 48,
paddingRight: Kb.Styles.globalMargins.small,
paddingTop: Kb.Styles.globalMargins.xtiny,
},
teamText: {
alignSelf: 'flex-start',
},
whitelist: Kb.Styles.platformStyles({
common: {
alignSelf: 'flex-start',
backgroundColor: Kb.Styles.globalColors.blueGrey,
marginBottom: Kb.Styles.globalMargins.xtiny,
marginLeft: 22,
marginTop: Kb.Styles.globalMargins.xtiny,
paddingRight: Kb.Styles.globalMargins.medium,
},
isElectron: {
height: 150,
width: '100%',
},
isMobile: {
width: '95%',
},
}),
whitelistInner: {
marginBottom: 1,
paddingRight: Kb.Styles.globalMargins.tiny,
},
whitelistOuter: {
marginBottom: Kb.Styles.globalMargins.tiny,
marginTop: Kb.Styles.globalMargins.tiny,
},
whitelistRowContainer: Kb.Styles.platformStyles({
common: {
backgroundColor: Kb.Styles.globalColors.white,
flexShrink: 0,
height: 40,
justifyContent: 'space-between',
marginLeft: Kb.Styles.globalMargins.tiny,
padding: Kb.Styles.globalMargins.tiny,
paddingRight: Kb.Styles.globalMargins.small,
},
}),
}))
export default Container