import React, { useState, useMemo } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; const ApplicationTrendsChart = ({ data }) => { // --- DATA LOGIC --- const safeData = useMemo(() => { const applicants = data || []; const last30Days = Array.from({ length: 30 }, (_, i) => { const d = new Date(); d.setDate(d.getDate() - (29 - i)); return d.toISOString().split('T')[0]; }); return last30Days.map(dateStr => { const count = applicants.filter(a => a.created_at && a.created_at.startsWith(dateStr)).length; return { name: dateStr.split('-')[2], value: count }; }); }, [data]); const maxValue = 4; const [hoverIndex, setHoverIndex] = useState(null); // ✅ FIX: High Resolution Coordinate System (was 100x100) const width = 800; const height = 300; const padding = { top: 40, right: 20, bottom: 50, left: 40 }; const chartWidth = width - padding.left - padding.right; const chartHeight = height - padding.top - padding.bottom; const getX = i => padding.left + (i / (safeData.length - 1)) * chartWidth; const getY = v => padding.top + chartHeight - (v / maxValue) * chartHeight; const path = safeData .map((d, i) => `${i === 0 ? 'M' : 'L'} ${getX(i)} ${getY(d.value)}`) .join(' '); const formatDate = (index) => { const date = new Date(); date.setDate(date.getDate() - (safeData.length - 1 - index)); return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); }; const getIndexFromX = (svgX) => { const ratio = (svgX - padding.left) / chartWidth; const index = Math.round(ratio * (safeData.length - 1)); return Math.max(0, Math.min(safeData.length - 1, index)); }; return (