From 1306187f01e91d400f228bcd3f4c7ed4ad8576d4 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 3 Mar 2024 19:02:35 +0800 Subject: [PATCH] refactor: rename MetricsTable to WebsiteMetricsTable --- .../dashboard/items/WebsiteEventItem.tsx | 4 +- .../components/website/MetricsTable.tsx | 88 ------------------ .../website/WebsiteMetricsTable.tsx | 90 +++++++++++++++++++ src/client/pages/Website/Detail.tsx | 16 ++-- 4 files changed, 100 insertions(+), 98 deletions(-) delete mode 100644 src/client/components/website/MetricsTable.tsx create mode 100644 src/client/components/website/WebsiteMetricsTable.tsx diff --git a/src/client/components/dashboard/items/WebsiteEventItem.tsx b/src/client/components/dashboard/items/WebsiteEventItem.tsx index a55e0b9..15dfcd3 100644 --- a/src/client/components/dashboard/items/WebsiteEventItem.tsx +++ b/src/client/components/dashboard/items/WebsiteEventItem.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { MetricsTable } from '../../website/MetricsTable'; +import { WebsiteMetricsTable } from '../../website/WebsiteMetricsTable'; import { useGlobalRangeDate } from '../../../hooks/useGlobalRangeDate'; export const WebsiteEventItem: React.FC<{ @@ -10,7 +10,7 @@ export const WebsiteEventItem: React.FC<{ const endAt = endDate.valueOf(); return ( - = React.memo((props) => { - const { websiteId, title, type, startAt, endAt } = props; - const workspaceId = useCurrentWorkspaceId(); - const { t } = useTranslation(); - - 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', - ellipsis: true, - render: (val) => - val ?? {t('(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'; diff --git a/src/client/components/website/WebsiteMetricsTable.tsx b/src/client/components/website/WebsiteMetricsTable.tsx new file mode 100644 index 0000000..b04bf16 --- /dev/null +++ b/src/client/components/website/WebsiteMetricsTable.tsx @@ -0,0 +1,90 @@ +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'; +import { useTranslation } from '@i18next-toolkit/react'; + +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 WebsiteMetricsTable: React.FC = React.memo( + (props) => { + const { websiteId, title, type, startAt, endAt } = props; + const workspaceId = useCurrentWorkspaceId(); + const { t } = useTranslation(); + + 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', + ellipsis: true, + render: (val) => + val ?? {t('(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 ( +
+ ); + } +); +WebsiteMetricsTable.displayName = 'WebsiteMetricsTable'; diff --git a/src/client/pages/Website/Detail.tsx b/src/client/pages/Website/Detail.tsx index b59a360..95b26f1 100644 --- a/src/client/pages/Website/Detail.tsx +++ b/src/client/pages/Website/Detail.tsx @@ -5,7 +5,7 @@ import { trpc } from '../../api/trpc'; import { ErrorTip } from '../../components/ErrorTip'; import { Loading } from '../../components/Loading'; import { NotFoundTip } from '../../components/NotFoundTip'; -import { MetricsTable } from '../../components/website/MetricsTable'; +import { WebsiteMetricsTable } from '../../components/website/WebsiteMetricsTable'; import { WebsiteOverview } from '../../components/website/WebsiteOverview'; import { useGlobalRangeDate } from '../../hooks/useGlobalRangeDate'; import { useCurrentWorkspaceId } from '../../store/user'; @@ -45,7 +45,7 @@ export const WebsiteDetail: React.FC = React.memo(() => { - { /> - { /> - { /> - { /> - { /> - { -