From 0a3479d06c4e1126c930b8590f4e38dce5c86e4f Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 28 Oct 2023 15:01:23 +0800 Subject: [PATCH] fix: fix monitor list upPercent calc problem --- src/client/components/monitor/MonitorList.tsx | 129 ++++++++++-------- 1 file changed, 73 insertions(+), 56 deletions(-) diff --git a/src/client/components/monitor/MonitorList.tsx b/src/client/components/monitor/MonitorList.tsx index 47f2930..f82f2d0 100644 --- a/src/client/components/monitor/MonitorList.tsx +++ b/src/client/components/monitor/MonitorList.tsx @@ -1,17 +1,18 @@ import clsx from 'clsx'; import React, { useMemo, useState } from 'react'; import { useNavigate } from 'react-router'; -import { trpc } from '../../api/trpc'; +import { AppRouterOutput, trpc } from '../../api/trpc'; import { useCurrentWorkspaceId } from '../../store/user'; import { NoWorkspaceTip } from '../NoWorkspaceTip'; import { MonitorHealthBar } from './MonitorHealthBar'; +type MonitorType = AppRouterOutput['monitor']['all'][number]; + export const MonitorList: React.FC = React.memo(() => { - const currentWorkspaceId = useCurrentWorkspaceId()!; + const workspaceId = useCurrentWorkspaceId(); const { data: monitors = [] } = trpc.monitor.all.useQuery({ - workspaceId: currentWorkspaceId, + workspaceId, }); - const navigate = useNavigate(); const initMonitorId = useMemo(() => { const pathname = window.location.pathname; const re = /^\/monitor\/([^\/]+?)$/; @@ -25,6 +26,37 @@ export const MonitorList: React.FC = React.memo(() => { return null; }, []); + + const [selectedMonitorId, setSelectedMonitorId] = useState( + initMonitorId + ); + + if (!workspaceId) { + return ; + } + + return ( +
+ {monitors.map((monitor) => ( + + ))} +
+ ); +}); +MonitorList.displayName = 'MonitorList'; + +export const MonitorListItem: React.FC<{ + monitor: MonitorType; + selectedMonitorId: string | null; + setSelectedMonitorId: (monitorId: string) => void; +}> = React.memo((props) => { + const { monitor, selectedMonitorId, setSelectedMonitorId } = props; + const navigate = useNavigate(); const [beats, setBeats] = useState< ({ value: number; @@ -32,10 +64,6 @@ export const MonitorList: React.FC = React.memo(() => { } | null)[] >([]); - const [selectedMonitorId, setSelectedMonitorId] = useState( - initMonitorId - ); - const upPercent = useMemo(() => { let up = 0; beats.forEach((b) => { @@ -51,59 +79,48 @@ export const MonitorList: React.FC = React.memo(() => { return parseFloat(((up / beats.length) * 100).toFixed(1)); }, [beats]); - if (!currentWorkspaceId) { - return ; - } - return ( -
- {monitors.map((monitor) => ( -
{ + navigate(`/monitor/${monitor.id}`); + setSelectedMonitorId(monitor.id); + }} + > +
+ { - navigate(`/monitor/${monitor.id}`); - setSelectedMonitorId(monitor.id); - }} > -
- - {upPercent}% - -
-
-
{monitor.name}
- {/*
- {monitor.tags.map((tag) => ( - - {tag.label} - - ))} -
*/} -
- -
- -
-
+ {upPercent}% + +
+
+
{monitor.name}
+ {/*
+ {monitor.tags.map((tag) => ( + + {tag.label} + ))} +
*/} +
+ +
+ +
); }); -MonitorList.displayName = 'MonitorList'; +MonitorListItem.displayName = 'MonitorListItem';