fix: fix custom display text not apply in health bar

This commit is contained in:
moonrailgun 2024-01-13 02:43:38 +08:00
parent ec591f0c54
commit 699fe6c967
3 changed files with 32 additions and 4 deletions

View File

@ -5,10 +5,13 @@ import { HealthBar, HealthBarBeat, HealthBarProps } from '../HealthBar';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { trpc } from '../../api/trpc'; import { trpc } from '../../api/trpc';
import { useWatch } from '../../hooks/useWatch'; import { useWatch } from '../../hooks/useWatch';
import { getMonitorProvider, getProviderDisplay } from './provider';
import { MonitorProvider } from './provider/types';
interface MonitorHealthBarProps { interface MonitorHealthBarProps {
workspaceId: string; workspaceId: string;
monitorId: string; monitorId: string;
monitorType?: string;
count?: number; count?: number;
size?: HealthBarProps['size']; size?: HealthBarProps['size'];
showCurrentStatus?: boolean; showCurrentStatus?: boolean;
@ -47,6 +50,8 @@ export const MonitorHealthBar: React.FC<MonitorHealthBarProps> = React.memo(
); );
}, [newDataList, recent, count]); }, [newDataList, recent, count]);
const provider = useMonitorProvider(monitorId, props.monitorType);
const beats = useMemo(() => { const beats = useMemo(() => {
return items.map((item): HealthBarBeat => { return items.map((item): HealthBarBeat => {
if (!item) { if (!item) {
@ -55,9 +60,11 @@ export const MonitorHealthBar: React.FC<MonitorHealthBarProps> = React.memo(
}; };
} }
const title = `${dayjs(item.createdAt).format('YYYY-MM-DD HH:mm')} | ${ const { text } = getProviderDisplay(item.value, provider);
item.value
}ms`; const title = `${dayjs(item.createdAt).format(
'YYYY-MM-DD HH:mm'
)} | ${text}`;
if (item.value < 0) { if (item.value < 0) {
return { return {
@ -71,7 +78,7 @@ export const MonitorHealthBar: React.FC<MonitorHealthBarProps> = React.memo(
status: 'health', status: 'health',
}; };
}); });
}, [items]); }, [items, provider]);
useWatch([items], () => { useWatch([items], () => {
props.onBeatsItemUpdate?.(items); props.onBeatsItemUpdate?.(items);
@ -103,3 +110,22 @@ export const MonitorHealthBar: React.FC<MonitorHealthBarProps> = React.memo(
} }
); );
MonitorHealthBar.displayName = 'MonitorHealthBar'; 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;
}

View File

@ -246,6 +246,7 @@ export const MonitorInfo: React.FC<MonitorInfoProps> = React.memo((props) => {
<MonitorHealthBar <MonitorHealthBar
workspaceId={workspaceId} workspaceId={workspaceId}
monitorId={monitorId} monitorId={monitorId}
monitorType={monitorInfo.type}
count={40} count={40}
size="large" size="large"
showCurrentStatus={true} showCurrentStatus={true}

View File

@ -109,6 +109,7 @@ export const MonitorListItem: React.FC<{
<MonitorHealthBar <MonitorHealthBar
workspaceId={workspaceId} workspaceId={workspaceId}
monitorId={monitorId} monitorId={monitorId}
monitorType={monitorType}
onBeatsItemUpdate={setBeats} onBeatsItemUpdate={setBeats}
/> />
</div> </div>