import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { financialHealthService } from '../services/api'; import useFormatters from '../hooks/useFormatters'; import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, Filler, } from 'chart.js'; import { Line } from 'react-chartjs-2'; ChartJS.register( CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, Filler ); const FinancialHealth = () => { const { t } = useTranslation(); const { currency } = useFormatters(); const [loading, setLoading] = useState(true); const [data, setData] = useState(null); const [history, setHistory] = useState([]); useEffect(() => { loadData(); }, []); const loadData = async () => { setLoading(true); try { const [healthData, historyData] = await Promise.all([ financialHealthService.get(), financialHealthService.getHistory({ months: 6 }) ]); setData(healthData); setHistory(historyData); } catch (error) { console.error('Error loading financial health:', error); } finally { setLoading(false); } }; const getScoreColor = (score) => { if (score >= 80) return '#10b981'; if (score >= 60) return '#22c55e'; if (score >= 40) return '#f59e0b'; if (score >= 20) return '#f97316'; return '#ef4444'; }; const getScoreLabel = (score) => { if (score >= 80) return t('financialHealth.excellent'); if (score >= 60) return t('financialHealth.good'); if (score >= 40) return t('financialHealth.regular'); if (score >= 20) return t('financialHealth.bad'); return t('financialHealth.critical'); }; const metricConfigs = { savingsCapacity: { icon: 'bi-piggy-bank', color: '#10b981', gradient: 'linear-gradient(135deg, #059669 0%, #10b981 100%)', }, debtControl: { icon: 'bi-credit-card', color: '#3b82f6', gradient: 'linear-gradient(135deg, #2563eb 0%, #3b82f6 100%)', }, budgetManagement: { icon: 'bi-wallet2', color: '#8b5cf6', gradient: 'linear-gradient(135deg, #7c3aed 0%, #8b5cf6 100%)', }, investments: { icon: 'bi-graph-up-arrow', color: '#f59e0b', gradient: 'linear-gradient(135deg, #d97706 0%, #f59e0b 100%)', }, emergencyFund: { icon: 'bi-shield-check', color: '#06b6d4', gradient: 'linear-gradient(135deg, #0891b2 0%, #06b6d4 100%)', }, futurePlanning: { icon: 'bi-calendar-check', color: '#ec4899', gradient: 'linear-gradient(135deg, #db2777 0%, #ec4899 100%)', }, }; const getInsightIcon = (type) => { switch (type) { case 'success': return 'bi-check-circle-fill'; case 'warning': return 'bi-exclamation-triangle-fill'; case 'danger': return 'bi-x-circle-fill'; case 'info': return 'bi-info-circle-fill'; default: return 'bi-lightbulb-fill'; } }; const getInsightColor = (type) => { switch (type) { case 'success': return '#10b981'; case 'warning': return '#f59e0b'; case 'danger': return '#ef4444'; case 'info': return '#3b82f6'; default: return '#8b5cf6'; } }; if (loading) { return (
{t('financialHealth.subtitle')}
{t('financialHealth.scoreDescription')}
{/* History Chart */} {history.length > 0 && ({insight.message}