-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathinstall.tsx
More file actions
852 lines (821 loc) · 28 KB
/
Copy pathinstall.tsx
File metadata and controls
852 lines (821 loc) · 28 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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
import * as C from '@/constants'
import * as ChatCommon from '@/constants/chat/common'
import * as Meta from '@/constants/chat/meta'
import * as Teams from '@/constants/teams'
import * as Kb from '@/common-adapters'
import * as React from 'react'
import {useModalHeaderState} from '@/stores/modal-header'
import ChannelPicker from './channel-picker'
import {useChatTeam} from '../team-hooks'
import {openURL} from '@/util/misc'
import * as T from '@/constants/types'
import {useAllChannelMetas} from '@/teams/common/channel-hooks'
import {useFeaturedBot} from '@/util/featured-bots'
import {RPCError} from '@/util/errors'
import logger from '@/logger'
import {useBotSettings} from './settings'
import {getInboxConversationMeta, metasReceived, participantInfoReceived} from '@/chat/inbox/metadata'
import {useConversationMeta} from '../data-hooks'
const RestrictedItem = '---RESTRICTED---'
export const useRefreshBotMembershipOnSuccess = (
conversationIDKey: T.Chat.ConversationIDKey | undefined,
waitingKey: string,
error: RPCError | undefined,
shouldRefreshMembership: boolean,
onSuccess: () => void
) => {
const waiting = C.Waiting.useAnyWaiting(waitingKey)
const wasWaitingRef = React.useRef(waiting)
const previewConversationByID = C.useRPC(T.RPCChat.localPreviewConversationByIDLocalRpcPromise)
React.useEffect(() => {
if (!waiting && wasWaitingRef.current && !error) {
if (!shouldRefreshMembership) {
onSuccess()
} else if (!conversationIDKey || !T.Chat.isValidConversationIDKey(conversationIDKey)) {
onSuccess()
} else {
previewConversationByID(
[{convID: T.Chat.keyToConversationID(conversationIDKey)}],
preview => {
participantInfoReceived(
conversationIDKey,
ChatCommon.uiParticipantsToParticipantInfo(preview.conv.participants ?? []),
getInboxConversationMeta(conversationIDKey)
)
onSuccess()
},
() => {
onSuccess()
}
)
}
}
wasWaitingRef.current = waiting
}, [
conversationIDKey,
error,
onSuccess,
previewConversationByID,
shouldRefreshMembership,
waiting,
])
}
export const useBotConversationIDKey = (inConvIDKey?: T.Chat.ConversationIDKey, teamID?: T.Teams.TeamID) => {
const cleanInConvIDKey = T.Chat.isValidConversationIDKey(inConvIDKey ?? '') ? inConvIDKey : undefined
const [generalConversation, setGeneralConversation] = React.useState<
| {
conversationIDKey: T.Chat.ConversationIDKey
teamID: T.Teams.TeamID
}
| undefined
>()
const findGeneralConvIDFromTeamID = C.useRPC(T.RPCChat.localFindGeneralConvFromTeamIDRpcPromise)
const requestIDRef = React.useRef(0)
const conversationIDKey =
cleanInConvIDKey ??
(generalConversation && generalConversation.teamID === teamID
? generalConversation.conversationIDKey
: undefined)
React.useEffect(() => {
requestIDRef.current += 1
if (cleanInConvIDKey || !teamID) {
return
}
const requestID = requestIDRef.current
findGeneralConvIDFromTeamID(
[{teamID}],
conv => {
if (requestIDRef.current !== requestID) {
return
}
const meta = Meta.inboxUIItemToConversationMeta(conv)
if (!meta) {
return
}
metasReceived([meta])
setGeneralConversation({conversationIDKey: meta.conversationIDKey, teamID})
},
() => {}
)
return () => {
if (requestIDRef.current === requestID) {
requestIDRef.current += 1
}
}
}, [cleanInConvIDKey, findGeneralConvIDFromTeamID, teamID])
return conversationIDKey
}
export const useBotTeamRole = (
conversationIDKey: T.Chat.ConversationIDKey | undefined,
botUsername: string
) => {
const [loaded, setLoaded] = React.useState<
| {
botUsername: string
conversationIDKey: T.Chat.ConversationIDKey
teamRole?: T.Teams.TeamRoleType
}
| undefined
>()
const loadBotTeamRole = C.useRPC(T.RPCChat.localGetTeamRoleInConversationRpcPromise)
const requestIDRef = React.useRef(0)
React.useEffect(() => {
requestIDRef.current += 1
if (!conversationIDKey) {
return undefined
}
const requestID = requestIDRef.current
loadBotTeamRole(
[{convID: T.Chat.keyToConversationID(conversationIDKey), username: botUsername}],
role => {
if (requestIDRef.current !== requestID) {
return
}
const teamRole = Teams.teamRoleByEnum[role]
setLoaded({
botUsername,
conversationIDKey,
teamRole: teamRole === 'none' ? undefined : teamRole,
})
},
error => {
if (requestIDRef.current !== requestID) {
return
}
logger.info(`useBotTeamRole: failed to refresh bot team role: ${error.message}`)
setLoaded({botUsername, conversationIDKey})
}
)
return () => {
if (requestIDRef.current === requestID) {
requestIDRef.current += 1
}
}
}, [botUsername, conversationIDKey, loadBotTeamRole])
return loaded && loaded.conversationIDKey === conversationIDKey && loaded.botUsername === botUsername
? loaded.teamRole
: undefined
}
type LoaderProps = {
botUsername: string
conversationIDKey?: T.Chat.ConversationIDKey
teamID?: T.Teams.TeamID
}
const InstallBotPopupLoader = (props: LoaderProps) => {
const botUsername = props.botUsername
const inConvIDKey = props.conversationIDKey
const teamID = props.teamID
const conversationIDKey = useBotConversationIDKey(inConvIDKey, teamID)
if (!conversationIDKey) return null
return <InstallBotPopup botUsername={botUsername} conversationIDKey={conversationIDKey} />
}
type Props = {
botUsername: string
conversationIDKey?: T.Chat.ConversationIDKey
}
const blankCommands: Array<T.RPCChat.ConversationCommand> = []
const addBotMember = async (p: {
botUsername: string
conversationIDKey: T.Chat.ConversationIDKey
installInConvs: ReadonlyArray<string>
installWithCommands: boolean
installWithMentions: boolean
installWithRestrict: boolean
}) => {
const {botUsername, conversationIDKey, installInConvs} = p
const {installWithCommands, installWithMentions, installWithRestrict} = p
try {
await T.RPCChat.localAddBotMemberRpcPromise(
{
botSettings: installWithRestrict
? {cmds: installWithCommands, convs: installInConvs, mentions: installWithMentions}
: null,
convID: T.Chat.keyToConversationID(conversationIDKey),
role: installWithRestrict ? T.RPCGen.TeamRole.restrictedbot : T.RPCGen.TeamRole.bot,
username: botUsername,
},
C.waitingKeyChatBotAdd
)
} catch (error) {
if (error instanceof RPCError) {
logger.info('addBotMember: failed to add bot member: ' + error.message)
}
}
}
const InstallBotPopup = (props: Props) => {
const {botUsername, conversationIDKey} = props
// state
const [installScreen, setInstallScreen] = React.useState(false)
const [channelPickerScreen, setChannelPickerScreen] = React.useState(false)
const [installWithCommands, setInstallWithCommands] = React.useState(true)
const [installWithMentions, setInstallWithMentions] = React.useState(true)
const [installWithRestrict, setInstallWithRestrict] = React.useState(true)
const [installInConvs, setInstallInConvs] = React.useState<ReadonlyArray<string>>([])
const [disableDone, setDisableDone] = React.useState(false)
const [loadedBotPublicCommands, setLoadedBotPublicCommands] = React.useState<
| {
botUsername: string
commands: T.Chat.BotPublicCommands
}
| undefined
>()
const meta = useConversationMeta(conversationIDKey ?? T.Chat.noConversationIDKey)
const commandsFromMeta = (
meta.botCommands.typ === T.RPCChat.ConversationCommandGroupsTyp.custom
? meta.botCommands.custom.commands || blankCommands
: blankCommands
)
.filter(c => c.username === botUsername)
.map(c => c.name)
const commands =
commandsFromMeta.length > 0
? ({commands: commandsFromMeta, loadError: false} satisfies T.Chat.BotPublicCommands)
: loadedBotPublicCommands?.botUsername === botUsername
? loadedBotPublicCommands.commands
: undefined
const featured = useFeaturedBot(botUsername)
const teamRole = useBotTeamRole(conversationIDKey, botUsername)
const inTeam = teamRole !== undefined ? !!teamRole : undefined
const inTeamUnrestricted = inTeam && teamRole === 'bot'
const isBot = teamRole === 'bot' || teamRole === 'restrictedbot' ? true : undefined
const {yourOperations} = useChatTeam(meta.teamID, meta.teamname)
const readOnly = meta.teamname ? !yourOperations.manageBots : false
const {settings} = useBotSettings(conversationIDKey, botUsername, !!inTeam)
let teamname: string | undefined
let teamID: T.Teams.TeamID = T.Teams.noTeamID
let refreshTeamID: T.Teams.TeamID | undefined
if (meta.teamname) {
teamID = meta.teamID
refreshTeamID = meta.teamID
teamname = meta.teamname
}
const {channelMetas} = useAllChannelMetas(teamID)
const mutationWaiting = C.Waiting.useAnyWaiting([C.waitingKeyChatBotAdd, C.waitingKeyChatBotRemove])
const error = C.Waiting.useAnyErrors([C.waitingKeyChatBotAdd, C.waitingKeyChatBotRemove])
const mutationError = C.Waiting.useAnyErrors(C.waitingKeyChatBotAdd)
// dispatch
const clearModals = C.Router2.clearModals
const navigateUp = C.Router2.navigateUp
const [pendingMutation, setPendingMutation] = React.useState<'add' | 'edit' | undefined>()
const onLearn = () => {
void openURL('https://book.keybase.io/docs/chat/restricted-bots')
}
const onInstall = () => {
if (!conversationIDKey) {
return
}
dispatchClearWaiting([C.waitingKeyChatBotAdd, C.waitingKeyChatBotRemove])
setPendingMutation('add')
C.ignorePromise(
addBotMember({
botUsername,
conversationIDKey,
installInConvs,
installWithCommands,
installWithMentions,
installWithRestrict,
})
)
}
const onEdit = () => {
if (!conversationIDKey) {
return
}
dispatchClearWaiting([C.waitingKeyChatBotAdd, C.waitingKeyChatBotRemove])
setPendingMutation('edit')
const f = async () => {
try {
await T.RPCChat.localSetBotMemberSettingsRpcPromise(
{
botSettings: {cmds: installWithCommands, convs: installInConvs, mentions: installWithMentions},
convID: T.Chat.keyToConversationID(conversationIDKey),
username: botUsername,
},
C.waitingKeyChatBotAdd
)
} catch (error) {
if (error instanceof RPCError) {
logger.info('addBotMember: failed to edit bot settings: ' + error.message)
}
}
}
C.ignorePromise(f())
}
const navigateAppend = C.Router2.navigateAppend
const onRemove = () => {
if (!conversationIDKey) {
return
}
navigateAppend({
name: 'chatConfirmRemoveBot',
params: {botUsername, conversationIDKey, teamID: refreshTeamID},
})
}
const onFeedback = () => {
navigateAppend({name: 'feedback', params: {}})
}
useRefreshBotMembershipOnSuccess(
conversationIDKey,
C.waitingKeyChatBotAdd,
mutationError,
pendingMutation !== undefined,
() => {
setPendingMutation(undefined)
clearModals()
}
)
const dispatchClearWaiting = C.Waiting.useDispatchClearWaiting()
const loadBotPublicCommands = C.useRPC(T.RPCChat.localListPublicBotCommandsLocalRpcPromise)
const botPublicCommandsRequestIDRef = React.useRef(0)
const clearedWaitingForBotRef = React.useRef<string | undefined>(undefined)
React.useEffect(() => {
if (!mutationWaiting && clearedWaitingForBotRef.current !== botUsername) {
clearedWaitingForBotRef.current = botUsername
dispatchClearWaiting([C.waitingKeyChatBotAdd, C.waitingKeyChatBotRemove])
}
}, [botUsername, dispatchClearWaiting, mutationWaiting])
React.useEffect(() => {
botPublicCommandsRequestIDRef.current += 1
if (commandsFromMeta.length > 0) {
return
}
const requestID = botPublicCommandsRequestIDRef.current
loadBotPublicCommands(
[{username: botUsername}],
res => {
if (botPublicCommandsRequestIDRef.current !== requestID) {
return
}
const commands = (res.commands ?? []).map(command => command.name)
setLoadedBotPublicCommands({botUsername, commands: {commands, loadError: false}})
},
() => {
if (botPublicCommandsRequestIDRef.current !== requestID) {
return
}
setLoadedBotPublicCommands({botUsername, commands: {commands: [], loadError: true}})
}
)
return () => {
if (botPublicCommandsRequestIDRef.current === requestID) {
botPublicCommandsRequestIDRef.current += 1
}
}
}, [botUsername, commandsFromMeta.length, loadBotPublicCommands])
const restrictedButton = (
<Kb.Box2 key={RestrictedItem} direction="vertical" fullWidth={true} style={styles.dropdownButton}>
<Kb.Text type="BodySemibold">Restricted bot (recommended)</Kb.Text>
<Kb.Text type="BodySmall">Customize which messages get encrypted for this bot.</Kb.Text>
</Kb.Box2>
)
const unrestrictedButton = (
<Kb.Box2 direction="vertical" fullWidth={true} style={styles.dropdownButton}>
<Kb.Text type="BodySemibold">Unrestricted bot</Kb.Text>
<Kb.Text type="BodySmall">All messages will be encrypted for this bot.</Kb.Text>
</Kb.Box2>
)
const dropdownButtons = [restrictedButton, unrestrictedButton]
const restrictPicker = !inTeam && !readOnly && (
<Kb.Dropdown
items={dropdownButtons}
selected={installWithRestrict ? restrictedButton : unrestrictedButton}
onChangedIdx={selected => setInstallWithRestrict(selected === 0)}
style={styles.dropdown}
overlayStyle={{width: '100%'}}
/>
)
const featuredContent = !!featured && (
<Kb.Box2 direction="vertical" style={styles.container} fullWidth={true} gap="small">
<Kb.Box2 direction="vertical" gap="small" fullWidth={true}>
<Kb.NameWithIcon
botAlias={featured.botAlias}
horizontal={true}
metaOne={featured.description}
onClick="profile"
username={botUsername}
size="big"
/>
<Kb.Markdown smallStandaloneEmoji={true} selectable={true}>
{featured.extendedDescription}
</Kb.Markdown>
</Kb.Box2>
{inTeam && isBot && !inTeamUnrestricted && (
<PermsList
channelMetas={channelMetas}
commands={commands}
settings={settings}
username={botUsername}
/>
)}
{!inTeam && (
<Kb.Text type="BodySmall">
<Kb.Text type="BodySmallPrimaryLink" onClick={onLearn}>
Learn more
</Kb.Text>{' '}
about bots in Keybase.
</Kb.Text>
)}
</Kb.Box2>
)
const usernameContent = !featured && (
<Kb.Box2 direction="vertical" gap="small" style={styles.container} fullWidth={true}>
<Kb.NameWithIcon horizontal={true} onClick="profile" username={botUsername} size="big" />
{inTeam && isBot && !inTeamUnrestricted && (
<PermsList
channelMetas={channelMetas}
settings={settings}
commands={commands}
username={botUsername}
/>
)}
</Kb.Box2>
)
const installContent = installScreen && (
<Kb.Box2 direction="vertical" fullWidth={true} style={styles.container} gap="small">
<Kb.NameWithIcon
botAlias={featured?.botAlias}
horizontal={true}
metaOne={featured?.description}
username={botUsername}
size="big"
/>
{installWithRestrict ? (
<Kb.Box2 direction="vertical" fullWidth={true} gap="small">
<Kb.Text type="BodyBig">It will be able to read:</Kb.Text>
<Kb.Box2 direction="vertical" fullWidth={true} gap="xtiny">
<Kb.Checkbox
checked={installWithCommands}
labelComponent={<CommandsLabel commands={commands} />}
onCheck={() => setInstallWithCommands(!installWithCommands)}
/>
<Kb.Checkbox
checked={installWithMentions}
labelComponent={
<Kb.Text
style={{flex: 1}}
type="Body"
>{`messages it has been mentioned in with @${botUsername}`}</Kb.Text>
}
onCheck={() => setInstallWithMentions(!installWithMentions)}
/>
</Kb.Box2>
{teamID && teamname && (
<Kb.Box2 direction="vertical" fullWidth={true} gap="tiny">
<Kb.Text type="BodyBig">In these channels:</Kb.Text>
<Kb.DropdownButton
selected={
<Kb.Box2 direction="horizontal" alignItems="center">
<Kb.Avatar
size={16}
teamname={teamname}
style={{marginRight: Kb.Styles.globalMargins.tiny}}
/>
<Kb.Text type="BodySemibold">
{teamname}{' '}
{installInConvs.length === 1
? `(#${channelMetas.get(installInConvs[0] ?? '')?.channelname ?? ''})`
: `(${installInConvs.length > 0 ? installInConvs.length : 'all'} channels)`}
</Kb.Text>
</Kb.Box2>
}
toggleOpen={() => setChannelPickerScreen(true)}
/>
</Kb.Box2>
)}
<Kb.Text type="BodySmall">
This bot will not be able to read any other messages, channels, files, or repositories.
</Kb.Text>
</Kb.Box2>
) : (
<Kb.Box2 direction="vertical" gap="tiny">
<Kb.Text type="Body">
<Kb.Text type="BodySemibold">Warning:</Kb.Text> This bot will be able to read all messages,
channels, files, and repositories.
</Kb.Text>
<Kb.Text type="Body">
<Kb.Text type="BodyPrimaryLink" onClick={() => setInstallWithRestrict(true)}>
Install as a restricted bot
</Kb.Text>
{' if you’d like to customize which messages are encrypted for this bot.'}
</Kb.Text>
</Kb.Box2>
)}
</Kb.Box2>
)
const channelPickerContent = channelPickerScreen && teamID && teamname && (
<Kb.Box2 direction="vertical" fullWidth={true} gap="small">
<ChannelPicker
channelMetas={channelMetas}
installInConvs={installInConvs}
setChannelPickerScreen={setChannelPickerScreen}
setDisableDone={setDisableDone}
setInstallInConvs={setInstallInConvs}
teamID={teamID}
teamName={teamname}
/>
</Kb.Box2>
)
const content = channelPickerScreen
? channelPickerContent
: installScreen
? installContent
: featured
? featuredContent
: usernameContent
const showInstallButton = installScreen && !inTeam && !channelPickerScreen
const showReviewButton = !installScreen && !inTeam
const showRemoveButton = inTeam && isBot && !installScreen
const showEditButton = inTeam && isBot && !inTeamUnrestricted && !installScreen
const showSaveButton = inTeam && installScreen && !channelPickerContent
const showDoneButton = channelPickerContent
const installButton = showInstallButton && (
<Kb.WaitingButton
fullWidth={true}
label="Install"
onClick={onInstall}
mode="Primary"
type="Default"
waitingKey={C.waitingKeyChatBotAdd}
/>
)
const reviewButton = showReviewButton && (
<Kb.Box2 direction="vertical" fullWidth={true} gap="tiny" style={styles.reviewButton}>
{readOnly ? (
<Kb.Text style={{alignSelf: 'center'}} type="BodySmall">
Ask an admin or owner to install this bot
</Kb.Text>
) : (
<Kb.Box2 direction="vertical" fullWidth={true} gap="tiny">
<Kb.Text type="BodySmall" style={{alignSelf: 'center'}}>
Install as
</Kb.Text>
{restrictPicker}
</Kb.Box2>
)}
<Kb.WaitingButton
fullWidth={true}
label="Review"
onClick={() => setInstallScreen(true)}
mode="Primary"
type="Default"
waitingKey={C.waitingKeyChatBotAdd}
disabled={readOnly}
/>
</Kb.Box2>
)
const removeButton = showRemoveButton && (
<Kb.WaitingButton
fullWidth={true}
label="Uninstall"
onClick={onRemove}
mode="Secondary"
type="Danger"
waitingKey={C.waitingKeyChatBotRemove}
/>
)
const editButton = showEditButton && (
<Kb.Button
fullWidth={true}
label="Edit settings"
onClick={() => {
if (settings) {
setInstallWithCommands(settings.cmds)
setInstallWithMentions(settings.mentions)
setInstallInConvs(settings.convs ?? [])
}
setInstallScreen(true)
}}
mode="Secondary"
type="Default"
/>
)
const saveButton = showSaveButton && (
<Kb.WaitingButton
fullWidth={true}
label="Save"
onClick={onEdit}
mode="Primary"
type="Default"
waitingKey={C.waitingKeyChatBotAdd}
/>
)
const doneButton = showDoneButton && (
<Kb.Button
fullWidth={true}
label={disableDone ? 'Select at least one channel' : 'Done'}
onClick={() => setChannelPickerScreen(false)}
disabled={disableDone}
mode="Primary"
type="Default"
/>
)
React.useEffect(() => {
const handleBack = () => {
if (channelPickerScreen) {
setChannelPickerScreen(false)
} else if (installScreen) {
setInstallScreen(false)
} else {
if (isMobile) {
navigateUp()
} else {
clearModals()
}
}
}
useModalHeaderState.setState({
botInTeam: !!inTeam,
botReadOnly: readOnly,
botSubScreen: channelPickerScreen ? 'channels' : installScreen ? 'install' : '',
onAction: handleBack,
title: channelPickerScreen ? 'Channels' : '',
})
return () => {
useModalHeaderState.setState({
botInTeam: false,
botReadOnly: false,
botSubScreen: '',
onAction: undefined,
title: '',
})
}
}, [channelPickerScreen, installScreen, inTeam, readOnly, navigateUp, clearModals])
const enabled = !!conversationIDKey
const bodyContent =
enabled && !channelPickerScreen ? (
<Kb.ScrollView style={styles.bodyScroll} contentContainerStyle={styles.bodyScrollContent}>
{content}
</Kb.ScrollView>
) : enabled ? (
content
) : (
<Kb.Box2 direction="vertical" fullHeight={true} fullWidth={true} centerChildren={true}>
<Kb.ProgressIndicator type="Large" />
</Kb.Box2>
)
return (
<>
<Kb.Box2 direction="vertical" flex={1} style={styles.outerContainer} fullWidth={true}>
{bodyContent}
</Kb.Box2>
{enabled && (!readOnly || showReviewButton) ? (
<Kb.Box2 direction="vertical" centerChildren={true} fullWidth={true} style={styles.modalFooter}>
<Kb.Box2 direction="horizontal" gap="tiny" fullWidth={true} centerChildren={true}>
<Kb.ButtonBar direction="column">
{doneButton}
{editButton}
{saveButton}
{reviewButton}
{installButton}
{removeButton}
</Kb.ButtonBar>
{!!error && (
<Kb.Text type="Body" style={{color: Kb.Styles.globalColors.redDark}}>
{'Something went wrong! Please try again, or send '}
<Kb.Text
type="Body"
style={{color: Kb.Styles.globalColors.redDark}}
underline={true}
onClick={onFeedback}
>
{'feedback'}
</Kb.Text>
</Kb.Text>
)}
</Kb.Box2>
</Kb.Box2>
) : null}
</>
)
}
type CommandsLabelProps = {
commands: T.Chat.BotPublicCommands | undefined
}
const maxCommandsShown = 3
const CommandsLabel = (props: CommandsLabelProps) => {
const [expanded, setExpanded] = React.useState(false)
let inner: React.ReactNode | undefined
if (!props.commands) {
inner = <Kb.ProgressIndicator />
} else if (props.commands.loadError) {
inner = (
<Kb.Text type="BodySemibold" style={{color: Kb.Styles.globalColors.redDark}}>
Error loading bot public commands.
</Kb.Text>
)
} else {
const numCommands = props.commands.commands.length
inner = props.commands.commands.map((c: string, i: number) => {
if (!expanded && i >= maxCommandsShown) {
return i === maxCommandsShown ? (
<Kb.Text key={i} type="Body">
{'• and '}
<Kb.Text
type="BodyPrimaryLink"
onClick={(e: React.BaseSyntheticEvent) => {
e.stopPropagation()
setExpanded(true)
}}
>{`${numCommands - maxCommandsShown} more`}</Kb.Text>
</Kb.Text>
) : null
}
return (
<Kb.Text key={i} type="Body">
{`• !${c}`}
</Kb.Text>
)
})
}
const punct = (props.commands?.commands.length ?? 0) > 0 ? ':' : '.'
return (
<Kb.Box2 direction="vertical" gap="tiny" fullWidth={true}>
<Kb.Text type="Body">{`messages that begin with bot commands${punct}`}</Kb.Text>
<Kb.Box2 direction="vertical" fullWidth={true}>
{inner}
</Kb.Box2>
</Kb.Box2>
)
}
type PermsListProps = {
channelMetas?: Map<T.Chat.ConversationIDKey, T.Chat.ConversationMeta>
commands: T.Chat.BotPublicCommands | undefined
settings?: T.RPCGen.TeamBotSettings
username: string
}
const PermsList = (props: PermsListProps) => {
return (
<Kb.Box2 direction="vertical" gap="small" fullWidth={true}>
<Kb.Text type="BodySemibold">This bot can currently read:</Kb.Text>
{props.settings ? (
<Kb.Box2 direction="vertical" gap="small" fullWidth={true}>
<Kb.Box2 direction="vertical" gap="tiny" fullWidth={true}>
{!(props.settings.cmds || props.settings.mentions) && (
<Kb.Text type="Body">{'• no messages, the bot is in write only mode'}</Kb.Text>
)}
{props.settings.cmds && (
<Kb.Box2 direction="horizontal" fullWidth={true} gap="xtiny">
<Kb.Text type="Body">{'•'}</Kb.Text>
<CommandsLabel commands={props.commands} />
</Kb.Box2>
)}
{props.settings.mentions && (
<Kb.Text type="Body">{`• messages it has been mentioned in with @${props.username}`}</Kb.Text>
)}
</Kb.Box2>
{props.settings.convs && props.channelMetas && (
<Kb.Box2 direction="vertical" gap="tiny" fullWidth={true}>
<Kb.Text type="BodySemibold">In these channels:</Kb.Text>
{props.settings.convs.map(convID => (
<Kb.Text type="Body" key={convID}>{`• #${
props.channelMetas?.get(convID)?.channelname ?? ''
}`}</Kb.Text>
))}
</Kb.Box2>
)}
</Kb.Box2>
) : (
<Kb.ProgressIndicator type="Large" />
)}
</Kb.Box2>
)
}
const styles = Kb.Styles.styleSheetCreate(() => ({
bodyScroll: {
flex: 1,
minHeight: 0,
position: 'relative',
},
bodyScrollContent: Kb.Styles.platformStyles({
common: {
...Kb.Styles.globalStyles.flexBoxColumn,
flexGrow: 1,
width: '100%',
},
}),
container: {
...Kb.Styles.padding(Kb.Styles.globalMargins.medium, Kb.Styles.globalMargins.small),
},
dropdown: {
width: '100%',
},
dropdownButton: {
padding: Kb.Styles.globalMargins.tiny,
},
modalFooter: Kb.Styles.platformStyles({
common: {
...Kb.Styles.padding(Kb.Styles.globalMargins.xsmall, Kb.Styles.globalMargins.small),
...Kb.Styles.topDivider(),
},
isElectron: {
...Kb.Styles.roundedBottom(),
},
}),
outerContainer: Kb.Styles.platformStyles({
common: {
minHeight: 0,
},
}),
reviewButton: {marginTop: -Kb.Styles.globalMargins.tiny},
}))
export default InstallBotPopupLoader