import { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; import { ArrowRight, TrendingUp, TrendingDown, RefreshCw, CreditCard, } from 'lucide-react'; import api, { type Account, type Transaction } from '../api'; function formatAmount(amount: number, currency: string) { const abs = Math.abs(amount); if (currency === 'USD') { return `$${abs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; } return `₡${abs.toLocaleString('es-CR', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; } function formatDate(dateStr: string) { return new Date(dateStr).toLocaleDateString('en-US', { month: 'short', day: 'numeric', }); } export default function Dashboard() { const [accounts, setAccounts] = useState([]); const [recent, setRecent] = useState([]); const [loading, setLoading] = useState(true); const fetchData = async () => { setLoading(true); try { const [accRes, txRes] = await Promise.all([ api.get('/accounts/'), api.get('/transactions/recent?limit=5'), ]); setAccounts(accRes.data); setRecent(txRes.data); } catch (e) { console.error(e); } finally { setLoading(false); } }; useEffect(() => { fetchData(); }, []); const totalCRC = accounts .filter((a) => a.currency === 'CRC') .reduce((s, a) => s + a.balance, 0); const totalUSD = accounts .filter((a) => a.currency === 'USD') .reduce((s, a) => s + a.balance, 0); return (
{/* Header */}

Dashboard

Financial overview

{/* Account balances */}
{accounts.map((account, i) => (
{account.label} {account.bank}

{formatAmount(account.balance, account.currency)}

))} {/* Totals */} {accounts.length > 0 && ( <>
Total CRC

{formatAmount(totalCRC, 'CRC')}

Total USD

{formatAmount(totalUSD, 'USD')}

)}
{/* Recent transactions */}

Recent Charges

View all
{recent.length === 0 && !loading ? (
No transactions yet. Add your first one!
) : (
{recent.map((tx, i) => (
{tx.transaction_type === 'DEVOLUCION' ? ( ) : ( )}

{tx.merchant}

{formatDate(tx.date)} {tx.category && ( {tx.category.name} )}

{tx.transaction_type === 'DEVOLUCION' ? '+' : '-'} {formatAmount(tx.amount, tx.currency)}
))}
)}
); }