Skip to content

Commit 5fcff43

Browse files
authored
Quick add buttons for bots (keybase#22627)
* feat: make quick add buttons for bot info panel * fix: add missing react hook dep
1 parent 66a9628 commit 5fcff43

1 file changed

Lines changed: 70 additions & 4 deletions

File tree

  • shared/chat/conversation/info-panel

‎shared/chat/conversation/info-panel/bot.tsx‎

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,65 @@ import * as RPCTypes from '../../../constants/types/rpc-gen'
1010
import * as Chat2Gen from '../../../actions/chat2-gen'
1111
import * as Constants from '../../../constants/chat2'
1212

13+
type AddToChannelProps = {
14+
conversationIDKey: Types.ConversationIDKey
15+
username: string
16+
}
17+
18+
const AddToChannel = (props: AddToChannelProps) => {
19+
const {conversationIDKey, username} = props
20+
const dispatch = Container.useDispatch()
21+
const settings = Container.useSelector(
22+
state => state.chat2.botSettings.get(conversationIDKey)?.get(username) ?? undefined
23+
)
24+
return (
25+
<Kb.WaitingButton
26+
disabled={!settings}
27+
type="Dim"
28+
mode="Secondary"
29+
icon="iconfont-new"
30+
tooltip="Add to this channel"
31+
onClick={e => {
32+
e.preventDefault()
33+
// if settings aren't loaded, don't even try to do anything
34+
if (settings && !settings.convs?.includes(conversationIDKey)) {
35+
dispatch(
36+
Chat2Gen.createEditBotSettings({
37+
allowCommands: settings.cmds,
38+
allowMentions: settings.mentions,
39+
conversationIDKey,
40+
convs: [conversationIDKey].concat(settings.convs ?? []),
41+
username,
42+
})
43+
)
44+
}
45+
}}
46+
waitingKey={Constants.waitingKeyBotAdd}
47+
/>
48+
)
49+
}
50+
1351
type BotProps = RPCTypes.FeaturedBot & {
1452
description?: string
1553
firstItem?: boolean
54+
showChannelAdd?: boolean
55+
showTeamAdd?: boolean
56+
conversationIDKey?: Types.ConversationIDKey
1657
onClick: (username: string) => void
1758
}
18-
1959
export const Bot = (props: BotProps) => {
2060
const {botAlias, description, botUsername} = props
2161
const {ownerTeam, ownerUser} = props
2262
const {onClick, firstItem} = props
63+
const {conversationIDKey, showChannelAdd, showTeamAdd} = props
64+
const dispatch = Container.useDispatch()
65+
React.useEffect(() => {
66+
if (conversationIDKey && showChannelAdd) {
67+
// fetch bot settings if trying to show the add to channel button
68+
dispatch(Chat2Gen.createRefreshBotSettings({conversationIDKey, username: botUsername}))
69+
}
70+
}, [dispatch, botUsername, conversationIDKey, showChannelAdd])
71+
2372
const lower = (
2473
<Kb.Box2
2574
direction="horizontal"
@@ -63,6 +112,13 @@ export const Bot = (props: BotProps) => {
63112
type="Large"
64113
firstItem={!!firstItem}
65114
icon={<Kb.Avatar size={Styles.isMobile ? 48 : 32} style={styles.avatarStyle} username={botUsername} />}
115+
action={
116+
showTeamAdd ? (
117+
<Kb.Button type="Dim" mode="Secondary" icon="iconfont-new" tooltip="Add to this team" />
118+
) : showChannelAdd && conversationIDKey ? (
119+
<AddToChannel conversationIDKey={conversationIDKey} username={botUsername} />
120+
) : null
121+
}
66122
body={
67123
<Kb.Box2 direction="vertical" style={styles.container}>
68124
{usernameDisplay}
@@ -200,9 +256,10 @@ export default (props: Props) => {
200256
}))
201257

202258
// bots in conv
203-
const botsInConv = teamType === 'big' ? botUsernames.filter(b => participantsAll.includes(b)) : botUsernames
259+
const botsInConv: string[] =
260+
teamType === 'big' ? botUsernames.filter(b => participantsAll.includes(b)) : botUsernames
204261

205-
const botsInTeam = botUsernames.filter(b => !botsInConv.includes(b))
262+
const botsInTeam: string[] = botUsernames.filter(b => !botsInConv.includes(b))
206263

207264
const onBotAdd = () => {
208265
dispatch(
@@ -296,7 +353,16 @@ export default (props: Props) => {
296353
if (!item.botUsername) {
297354
return null
298355
} else {
299-
return <Bot {...item} firstItem={item.index === 0} onClick={onBotSelect} />
356+
return (
357+
<Bot
358+
{...item}
359+
conversationIDKey={conversationIDKey}
360+
firstItem={item.index === 0}
361+
onClick={onBotSelect}
362+
showChannelAdd={canManageBots && teamType === 'big' && botsInTeam.includes(item.botUsername)}
363+
showTeamAdd={canManageBots && featuredBots.find(bot => bot.botUsername === item.botUsername)}
364+
/>
365+
)
300366
}
301367
},
302368
renderSectionHeader: renderTabs,

0 commit comments

Comments
 (0)