2023-10-06 14:08:15 +00:00
|
|
|
import { Table } from 'antd';
|
|
|
|
import { ColumnsType } from 'antd/es/table/interface';
|
|
|
|
import React from 'react';
|
2023-10-16 14:34:01 +00:00
|
|
|
import { AppRouterOutput, trpc } from '../../api/trpc';
|
2023-10-06 14:08:15 +00:00
|
|
|
import { useCurrentWorkspaceId } from '../../store/user';
|
2023-10-07 09:28:30 +00:00
|
|
|
import { sum } from 'lodash-es';
|
2023-10-30 14:14:40 +00:00
|
|
|
import { formatNumber } from '../../utils/common';
|
2023-10-06 14:08:15 +00:00
|
|
|
|
2023-10-16 14:34:01 +00:00
|
|
|
type MetricsItemType = AppRouterOutput['website']['metrics'][number];
|
2023-10-07 11:32:46 +00:00
|
|
|
|
2023-10-06 14:08:15 +00:00
|
|
|
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<MetricsTableProps> = 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,
|
|
|
|
});
|
|
|
|
|
2023-10-07 09:28:30 +00:00
|
|
|
const total = sum(metrics.map((m) => m.y));
|
|
|
|
|
2023-10-07 11:32:46 +00:00
|
|
|
const columns: ColumnsType<MetricsItemType> = [
|
2023-10-06 14:08:15 +00:00
|
|
|
{
|
|
|
|
title: title[0],
|
|
|
|
dataIndex: 'x',
|
2023-10-07 11:32:46 +00:00
|
|
|
render: (val) => val ?? <span className="italic">(None)</span>,
|
2023-10-06 14:08:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: title[1],
|
|
|
|
dataIndex: 'y',
|
|
|
|
width: 100,
|
2023-10-07 09:28:30 +00:00
|
|
|
align: 'center',
|
|
|
|
render: (val) => {
|
|
|
|
const percent = (Number(val) / total) * 100;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flex">
|
2023-10-30 14:14:40 +00:00
|
|
|
<div className="w-12 text-right">{formatNumber(val)}</div>
|
2023-10-07 11:32:46 +00:00
|
|
|
<div className="inline-block w-12 relative border-l ml-1 px-1">
|
2023-10-07 09:28:30 +00:00
|
|
|
<div
|
|
|
|
className="bg-blue-300 absolute h-full bg-opacity-25 left-0 top-0 pointer-events-none"
|
|
|
|
style={{ width: `${percent}%` }}
|
|
|
|
/>
|
|
|
|
<span>{percent.toFixed(0)}%</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2023-10-06 14:08:15 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
rowKey="x"
|
|
|
|
loading={isLoading}
|
|
|
|
dataSource={metrics}
|
|
|
|
columns={columns}
|
2023-10-07 09:28:30 +00:00
|
|
|
pagination={{
|
|
|
|
pageSize: 10,
|
|
|
|
hideOnSinglePage: true,
|
|
|
|
}}
|
2023-10-06 14:08:15 +00:00
|
|
|
size="small"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
MetricsTable.displayName = 'MetricsTable';
|