import type { FC } from '../../../lib/teact/teact'; import React, { memo, useMemo } from '../../../lib/teact/teact'; import { getActions, getGlobal } from '../../../global'; import type { ApiCommentsInfo } from '../../../api/types'; import { selectPeer } from '../../../global/selectors'; import buildClassName from '../../../util/buildClassName'; import { formatIntegerCompact } from '../../../util/textFormat'; import useLang from '../../../hooks/useLang'; import useLastCallback from '../../../hooks/useLastCallback'; import useAsyncRendering from '../../right/hooks/useAsyncRendering'; import AnimatedCounter from '../../common/AnimatedCounter'; import Avatar from '../../common/Avatar'; import Spinner from '../../ui/Spinner'; import './CommentButton.scss'; type OwnProps = { threadInfo: ApiCommentsInfo; disabled?: boolean; isLoading?: boolean; isCustomShape?: boolean; }; const SHOW_LOADER_DELAY = 450; const CommentButton: FC = ({ isCustomShape, threadInfo, disabled, isLoading, }) => { const { openThread } = getActions(); const shouldRenderLoading = useAsyncRendering([isLoading], SHOW_LOADER_DELAY); const lang = useLang(); const { originMessageId, chatId, messagesCount, lastMessageId, lastReadInboxMessageId, recentReplierIds, originChannelId, } = threadInfo; const handleClick = useLastCallback(() => { openThread({ isComments: true, chatId, originMessageId, originChannelId, }); }); const recentRepliers = useMemo(() => { if (!recentReplierIds?.length) { return undefined; } // No need for expensive global updates on chats and users, so we avoid them const global = getGlobal(); return recentReplierIds.map((peerId) => { return selectPeer(global, peerId); }).filter(Boolean); }, [recentReplierIds]); if (messagesCount === undefined) { return undefined; } function renderRecentRepliers() { return ( Boolean(recentRepliers?.length) && (
{recentRepliers!.map((peer) => ( ))}
) ); } const hasUnread = Boolean(lastReadInboxMessageId && lastMessageId && lastReadInboxMessageId < lastMessageId); const commentsText = messagesCount ? (lang('CommentsCount', '%COMMENTS_COUNT%', undefined, messagesCount) as string) .split('%') .map((s) => { return (s === 'COMMENTS_COUNT' ? : s); }) : undefined; return (
{!recentRepliers?.length && } {renderRecentRepliers()}
{messagesCount ? commentsText : lang('LeaveAComment')}
{isLoading && ( ) }
); }; export default memo(CommentButton);