From 81bb6360695baaf51c758f069eded2a3dbac676a Mon Sep 17 00:00:00 2001 From: Mihir A Khandekar Date: Tue, 13 Jun 2023 21:12:06 +0530 Subject: [PATCH] console: add tooltip to db latency badge PR-URL: https://github.com/hasura/graphql-engine-mono/pull/9525 GitOrigin-RevId: e4bd2629946ccb24d609d2643e8032e5d8a1992c --- .../parts/LatencyBadge.tsx | 86 +++++++++++++------ 1 file changed, 60 insertions(+), 26 deletions(-) diff --git a/frontend/libs/console/legacy-ce/src/lib/features/ConnectDBRedesign/components/ListConnectedDatabases/parts/LatencyBadge.tsx b/frontend/libs/console/legacy-ce/src/lib/features/ConnectDBRedesign/components/ListConnectedDatabases/parts/LatencyBadge.tsx index 0a3ee22ffe1..d3e50b1368b 100644 --- a/frontend/libs/console/legacy-ce/src/lib/features/ConnectDBRedesign/components/ListConnectedDatabases/parts/LatencyBadge.tsx +++ b/frontend/libs/console/legacy-ce/src/lib/features/ConnectDBRedesign/components/ListConnectedDatabases/parts/LatencyBadge.tsx @@ -1,38 +1,72 @@ +import { useMemo } from 'react'; import { FaCheck, FaExclamationTriangle, FaMinusCircle } from 'react-icons/fa'; +import ToolTip from '../../../../../../lib/components/Common/Tooltip/Tooltip'; +import { Badge, BadgeColor } from '../../../../../new-components/Badge'; import { Latency } from '../../../types'; -import { Badge } from '../../../../../new-components/Badge'; -import React from 'react'; -export const LatencyBadge = ({ - latencies, - dataSourceName, -}: { +type AvgLatency = Latency['avgLatency']; + +type GetBadgeProps = { + color: BadgeColor; + icon: React.ReactNode; + label: string; +}; + +type LatencyBadgeProps = { latencies: Latency[]; dataSourceName: string; -}) => { - const currentDataSourceLatencyInfo = latencies.find( - latencyInfo => latencyInfo.dataSourceName === dataSourceName +}; + +const getMessage = (avgLatency: AvgLatency) => + `Latency: ${Math.ceil(avgLatency)} ms`; + +const getBadgeProps = (avgLatency: AvgLatency): GetBadgeProps => { + if (avgLatency < 100) { + return { + color: 'green', + icon: , + label: 'Connection', + }; + } else if (avgLatency < 200) { + return { + color: 'yellow', + icon: , + label: 'Acceptable', + }; + } else { + return { + color: 'red', + icon: , + label: 'Elevated Latency', + }; + } +}; + +export const LatencyBadge = ({ + latencies = [], + dataSourceName, +}: LatencyBadgeProps) => { + const currentDataSourceLatencyInfo = useMemo( + () => + latencies.find( + latencyInfo => latencyInfo.dataSourceName === dataSourceName + ), + [latencies, dataSourceName] ); - if (!currentDataSourceLatencyInfo) return null; + if (!currentDataSourceLatencyInfo) { + return null; + } - if (currentDataSourceLatencyInfo.avgLatency < 100) - return ( - - Connection - - ); - - if (currentDataSourceLatencyInfo.avgLatency < 200) - return ( - - Acceptable - - ); + const { avgLatency } = currentDataSourceLatencyInfo || {}; + const message = getMessage(avgLatency); + const badgeProps = getBadgeProps(avgLatency); return ( - - Elevated Latency - + + + {badgeProps.icon} {badgeProps.label} + + ); };