import type { FC } from '../../lib/teact/teact'; import React, { memo, useCallback } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import { getPrivateChatUserId, getUserFirstOrLastName, isChatBasicGroup, isChatChannel, isChatSuperGroup, isUserId, } from '../../global/helpers'; import { selectChat, selectIsChatWithSelf, selectUser } from '../../global/selectors'; import renderText from './helpers/renderText'; import useLang from '../../hooks/useLang'; import Button from '../ui/Button'; import Modal from '../ui/Modal'; export type OwnProps = { isOpen: boolean; chatId: string; messageId: number; onClose: () => void; }; type StateProps = { isChannel: boolean; isPrivateChat: boolean; isChatWithSelf: boolean; isGroup: boolean; isSuperGroup: boolean; canPinForAll: boolean; contactName?: string; }; const PinMessageModal: FC = ({ isOpen, messageId, isChannel, isGroup, isSuperGroup, canPinForAll, contactName, onClose, }) => { const { pinMessage } = getActions(); const handlePinMessageForAll = useCallback(() => { pinMessage({ messageId, isUnpin: false, }); onClose(); }, [pinMessage, messageId, onClose]); const handlePinMessage = useCallback(() => { pinMessage({ messageId, isUnpin: false, isOneSide: true, isSilent: true, }); onClose(); }, [messageId, onClose, pinMessage]); const lang = useLang(); function renderMessage() { if (isChannel) { return lang('PinMessageAlertChannel'); } if (isGroup || isSuperGroup) { return lang('PinMessageAlert'); } return lang('PinMessageAlertChat'); } return (

{renderMessage()}

{canPinForAll && ( )}
); }; export default memo(withGlobal( (global, { chatId }): StateProps => { const isPrivateChat = isUserId(chatId); const isChatWithSelf = selectIsChatWithSelf(global, chatId); const chat = selectChat(global, chatId); const isChannel = Boolean(chat) && isChatChannel(chat); const isGroup = Boolean(chat) && isChatBasicGroup(chat); const isSuperGroup = Boolean(chat) && isChatSuperGroup(chat); const canPinForAll = (isPrivateChat && !isChatWithSelf) || isSuperGroup || isGroup; const contactName = chat && isUserId(chat.id) ? getUserFirstOrLastName(selectUser(global, getPrivateChatUserId(chat)!)) : undefined; return { isPrivateChat, isChatWithSelf, isChannel, isGroup, isSuperGroup, canPinForAll, contactName, }; }, )(PinMessageModal));