From ed433455c83728f2ea7539b330f7725454622646 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 17 Dec 2023 19:04:19 +0800 Subject: [PATCH] refactor: abstract MonitorListItem component parameters --- src/client/components/monitor/MonitorList.tsx | 36 +++++++++---------- .../components/monitor/MonitorListItem.tsx | 32 ++++++----------- 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/src/client/components/monitor/MonitorList.tsx b/src/client/components/monitor/MonitorList.tsx index a4c160d..c7fd653 100644 --- a/src/client/components/monitor/MonitorList.tsx +++ b/src/client/components/monitor/MonitorList.tsx @@ -1,33 +1,26 @@ -import React, { useMemo, useState } from 'react'; +import React, { useMemo } from 'react'; import { trpc } from '../../api/trpc'; import { useCurrentWorkspaceId } from '../../store/user'; import { NoWorkspaceTip } from '../NoWorkspaceTip'; import { Loading } from '../Loading'; import { Empty } from 'antd'; import { MonitorListItem } from './MonitorListItem'; +import { useNavigate, useParams } from 'react-router'; +import clsx from 'clsx'; export const MonitorList: React.FC = React.memo(() => { const workspaceId = useCurrentWorkspaceId(); const { data: monitors = [], isLoading } = trpc.monitor.all.useQuery({ workspaceId, }); - const initMonitorId = useMemo(() => { - const pathname = window.location.pathname; - const re = /^\/monitor\/([^\/]+?)$/; - if (re.test(pathname)) { - const id = pathname.match(re)?.[1]; + const navigate = useNavigate(); + const params = useParams<{ + '*': string; + }>()['*']; - if (typeof id === 'string') { - return id; - } - } - - return null; - }, []); - - const [selectedMonitorId, setSelectedMonitorId] = useState( - initMonitorId - ); + const selectedMonitorId = useMemo(() => { + return params?.split('/')[0] ?? ''; + }, [params]); if (!workspaceId) { return ; @@ -44,10 +37,13 @@ export const MonitorList: React.FC = React.memo(() => { {monitors.map((monitor) => ( { + navigate(`/monitor/${monitor.id}`); + }} /> ))} diff --git a/src/client/components/monitor/MonitorListItem.tsx b/src/client/components/monitor/MonitorListItem.tsx index f067755..ecbd1ca 100644 --- a/src/client/components/monitor/MonitorListItem.tsx +++ b/src/client/components/monitor/MonitorListItem.tsx @@ -1,20 +1,16 @@ import clsx from 'clsx'; import React, { useMemo, useState } from 'react'; -import { useNavigate } from 'react-router'; -import { AppRouterOutput } from '../../api/trpc'; import { MonitorHealthBar } from './MonitorHealthBar'; -type MonitorType = AppRouterOutput['monitor']['all'][number]; - export const MonitorListItem: React.FC<{ - monitor: MonitorType; + className?: string; workspaceId: string; - selectedMonitorId: string | null; - setSelectedMonitorId: (monitorId: string) => void; + monitorId: string; + monitorName: string; + onClick?: () => void; }> = React.memo((props) => { - const { monitor, workspaceId, selectedMonitorId, setSelectedMonitorId } = - props; - const navigate = useNavigate(); + const { className, workspaceId, monitorId, monitorName, onClick } = props; + const [beats, setBeats] = useState< ({ value: number; @@ -39,17 +35,11 @@ export const MonitorListItem: React.FC<{ return (
{ - navigate(`/monitor/${monitor.id}`); - setSelectedMonitorId(monitor.id); - }} + onClick={onClick} >
-
{monitor.name}
+
{monitorName}
{/*
{monitor.tags.map((tag) => (