From cd4e9077d53234f116116333731624262484f3ba Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 8 Apr 2022 20:59:20 +0200 Subject: [PATCH] Middle Header Menu: Support bot command buttons (#1811) --- src/components/middle/HeaderMenuContainer.tsx | 50 +++++++++++++++++-- src/components/middle/composer/Composer.tsx | 4 +- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/components/middle/HeaderMenuContainer.tsx b/src/components/middle/HeaderMenuContainer.tsx index 9bca59b0..61f13eb0 100644 --- a/src/components/middle/HeaderMenuContainer.tsx +++ b/src/components/middle/HeaderMenuContainer.tsx @@ -1,15 +1,16 @@ import React, { - FC, memo, useCallback, useEffect, useState, + FC, memo, useCallback, useEffect, useMemo, useState, } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; -import { ApiChat } from '../../api/types'; +import { ApiBotCommand, ApiChat } from '../../api/types'; import { IAnchorPosition } from '../../types'; +import { REPLIES_USER_ID } from '../../config'; import { IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment'; import { disableScrolling, enableScrolling } from '../../util/scrollLock'; import { - selectChat, selectNotifySettings, selectNotifyExceptions, selectUser, + selectChat, selectNotifySettings, selectNotifyExceptions, selectUser, selectChatBot, } from '../../global/selectors'; import { isUserId, getCanDeleteChat, selectIsChatMuted, getCanAddContact, @@ -24,6 +25,21 @@ import DeleteChatModal from '../common/DeleteChatModal'; import './HeaderMenuContainer.scss'; +const BOT_BUTTONS: Record = { + settings: { + icon: 'bots', + label: 'BotSettings', + }, + privacy: { + icon: 'info', + label: 'Privacy', + }, + help: { + icon: 'help', + label: 'BotHelp', + }, +}; + export type OwnProps = { chatId: string; threadId: number; @@ -49,6 +65,7 @@ export type OwnProps = { type StateProps = { chat?: ApiChat; + botCommands?: ApiBotCommand[]; isPrivate?: boolean; isMuted?: boolean; canAddContact?: boolean; @@ -62,6 +79,7 @@ const HeaderMenuContainer: FC = ({ withExtraActions, anchor, isChannel, + botCommands, canStartBot, canRestartBot, canSubscribe, @@ -187,6 +205,28 @@ const HeaderMenuContainer: FC = ({ const lang = useLang(); + const botButtons = useMemo(() => { + return botCommands?.map(({ command }) => { + const cmd = BOT_BUTTONS[command]; + if (!cmd) return undefined; + const handleClick = () => { + sendBotCommand({ command: `/${command}` }); + closeMenu(); + }; + + return ( + + {lang(cmd.label)} + + ); + }); + }, [botCommands, closeMenu, lang, sendBotCommand]); + return (
@@ -282,6 +322,7 @@ const HeaderMenuContainer: FC = ({ {lang('Statistics')} )} + {botButtons} {canLeave && ( ( const user = isPrivate ? selectUser(global, chatId) : undefined; const canAddContact = user && getCanAddContact(user); + const chatBot = chatId !== REPLIES_USER_ID ? selectChatBot(global, chatId) : undefined; + return { chat, isMuted: selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)), @@ -323,6 +366,7 @@ export default memo(withGlobal( canAddContact, canDeleteChat: getCanDeleteChat(chat), hasLinkedChat: Boolean(chat?.fullInfo?.linkedChatId), + botCommands: chatBot?.fullInfo?.botCommands, }; }, )(HeaderMenuContainer)); diff --git a/src/components/middle/composer/Composer.tsx b/src/components/middle/composer/Composer.tsx index 8250aeb4..0b4c4fbc 100644 --- a/src/components/middle/composer/Composer.tsx +++ b/src/components/middle/composer/Composer.tsx @@ -1251,8 +1251,8 @@ export default memo(withGlobal( emojiKeywords: emojiKeywords?.keywords, inlineBots: global.inlineBots.byUsername, isInlineBotLoading: global.inlineBots.isLoading, - chatBotCommands: chat && chat.fullInfo && chat.fullInfo.botCommands, - botCommands: chatBot && chatBot.fullInfo ? (chatBot.fullInfo.botCommands || false) : undefined, + chatBotCommands: chat?.fullInfo?.botCommands, + botCommands: chatBot?.fullInfo ? (chatBot.fullInfo.botCommands || false) : undefined, sendAsUser, sendAsChat, sendAsId,