Files
CrazeAnalytix/components/MetricDetailTooltip.tsx
T

108 lines
5.1 KiB
TypeScript
Raw Normal View History

import React, { useState } from 'react';
interface MetricDetailTooltipProps {
children: React.ReactNode;
currentValue: number;
previousValue: number;
yoyValue: number;
currentWeekLabel: string;
previousWeekLabel: string;
yoyWeekLabel: string;
metricName: string;
metricColor: string;
formatValue?: (val: number) => string;
}
/**
* Tooltip component to show comparison details on hover (WoW and YoY).
* Extracted from WeeklyGrid to prevent circular dependencies and initialization errors.
*/
export const MetricDetailTooltip: React.FC<MetricDetailTooltipProps> = ({
children,
currentValue,
previousValue,
yoyValue,
currentWeekLabel,
previousWeekLabel,
yoyWeekLabel,
metricName,
metricColor,
formatValue,
}) => {
const [isVisible, setIsVisible] = useState(false);
const wowGrowth = previousValue > 0 ? ((currentValue - previousValue) / previousValue) * 100 : (currentValue > 0 ? 100 : 0);
const yoyGrowth = yoyValue > 0 ? ((currentValue - yoyValue) / yoyValue) * 100 : null;
const format = formatValue || ((v: number) => v.toLocaleString('de-DE'));
const hasYoyData = yoyValue > 0;
return (
<div
className="relative inline-flex items-center"
onMouseEnter={() => setIsVisible(true)}
onMouseLeave={() => setIsVisible(false)}
>
<div className="cursor-help">{children}</div>
{isVisible && (
<div className="absolute bottom-full left-1/2 -translate-x-1/2 mb-2 w-72 bg-slate-950 border border-white/20 rounded-xl shadow-2xl z-[200] p-3 pointer-events-none animate-in fade-in zoom-in-95 duration-150">
<div className="text-[10px] font-black text-slate-400 uppercase tracking-widest mb-2 pb-2 border-b border-white/10">
📊 {metricName} Comparison
</div>
<div className="space-y-1.5">
{/* Current Week */}
<div className="flex justify-between items-center">
<span className="text-[10px] text-slate-500 font-bold">{currentWeekLabel}</span>
<span className={`text-xs font-black ${metricColor}`}>{format(currentValue)}</span>
</div>
{/* Previous Week */}
<div className="flex justify-between items-center">
<span className="text-[10px] text-slate-500 font-bold">{previousWeekLabel}</span>
<span className="text-xs font-bold text-slate-400">{previousValue > 0 ? format(previousValue) : 'N/A'}</span>
</div>
{/* Same Week Last Year */}
<div className="flex justify-between items-center">
<span className="text-[10px] text-slate-500 font-bold">{yoyWeekLabel}</span>
<span className={`text-xs font-bold ${hasYoyData ? 'text-slate-400' : 'text-slate-600 italic'}`}>
{hasYoyData ? format(yoyValue) : 'No data'}
</span>
</div>
{/* WoW Growth */}
<div className="border-t border-white/10 pt-2 mt-2">
<div className="flex justify-between items-center">
<span className="text-[10px] text-slate-500 font-bold">vs Previous Week</span>
{previousValue > 0 ? (
<span className={`text-xs font-black ${wowGrowth >= 0 ? 'text-emerald-400' : 'text-red-400'}`}>
{wowGrowth >= 0 ? '▲' : '▼'} {Math.abs(wowGrowth).toFixed(1)}%
</span>
) : (
<span className="text-xs font-bold text-slate-600 italic">N/A</span>
)}
</div>
</div>
{/* YoY Growth */}
<div className="flex justify-between items-center">
<span className="text-[10px] text-slate-500 font-bold">vs Same Week Last Year</span>
{yoyGrowth !== null ? (
<span className={`text-xs font-black ${yoyGrowth >= 0 ? 'text-emerald-400' : 'text-red-400'}`}>
{yoyGrowth >= 0 ? '▲' : '▼'} {Math.abs(yoyGrowth).toFixed(1)}%
</span>
) : (
<span className="text-xs font-bold text-slate-600 italic">No data</span>
)}
</div>
</div>
{/* Arrow */}
<div className="absolute bottom-[-6px] left-1/2 -translate-x-1/2 w-3 h-3 bg-slate-950 border-r border-b border-white/20 rotate-45"></div>
</div>
)}
</div>
);
};