From 699fe6c967cf4ee5278b723c27239957631b82c4 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 13 Jan 2024 02:43:38 +0800 Subject: [PATCH] fix: fix custom display text not apply in health bar --- .../components/monitor/MonitorHealthBar.tsx | 34 ++++++++++++++++--- src/client/components/monitor/MonitorInfo.tsx | 1 + .../components/monitor/MonitorListItem.tsx | 1 + 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/client/components/monitor/MonitorHealthBar.tsx b/src/client/components/monitor/MonitorHealthBar.tsx index 25c1d68..1337b22 100644 --- a/src/client/components/monitor/MonitorHealthBar.tsx +++ b/src/client/components/monitor/MonitorHealthBar.tsx @@ -5,10 +5,13 @@ import { HealthBar, HealthBarBeat, HealthBarProps } from '../HealthBar'; import dayjs from 'dayjs'; import { trpc } from '../../api/trpc'; import { useWatch } from '../../hooks/useWatch'; +import { getMonitorProvider, getProviderDisplay } from './provider'; +import { MonitorProvider } from './provider/types'; interface MonitorHealthBarProps { workspaceId: string; monitorId: string; + monitorType?: string; count?: number; size?: HealthBarProps['size']; showCurrentStatus?: boolean; @@ -47,6 +50,8 @@ export const MonitorHealthBar: React.FC = React.memo( ); }, [newDataList, recent, count]); + const provider = useMonitorProvider(monitorId, props.monitorType); + const beats = useMemo(() => { return items.map((item): HealthBarBeat => { if (!item) { @@ -55,9 +60,11 @@ export const MonitorHealthBar: React.FC = React.memo( }; } - const title = `${dayjs(item.createdAt).format('YYYY-MM-DD HH:mm')} | ${ - item.value - }ms`; + const { text } = getProviderDisplay(item.value, provider); + + const title = `${dayjs(item.createdAt).format( + 'YYYY-MM-DD HH:mm' + )} | ${text}`; if (item.value < 0) { return { @@ -71,7 +78,7 @@ export const MonitorHealthBar: React.FC = React.memo( status: 'health', }; }); - }, [items]); + }, [items, provider]); useWatch([items], () => { props.onBeatsItemUpdate?.(items); @@ -103,3 +110,22 @@ export const MonitorHealthBar: React.FC = React.memo( } ); MonitorHealthBar.displayName = 'MonitorHealthBar'; + +function useMonitorProvider( + monitorId: string, + monitorType?: string +): MonitorProvider | null { + const { data: [monitorPublicInfo] = [] } = + trpc.monitor.getPublicInfo.useQuery( + { + monitorIds: [monitorId], + }, + { + enabled: !monitorType, + } + ); + + const type = monitorType ?? monitorPublicInfo?.type; + + return type ? getMonitorProvider(type) : null; +} diff --git a/src/client/components/monitor/MonitorInfo.tsx b/src/client/components/monitor/MonitorInfo.tsx index 1b31bdb..b58dac8 100644 --- a/src/client/components/monitor/MonitorInfo.tsx +++ b/src/client/components/monitor/MonitorInfo.tsx @@ -246,6 +246,7 @@ export const MonitorInfo: React.FC = React.memo((props) => {