import { Table } from 'antd'; import { ColumnsType } from 'antd/es/table/interface'; import React from 'react'; import { AppRouterOutput, trpc } from '../../api/trpc'; import { useCurrentWorkspaceId } from '../../store/user'; import { sum } from 'lodash-es'; import { formatNumber } from '../../utils/common'; type MetricsItemType = AppRouterOutput['website']['metrics'][number]; interface MetricsTableProps { websiteId: string; title: [string, string]; type: | 'url' | 'language' | 'referrer' | 'browser' | 'os' | 'device' | 'country' | 'event'; startAt: number; endAt: number; } export const MetricsTable: React.FC = React.memo((props) => { const workspaceId = useCurrentWorkspaceId()!; const { websiteId, title, type, startAt, endAt } = props; const { isLoading, data: metrics = [] } = trpc.website.metrics.useQuery({ workspaceId, websiteId, type, startAt, endAt, }); const total = sum(metrics.map((m) => m.y)); const columns: ColumnsType = [ { title: title[0], dataIndex: 'x', render: (val) => val ?? (None), }, { title: title[1], dataIndex: 'y', width: 100, align: 'center', render: (val) => { const percent = (Number(val) / total) * 100; return (
{formatNumber(val)}
{percent.toFixed(0)}%
); }, }, ]; return ( ); }); MetricsTable.displayName = 'MetricsTable';