import type { FC } from '../../lib/teact/teact'; import React, { memo, useEffect, useRef } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import type { ApiSticker, ApiUpdateConnectionStateType } from '../../api/types'; import type { MessageList } from '../../global/types'; import { getPeerIdDividend } from '../../global/helpers'; import { selectChat, selectChatLastMessage, selectCurrentMessageList } from '../../global/selectors'; import useLang from '../../hooks/useLang'; import useLastCallback from '../../hooks/useLastCallback'; import StickerView from '../common/StickerView'; import './ContactGreeting.scss'; type OwnProps = { userId: string; }; type StateProps = { sticker?: ApiSticker; lastUnreadMessageId?: number; connectionState?: ApiUpdateConnectionStateType; currentMessageList?: MessageList; }; const ContactGreeting: FC = ({ sticker, connectionState, lastUnreadMessageId, currentMessageList, }) => { const { loadGreetingStickers, sendMessage, markMessageListRead, } = getActions(); const lang = useLang(); // eslint-disable-next-line no-null/no-null const containerRef = useRef(null); useEffect(() => { if (sticker || connectionState !== 'connectionStateReady') { return; } loadGreetingStickers(); }, [connectionState, loadGreetingStickers, sticker]); useEffect(() => { if (connectionState === 'connectionStateReady' && lastUnreadMessageId) { markMessageListRead({ maxId: lastUnreadMessageId }); } }, [connectionState, markMessageListRead, lastUnreadMessageId]); const handleStickerSelect = useLastCallback(() => { if (!currentMessageList) { return; } sendMessage({ sticker: { ...sticker!, isPreloadedGlobally: true, }, messageList: currentMessageList, }); }); return (

{lang('Conversation.EmptyPlaceholder')}

{lang('Conversation.GreetingText')}

{sticker && ( )}
); }; export default memo(withGlobal( (global, { userId }): StateProps => { const { stickers } = global.stickers.greeting; const dividend = getPeerIdDividend(userId) + getPeerIdDividend(global.currentUserId!); const sticker = stickers?.length ? stickers[dividend % stickers.length] : undefined; const chat = selectChat(global, userId); if (!chat) { return {}; } const lastMessage = selectChatLastMessage(global, chat.id); return { sticker, lastUnreadMessageId: lastMessage && lastMessage.id !== chat.lastReadInboxMessageId ? lastMessage.id : undefined, connectionState: global.connectionState, currentMessageList: selectCurrentMessageList(global), }; }, )(ContactGreeting));