From 8ff5db80e21a8489a347be7a907cb5dad8f37aba Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 31 Jan 2024 01:22:01 +0800 Subject: [PATCH] feat: add previous period in website overview --- src/client/components/website/MetricCard.tsx | 21 ++++++- .../components/website/WebsiteOverview.tsx | 62 ++++++++++++------- src/client/store/global.ts | 2 + src/server/model/_schema/filter.ts | 2 +- src/server/model/monitor/provider/http.ts | 2 +- src/server/trpc/routers/website.ts | 8 ++- 6 files changed, 67 insertions(+), 30 deletions(-) diff --git a/src/client/components/website/MetricCard.tsx b/src/client/components/website/MetricCard.tsx index 3713a5b..21bf65c 100644 --- a/src/client/components/website/MetricCard.tsx +++ b/src/client/components/website/MetricCard.tsx @@ -1,9 +1,11 @@ import { Tag } from 'antd'; import React from 'react'; import { formatNumber } from '../../utils/common'; +import { useGlobalStateStore } from '../../store/global'; interface MetricCardProps { value?: number; + prev?: number; change?: number; label: string; reverseColors?: boolean; @@ -13,12 +15,16 @@ interface MetricCardProps { export const MetricCard: React.FC = React.memo((props) => { const { value = 0, + prev = 0, change = 0, label, reverseColors = false, format = formatNumber, hideComparison = false, } = props; + const showPreviousPeriod = useGlobalStateStore( + (state) => state.showPreviousPeriod + ); return (
@@ -26,14 +32,25 @@ export const MetricCard: React.FC = React.memo((props) => { {format(value)}
- {label} - {~~change !== 0 && !hideComparison && ( + {label} + {change !== 0 && !hideComparison && ( = 0 ? 'green' : 'red'}> {change > 0 && '+'} {format(change)} )}
+ + {showPreviousPeriod && ( +
+
+ {format(prev)} +
+
+ Previous {label} +
+
+ )} ); }); diff --git a/src/client/components/website/WebsiteOverview.tsx b/src/client/components/website/WebsiteOverview.tsx index c3f0772..8b41355 100644 --- a/src/client/components/website/WebsiteOverview.tsx +++ b/src/client/components/website/WebsiteOverview.tsx @@ -1,4 +1,4 @@ -import { Button, message, Spin } from 'antd'; +import { Button, message, Spin, Switch } from 'antd'; import React, { useMemo } from 'react'; import { Column, ColumnConfig } from '@ant-design/charts'; import { SyncOutlined } from '@ant-design/icons'; @@ -23,6 +23,7 @@ import { MonitorHealthBar } from '../monitor/MonitorHealthBar'; import { useNavigate } from 'react-router'; import { AppRouterOutput, trpc } from '../../api/trpc'; import { getUserTimezone } from '../../api/model/user'; +import { useGlobalStateStore } from '../../store/global'; export const WebsiteOverview: React.FC<{ website: WebsiteInfo; @@ -32,6 +33,9 @@ export const WebsiteOverview: React.FC<{ const { website, showDateFilter = false, actions } = props; const { startDate, endDate, unit, refresh } = useGlobalRangeDate(); const navigate = useNavigate(); + const showPreviousPeriod = useGlobalStateStore( + (state) => state.showPreviousPeriod + ); const { pageviews, @@ -113,7 +117,19 @@ export const WebsiteOverview: React.FC<{
{stats && }
-
+
+
+ + useGlobalStateStore.setState({ + showPreviousPeriod: checked, + }) + } + /> + Previous period +
+