Middle Header Menu: Support bot command buttons (#1811)
This commit is contained in:
parent
b3a87f6b12
commit
cd4e9077d5
@ -1,15 +1,16 @@
|
|||||||
import React, {
|
import React, {
|
||||||
FC, memo, useCallback, useEffect, useState,
|
FC, memo, useCallback, useEffect, useMemo, useState,
|
||||||
} from '../../lib/teact/teact';
|
} from '../../lib/teact/teact';
|
||||||
import { getActions, withGlobal } from '../../global';
|
import { getActions, withGlobal } from '../../global';
|
||||||
|
|
||||||
import { ApiChat } from '../../api/types';
|
import { ApiBotCommand, ApiChat } from '../../api/types';
|
||||||
import { IAnchorPosition } from '../../types';
|
import { IAnchorPosition } from '../../types';
|
||||||
|
|
||||||
|
import { REPLIES_USER_ID } from '../../config';
|
||||||
import { IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment';
|
import { IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment';
|
||||||
import { disableScrolling, enableScrolling } from '../../util/scrollLock';
|
import { disableScrolling, enableScrolling } from '../../util/scrollLock';
|
||||||
import {
|
import {
|
||||||
selectChat, selectNotifySettings, selectNotifyExceptions, selectUser,
|
selectChat, selectNotifySettings, selectNotifyExceptions, selectUser, selectChatBot,
|
||||||
} from '../../global/selectors';
|
} from '../../global/selectors';
|
||||||
import {
|
import {
|
||||||
isUserId, getCanDeleteChat, selectIsChatMuted, getCanAddContact,
|
isUserId, getCanDeleteChat, selectIsChatMuted, getCanAddContact,
|
||||||
@ -24,6 +25,21 @@ import DeleteChatModal from '../common/DeleteChatModal';
|
|||||||
|
|
||||||
import './HeaderMenuContainer.scss';
|
import './HeaderMenuContainer.scss';
|
||||||
|
|
||||||
|
const BOT_BUTTONS: Record<string, { icon: string; label: string }> = {
|
||||||
|
settings: {
|
||||||
|
icon: 'bots',
|
||||||
|
label: 'BotSettings',
|
||||||
|
},
|
||||||
|
privacy: {
|
||||||
|
icon: 'info',
|
||||||
|
label: 'Privacy',
|
||||||
|
},
|
||||||
|
help: {
|
||||||
|
icon: 'help',
|
||||||
|
label: 'BotHelp',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
export type OwnProps = {
|
export type OwnProps = {
|
||||||
chatId: string;
|
chatId: string;
|
||||||
threadId: number;
|
threadId: number;
|
||||||
@ -49,6 +65,7 @@ export type OwnProps = {
|
|||||||
|
|
||||||
type StateProps = {
|
type StateProps = {
|
||||||
chat?: ApiChat;
|
chat?: ApiChat;
|
||||||
|
botCommands?: ApiBotCommand[];
|
||||||
isPrivate?: boolean;
|
isPrivate?: boolean;
|
||||||
isMuted?: boolean;
|
isMuted?: boolean;
|
||||||
canAddContact?: boolean;
|
canAddContact?: boolean;
|
||||||
@ -62,6 +79,7 @@ const HeaderMenuContainer: FC<OwnProps & StateProps> = ({
|
|||||||
withExtraActions,
|
withExtraActions,
|
||||||
anchor,
|
anchor,
|
||||||
isChannel,
|
isChannel,
|
||||||
|
botCommands,
|
||||||
canStartBot,
|
canStartBot,
|
||||||
canRestartBot,
|
canRestartBot,
|
||||||
canSubscribe,
|
canSubscribe,
|
||||||
@ -187,6 +205,28 @@ const HeaderMenuContainer: FC<OwnProps & StateProps> = ({
|
|||||||
|
|
||||||
const lang = useLang();
|
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 (
|
||||||
|
<MenuItem
|
||||||
|
key={command}
|
||||||
|
icon={cmd.icon}
|
||||||
|
// eslint-disable-next-line react/jsx-no-bind
|
||||||
|
onClick={handleClick}
|
||||||
|
>
|
||||||
|
{lang(cmd.label)}
|
||||||
|
</MenuItem>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}, [botCommands, closeMenu, lang, sendBotCommand]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Portal>
|
<Portal>
|
||||||
<div className="HeaderMenuContainer">
|
<div className="HeaderMenuContainer">
|
||||||
@ -282,6 +322,7 @@ const HeaderMenuContainer: FC<OwnProps & StateProps> = ({
|
|||||||
{lang('Statistics')}
|
{lang('Statistics')}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
)}
|
)}
|
||||||
|
{botButtons}
|
||||||
{canLeave && (
|
{canLeave && (
|
||||||
<MenuItem
|
<MenuItem
|
||||||
destructive
|
destructive
|
||||||
@ -316,6 +357,8 @@ export default memo(withGlobal<OwnProps>(
|
|||||||
const user = isPrivate ? selectUser(global, chatId) : undefined;
|
const user = isPrivate ? selectUser(global, chatId) : undefined;
|
||||||
const canAddContact = user && getCanAddContact(user);
|
const canAddContact = user && getCanAddContact(user);
|
||||||
|
|
||||||
|
const chatBot = chatId !== REPLIES_USER_ID ? selectChatBot(global, chatId) : undefined;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
chat,
|
chat,
|
||||||
isMuted: selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)),
|
isMuted: selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)),
|
||||||
@ -323,6 +366,7 @@ export default memo(withGlobal<OwnProps>(
|
|||||||
canAddContact,
|
canAddContact,
|
||||||
canDeleteChat: getCanDeleteChat(chat),
|
canDeleteChat: getCanDeleteChat(chat),
|
||||||
hasLinkedChat: Boolean(chat?.fullInfo?.linkedChatId),
|
hasLinkedChat: Boolean(chat?.fullInfo?.linkedChatId),
|
||||||
|
botCommands: chatBot?.fullInfo?.botCommands,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
)(HeaderMenuContainer));
|
)(HeaderMenuContainer));
|
||||||
|
|||||||
@ -1251,8 +1251,8 @@ export default memo(withGlobal<OwnProps>(
|
|||||||
emojiKeywords: emojiKeywords?.keywords,
|
emojiKeywords: emojiKeywords?.keywords,
|
||||||
inlineBots: global.inlineBots.byUsername,
|
inlineBots: global.inlineBots.byUsername,
|
||||||
isInlineBotLoading: global.inlineBots.isLoading,
|
isInlineBotLoading: global.inlineBots.isLoading,
|
||||||
chatBotCommands: chat && chat.fullInfo && chat.fullInfo.botCommands,
|
chatBotCommands: chat?.fullInfo?.botCommands,
|
||||||
botCommands: chatBot && chatBot.fullInfo ? (chatBot.fullInfo.botCommands || false) : undefined,
|
botCommands: chatBot?.fullInfo ? (chatBot.fullInfo.botCommands || false) : undefined,
|
||||||
sendAsUser,
|
sendAsUser,
|
||||||
sendAsChat,
|
sendAsChat,
|
||||||
sendAsId,
|
sendAsId,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user