"use client"; import { Bell } from "lucide-react"; import { useState } from "react"; import { CHART_COLORS, SEVERITY_ORDER } from "./shared/constants"; import { getSeverityColorByName } from "./shared/utils"; import { BarDataPoint } from "./types"; interface HorizontalBarChartProps { data: BarDataPoint[]; height?: number; title?: string; } export function HorizontalBarChart({ data, title }: HorizontalBarChartProps) { const [hoveredIndex, setHoveredIndex] = useState(null); const total = data.reduce((sum, d) => sum + (Number(d.value) || 0), 0); const isEmpty = total <= 0; const emptyData: BarDataPoint[] = [ { name: "Critical", value: 1, percentage: 100 }, { name: "High", value: 1, percentage: 100 }, { name: "Medium", value: 1, percentage: 100 }, { name: "Low", value: 1, percentage: 100 }, ]; const sortedData = (isEmpty ? emptyData : [...data]).sort((a, b) => { const orderA = SEVERITY_ORDER[a.name as keyof typeof SEVERITY_ORDER] ?? 999; const orderB = SEVERITY_ORDER[b.name as keyof typeof SEVERITY_ORDER] ?? 999; return orderA - orderB; }); return (
{title && (

{title}

)}
{sortedData.map((item, index) => { const isHovered = !isEmpty && hoveredIndex === index; const isFaded = !isEmpty && hoveredIndex !== null && !isHovered; const barColor = isEmpty ? CHART_COLORS.gridLine : item.color || getSeverityColorByName(item.name) || CHART_COLORS.defaultColor; return (
!isEmpty && setHoveredIndex(index)} onMouseLeave={() => !isEmpty && setHoveredIndex(null)} > {/* Label */}
{item.name}
{/* Bar - flexible */}
{(item.value > 0 || isEmpty) && (
d.value))) * 100}%`, backgroundColor: barColor, opacity: isFaded ? 0.5 : 1, }} /> )} {isHovered && (
{/* Title with color chip */}

{item.value.toLocaleString()} {item.name} Risk

{/* New Findings row */} {item.newFindings !== undefined && (

{item.newFindings} New Findings

)} {/* Change percentage row */} {item.change !== undefined && (

{item.change > 0 ? "+" : ""} {item.change}% Since last scan

)}
)}
{/* Percentage and Count */}
{isEmpty ? "0" : item.percentage}% {isEmpty ? "0" : item.value.toLocaleString()}
); })}
); }