import type { FC } from '../../../lib/teact/teact'; import React, { memo } from '../../../lib/teact/teact'; import type { ApiChannelStatistics, ApiGroupStatistics, ApiMessageStatistics, StatisticsOverviewItem, } from '../../../api/types'; import { formatInteger, formatIntegerCompact } from '../../../util/textFormat'; import { formatFullDate } from '../../../util/dateFormat'; import buildClassName from '../../../util/buildClassName'; import useLang from '../../../hooks/useLang'; import './StatisticsOverview.scss'; type OverviewCell = { name: string; title: string; isPercentage?: boolean; isPlain?: boolean; isApproximate?: boolean; }; const CHANNEL_OVERVIEW: OverviewCell[][] = [ [ { name: 'followers', title: 'ChannelStats.Overview.Followers' }, { name: 'enabledNotifications', title: 'ChannelStats.Overview.EnabledNotifications', isPercentage: true }, ], [ { name: 'viewsPerPost', title: 'ChannelStats.Overview.ViewsPerPost' }, { name: 'sharesPerPost', title: 'ChannelStats.Overview.SharesPerPost' }, ], ]; const GROUP_OVERVIEW: OverviewCell[][] = [ [ { name: 'members', title: 'Stats.GroupMembers' }, { name: 'messages', title: 'Stats.GroupMessages' }, ], [ { name: 'viewers', title: 'Stats.GroupViewers' }, { name: 'posters', title: 'Stats.GroupPosters' }, ], ]; const MESSAGE_OVERVIEW: OverviewCell[][] = [ [ { name: 'views', title: 'Stats.Message.Views', isPlain: true }, { name: 'forwards', title: 'Stats.Message.PrivateShares', isPlain: true, isApproximate: true, }, ], [ { name: 'publicForwards', title: 'Stats.Message.PublicShares', isPlain: true }, ], ]; export type OwnProps = { isGroup?: boolean; isMessage?: boolean; statistics: ApiChannelStatistics | ApiGroupStatistics | ApiMessageStatistics; }; const StatisticsOverview: FC = ({ isGroup, isMessage, statistics }) => { const lang = useLang(); const renderOverviewItemValue = ({ change, percentage }: StatisticsOverviewItem) => { if (!change) { return undefined; } const isChangeNegative = Number(change) < 0; return ( {isChangeNegative ? `-${formatIntegerCompact(Math.abs(change))}` : `+${formatIntegerCompact(change)}`} {percentage && ( <> {' '} ({percentage}%) )} ); }; const { period } = (statistics as ApiGroupStatistics); return (
{lang('StatisticOverview')}
{period && (
{formatFullDate(lang, period.minDate * 1000)} — {formatFullDate(lang, period.maxDate * 1000)}
)}
{(isMessage ? MESSAGE_OVERVIEW : isGroup ? GROUP_OVERVIEW : CHANNEL_OVERVIEW).map((row) => ( {row.map((cell: OverviewCell) => { const field = (statistics as any)[cell.name]; if (cell.isPlain) { return ( ); } if (cell.isPercentage) { return ( ); } return ( ); })} ))}
{cell.isApproximate ? `≈${formatInteger(field)}` : formatInteger(field)}

{lang(cell.title)}

{field.percentage}%

{lang(cell.title)}

{formatIntegerCompact(field.current)} {' '} {renderOverviewItemValue(field)}

{lang(cell.title)}

); }; export default memo(StatisticsOverview);