import React, { useState, useEffect, useCallback, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { useNavigate } from 'react-router-dom'; import { dashboardService } from '../../services/api'; import useFormatters from '../../hooks/useFormatters'; const OverdueWidget = () => { const { t } = useTranslation(); const { currency } = useFormatters(); const navigate = useNavigate(); const cardRef = useRef(null); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [expandedRange, setExpandedRange] = useState(null); const [isMobile, setIsMobile] = useState(window.innerWidth < 768); const [isExpanded, setIsExpanded] = useState(false); // Detectar mudanças de tamanho da tela useEffect(() => { const handleResize = () => { setIsMobile(window.innerWidth < 768); }; window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, []); // Auto-expandir quando houver dados em mobile useEffect(() => { if (isMobile && data?.items?.length > 0 && !isExpanded) { setIsExpanded(true); } }, [isMobile, data, isExpanded]); const loadData = useCallback(async () => { setLoading(true); try { const result = await dashboardService.getOverdue(50); setData(result); // Expandir automaticamente a primeira faixa com items if (result?.by_range?.length > 0) { setExpandedRange(result.by_range[0].key); } } catch (error) { console.error('Error loading overdue transactions:', error); } finally { setLoading(false); } }, []); useEffect(() => { loadData(); }, [loadData]); const getTypeIcon = (item) => { if (item.type === 'recurring') { return 'bi-arrow-repeat'; } if (item.type === 'liability') { return 'bi-credit-card-2-back'; } return item.transaction_type === 'credit' ? 'bi-arrow-down-circle' : 'bi-arrow-up-circle'; }; const getTypeColor = (item) => { if (item.type === 'recurring') return '#f59e0b'; if (item.type === 'liability') return '#8b5cf6'; // purple for liability return item.transaction_type === 'credit' ? '#10b981' : '#ef4444'; }; const getRangeColor = (key) => { switch (key) { case 'critical': return '#dc2626'; // red-600 case 'high': return '#ea580c'; // orange-600 case 'medium': return '#d97706'; // amber-600 case 'low': return '#ca8a04'; // yellow-600 default: return '#6b7280'; } }; const getRangeIcon = (key) => { switch (key) { case 'critical': return 'bi-exclamation-octagon-fill'; case 'high': return 'bi-exclamation-triangle-fill'; case 'medium': return 'bi-exclamation-circle-fill'; case 'low': return 'bi-clock-fill'; default: return 'bi-circle-fill'; } }; const handleTransactionClick = (item) => { if (item.type === 'transaction') { navigate(`/transactions?highlight=${item.id}`); } else if (item.type === 'recurring') { navigate(`/recurring?highlight=${item.template_id}`); } else if (item.type === 'liability') { navigate(`/liabilities?highlight=${item.liability_account_id}`); } }; const toggleRange = (key) => { setExpandedRange(expandedRange === key ? null : key); }; return (
{t('dashboard.noOverdueTransactions')}