import React from 'react'; import { formatTimeOrDate } from 'App/date'; import cn from 'classnames'; import { ArrowUp, ArrowDown } from 'lucide-react'; import { useTranslation } from 'react-i18next'; interface PayloadItem { hide?: boolean; name: string; value: number; prevValue?: number; color?: string; payload?: any; } interface Props { active: boolean; payload: PayloadItem[]; label: string; hoveredSeries?: string | null; } function CustomTooltip(props: Props) { const { t } = useTranslation(); const { active, payload, label, hoveredSeries = null } = props; // Return null if tooltip is not active or there is no valid payload if (!active || !payload?.length || !hoveredSeries) return null; // Find the current and comparison payloads const currentPayload = payload.find((p) => p.name === hoveredSeries); const comparisonPayload = payload.find( (p) => p.name === `${hoveredSeries.replace(' (Comparison)', '')} (Comparison)` || p.name === `${hoveredSeries} (Comparison)`, ); if (!currentPayload) return null; // Create transformed array with comparison data const transformedArray = [ { ...currentPayload, prevValue: comparisonPayload ? comparisonPayload.value : null, }, ]; const isHigher = (item: { value: number; prevValue: number }) => item.prevValue !== null && item.prevValue < item.value; const getPercentDelta = (val: number, prevVal: number) => (((val - prevVal) / prevVal) * 100).toFixed(2); return (
{transformedArray.map((p, index) => (
{index + 1}
{p.name}
{label},{' '} {p.payload?.timestamp ? ( formatTimeOrDate(p.payload.timestamp) ) : (
'{t('Timestamp is not Applicable')}'
)}
{p.value}
))}
); } export function CompareTag({ isHigher, absDelta, delta, }: { isHigher: boolean | null; // Allow null for default view absDelta?: number | string | null; delta?: string | null; }) { const { t } = useTranslation(); return (
{isHigher === null ? (
{t('No Comparison')}
) : ( <> {!isHigher ? : }
{absDelta}
({delta} %)
)}
); } export default CustomTooltip;