Migrate all components and pages to shadcn/ui with DataTable
All checks were successful
Deploy to VPS / deploy (push) Successful in 28s

Replace custom markup across all pages and components with shadcn/ui
primitives (Dialog, Sheet, Select, Card, Tabs, etc.). Add reusable
DataTable component powered by @tanstack/react-table with sortable
column headers and client-side pagination. Introduce TransactionList
with responsive mobile cards and desktop DataTable, dashboard section
customization (DashboardSection, SectionConfigDialog), and settings
API types.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Carlos Escalante
2026-03-22 14:45:44 -06:00
parent 46f2d8679c
commit 2cd0d3b2e1
17 changed files with 1626 additions and 1109 deletions

View File

@@ -7,8 +7,6 @@ import {
Bar,
XAxis,
YAxis,
Tooltip,
ResponsiveContainer,
LineChart,
Line,
} from 'recharts';
@@ -16,7 +14,13 @@ import { BarChart3 } from 'lucide-react';
import api from '../api';
import BillingCycleSelector from '../components/BillingCycleSelector';
import { useTheme } from '../ThemeContext';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
type ChartConfig,
} from '@/components/ui/chart';
interface CategorySpending {
category_id: number | null;
@@ -42,18 +46,30 @@ interface DailySpending {
}
const COLORS = [
'#606C38', '#BC6C25', '#8b5cf6', '#DDA15E', '#ef4444',
'#ec4899', '#283618', '#f97316', '#6366f1', '#8a9462',
'#e879f9', '#c2c9a7', '#fb923c', '#a78bfa', '#7a8a4a',
'#fbbf24',
'var(--chart-1)', 'var(--chart-2)', 'var(--chart-3)', 'var(--chart-4)', 'var(--chart-5)',
'oklch(0.7 0.15 30)', 'oklch(0.65 0.2 300)', 'oklch(0.6 0.15 150)',
'oklch(0.75 0.12 60)', 'oklch(0.55 0.18 250)',
];
function formatCRC(value: number) {
return `${Math.round(value).toLocaleString('es-CR')}`;
}
const trendChartConfig = {
total_crc: {
label: 'Total CRC',
color: 'var(--chart-1)',
},
} satisfies ChartConfig;
const dailyChartConfig = {
total: {
label: 'Daily Spending',
color: 'var(--chart-2)',
},
} satisfies ChartConfig;
export default function Analytics() {
const { theme } = useTheme();
const [cycle, setCycle] = useState<{ year: number; month: number } | null>(null);
const [byCategory, setByCategory] = useState<CategorySpending[]>([]);
const [trend, setTrend] = useState<MonthlyTrend[]>([]);
@@ -79,194 +95,216 @@ export default function Analytics() {
.catch(console.error);
}, [cycle]);
const tooltipStyle = {
background: theme === 'dark' ? '#1e293b' : '#FEFAE0',
border: `1px solid ${theme === 'dark' ? '#334155' : 'rgba(96,108,56,0.25)'}`,
borderRadius: '8px',
fontSize: '12px',
color: theme === 'dark' ? '#e2e8f0' : '#283618',
};
const tickColor = theme === 'dark' ? '#64748b' : '#8a9462';
// Build dynamic chart config for pie chart
const pieChartConfig = byCategory.reduce<ChartConfig>((acc, cat, i) => {
acc[cat.category_name] = {
label: cat.category_name,
color: COLORS[i % COLORS.length],
};
return acc;
}, {});
return (
<div className="space-y-6">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
<div className="flex items-center gap-2">
<BarChart3 className="w-5 h-5 text-[#606C38] dark:text-[#7a8a4a]" />
<h1 className="text-2xl font-bold">Analytics</h1>
<BarChart3 className="w-5 h-5 text-primary" />
<h1 className="text-2xl font-bold font-heading">Analytics</h1>
</div>
<p className="text-sm text-text-muted mt-1">Spending breakdown and trends</p>
<p className="text-sm text-muted-foreground mt-1">Spending breakdown and trends</p>
</div>
<BillingCycleSelector value={cycle} onChange={setCycle} />
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
{/* Spending by Category - Donut */}
<div className="bg-surface-card border border-border rounded-xl p-5">
<h2 className="text-sm font-semibold text-text-secondary uppercase tracking-wider mb-4">
Spending by Category
</h2>
{byCategory.length === 0 ? (
<div className="h-64 flex items-center justify-center text-text-faint text-sm">
No data for this period
</div>
) : (
<div className="flex flex-col items-center">
<ResponsiveContainer width="100%" height={260}>
<PieChart>
<Pie
data={byCategory}
dataKey="total"
nameKey="category_name"
cx="50%"
cy="50%"
innerRadius={60}
outerRadius={100}
paddingAngle={2}
strokeWidth={0}
>
{byCategory.map((_, i) => (
<Cell key={i} fill={COLORS[i % COLORS.length]} />
))}
</Pie>
<Tooltip
contentStyle={tooltipStyle}
formatter={(value: any) => formatCRC(Number(value))}
/>
</PieChart>
</ResponsiveContainer>
{/* Legend */}
<div className="grid grid-cols-2 gap-x-6 gap-y-1.5 mt-2 w-full max-w-md">
{byCategory.slice(0, 10).map((cat, i) => (
<div key={cat.category_name} className="flex items-center gap-2 text-xs">
<div
className="w-2.5 h-2.5 rounded-full flex-shrink-0"
style={{ background: COLORS[i % COLORS.length] }}
/>
<span className="text-text-secondary truncate">{cat.category_name}</span>
<span className="text-text-faint ml-auto">{cat.percentage}%</span>
</div>
))}
<Card>
<CardHeader>
<CardTitle className="text-sm uppercase tracking-wider text-muted-foreground">
Spending by Category
</CardTitle>
</CardHeader>
<CardContent>
{byCategory.length === 0 ? (
<div className="h-64 flex items-center justify-center text-muted-foreground text-sm">
No data for this period
</div>
</div>
)}
</div>
) : (
<div className="flex flex-col items-center">
<ChartContainer config={pieChartConfig} className="h-[260px] w-full">
<PieChart>
<Pie
data={byCategory}
dataKey="total"
nameKey="category_name"
cx="50%"
cy="50%"
innerRadius={60}
outerRadius={100}
paddingAngle={2}
strokeWidth={0}
>
{byCategory.map((_, i) => (
<Cell key={i} fill={COLORS[i % COLORS.length]} />
))}
</Pie>
<ChartTooltip
content={
<ChartTooltipContent
formatter={(value) => formatCRC(Number(value))}
/>
}
/>
</PieChart>
</ChartContainer>
{/* Legend */}
<div className="grid grid-cols-2 gap-x-6 gap-y-1.5 mt-2 w-full max-w-md">
{byCategory.slice(0, 10).map((cat, i) => (
<div key={cat.category_name} className="flex items-center gap-2 text-xs">
<div
className="w-2.5 h-2.5 rounded-full flex-shrink-0"
style={{ background: COLORS[i % COLORS.length] }}
/>
<span className="text-muted-foreground truncate">{cat.category_name}</span>
<span className="text-muted-foreground/60 ml-auto">{cat.percentage}%</span>
</div>
))}
</div>
</div>
)}
</CardContent>
</Card>
{/* Monthly Trend - Bar */}
<div className="bg-surface-card border border-border rounded-xl p-5">
<h2 className="text-sm font-semibold text-text-secondary uppercase tracking-wider mb-4">
Monthly Spending (CRC)
</h2>
{trend.length === 0 ? (
<div className="h-64 flex items-center justify-center text-text-faint text-sm">
No data
</div>
) : (
<ResponsiveContainer width="100%" height={300}>
<BarChart data={trend}>
<XAxis
dataKey="label"
tick={{ fill: tickColor, fontSize: 11 }}
axisLine={false}
tickLine={false}
/>
<YAxis
tick={{ fill: tickColor, fontSize: 11 }}
axisLine={false}
tickLine={false}
tickFormatter={(v) => `${(v / 1000).toFixed(0)}k`}
/>
<Tooltip
contentStyle={tooltipStyle}
formatter={(value: any) => formatCRC(Number(value))}
/>
<Bar dataKey="total_crc" fill="#606C38" radius={[4, 4, 0, 0]} />
</BarChart>
</ResponsiveContainer>
)}
</div>
<Card>
<CardHeader>
<CardTitle className="text-sm uppercase tracking-wider text-muted-foreground">
Monthly Spending (CRC)
</CardTitle>
</CardHeader>
<CardContent>
{trend.length === 0 ? (
<div className="h-64 flex items-center justify-center text-muted-foreground text-sm">
No data
</div>
) : (
<ChartContainer config={trendChartConfig} className="h-[300px] w-full">
<BarChart data={trend}>
<XAxis
dataKey="label"
axisLine={false}
tickLine={false}
/>
<YAxis
axisLine={false}
tickLine={false}
tickFormatter={(v) => `${(v / 1000).toFixed(0)}k`}
/>
<ChartTooltip
content={
<ChartTooltipContent
formatter={(value) => formatCRC(Number(value))}
/>
}
/>
<Bar dataKey="total_crc" fill="var(--color-total_crc)" radius={[4, 4, 0, 0]} />
</BarChart>
</ChartContainer>
)}
</CardContent>
</Card>
{/* Daily Spending - Line */}
<div className="bg-surface-card border border-border rounded-xl p-5 lg:col-span-2">
<h2 className="text-sm font-semibold text-text-secondary uppercase tracking-wider mb-4">
Daily Spending
</h2>
{daily.length === 0 ? (
<div className="h-48 flex items-center justify-center text-text-faint text-sm">
No data for this period
</div>
) : (
<ResponsiveContainer width="100%" height={240}>
<LineChart data={daily}>
<XAxis
dataKey="date"
tick={{ fill: tickColor, fontSize: 10 }}
axisLine={false}
tickLine={false}
tickFormatter={(v) => {
const d = new Date(v);
return `${d.getMonth() + 1}/${d.getDate()}`;
}}
/>
<YAxis
tick={{ fill: tickColor, fontSize: 11 }}
axisLine={false}
tickLine={false}
tickFormatter={(v) => `${(v / 1000).toFixed(0)}k`}
/>
<Tooltip
contentStyle={tooltipStyle}
formatter={(value: any) => formatCRC(Number(value))}
labelFormatter={(label) => new Date(label).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}
/>
<Line
type="monotone"
dataKey="total"
stroke="#BC6C25"
strokeWidth={2}
dot={{ fill: '#BC6C25', r: 3 }}
activeDot={{ r: 5 }}
/>
</LineChart>
</ResponsiveContainer>
)}
</div>
<Card className="lg:col-span-2">
<CardHeader>
<CardTitle className="text-sm uppercase tracking-wider text-muted-foreground">
Daily Spending
</CardTitle>
</CardHeader>
<CardContent>
{daily.length === 0 ? (
<div className="h-48 flex items-center justify-center text-muted-foreground text-sm">
No data for this period
</div>
) : (
<ChartContainer config={dailyChartConfig} className="h-[240px] w-full">
<LineChart data={daily}>
<XAxis
dataKey="date"
axisLine={false}
tickLine={false}
tickFormatter={(v) => {
const d = new Date(v);
return `${d.getMonth() + 1}/${d.getDate()}`;
}}
/>
<YAxis
axisLine={false}
tickLine={false}
tickFormatter={(v) => `${(v / 1000).toFixed(0)}k`}
/>
<ChartTooltip
content={
<ChartTooltipContent
formatter={(value) => formatCRC(Number(value))}
labelFormatter={(label) =>
new Date(label).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })
}
/>
}
/>
<Line
type="monotone"
dataKey="total"
stroke="var(--color-total)"
strokeWidth={2}
dot={{ fill: 'var(--color-total)', r: 3 }}
activeDot={{ r: 5 }}
/>
</LineChart>
</ChartContainer>
)}
</CardContent>
</Card>
</div>
{/* Top categories summary */}
{byCategory.length > 0 && (
<div className="bg-surface-card border border-border rounded-xl p-5">
<h2 className="text-sm font-semibold text-text-secondary uppercase tracking-wider mb-4">
Top Categories
</h2>
<div className="space-y-3">
{byCategory.slice(0, 8).map((cat, i) => (
<div key={cat.category_name} className="flex items-center gap-3">
<div
className="w-3 h-3 rounded-full flex-shrink-0"
style={{ background: COLORS[i % COLORS.length] }}
/>
<span className="text-sm flex-1">{cat.category_name}</span>
<span className="text-xs text-text-muted">{cat.count} txns</span>
<span className="text-sm font-mono font-medium w-32 text-right">
{formatCRC(cat.total)}
</span>
<div className="w-24 bg-surface-hover rounded-full h-1.5">
<Card>
<CardHeader>
<CardTitle className="text-sm uppercase tracking-wider text-muted-foreground">
Top Categories
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-3">
{byCategory.slice(0, 8).map((cat, i) => (
<div key={cat.category_name} className="flex items-center gap-3">
<div
className="h-1.5 rounded-full"
style={{
width: `${cat.percentage}%`,
background: COLORS[i % COLORS.length],
}}
className="w-3 h-3 rounded-full flex-shrink-0"
style={{ background: COLORS[i % COLORS.length] }}
/>
<span className="text-sm flex-1">{cat.category_name}</span>
<span className="text-xs text-muted-foreground">{cat.count} txns</span>
<span className="text-sm font-mono font-medium w-32 text-right">
{formatCRC(cat.total)}
</span>
<div className="w-24 bg-muted rounded-full h-1.5">
<div
className="h-1.5 rounded-full"
style={{
width: `${cat.percentage}%`,
background: COLORS[i % COLORS.length],
}}
/>
</div>
</div>
</div>
))}
</div>
</div>
))}
</div>
</CardContent>
</Card>
)}
</div>
);

View File

@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useState, useMemo } from 'react';
import { Link } from 'react-router-dom';
import {
ArrowRight,
@@ -12,31 +12,36 @@ import {
} from 'lucide-react';
import api, { type Account, type Transaction } from '../api';
import { useSettings } from '@/hooks/useSettings';
import { formatAmount, formatDate } from '@/lib/format';
import DashboardSection from '@/components/DashboardSection';
import SectionConfigDialog from '@/components/SectionConfigDialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { cn } from '@/lib/utils';
function formatAmount(amount: number, currency: string) {
const abs = Math.abs(amount);
if (currency === 'BTC') return abs.toFixed(8);
if (currency === 'XMR') return abs.toFixed(4);
if (currency === 'USD') {
return `$${abs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
return `${abs.toLocaleString('es-CR', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
// --- Section definitions ---
interface SectionDef {
filterFn: (a: Account) => boolean;
totalCurrency: string; // empty string = no total
}
function formatDate(dateStr: string) {
return new Date(dateStr).toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
}
const SECTION_DEFS: Record<string, SectionDef> = {
crc_accounts: { filterFn: (a) => a.account_type === 'BANK' && a.currency === 'CRC', totalCurrency: 'CRC' },
usd_accounts: { filterFn: (a) => a.account_type === 'BANK' && a.currency === 'USD', totalCurrency: 'USD' },
pension: { filterFn: (a) => a.account_type === 'PENSION', totalCurrency: 'CRC' },
savings: { filterFn: (a) => a.account_type === 'SAVINGS', totalCurrency: 'CRC' },
liabilities: { filterFn: (a) => a.account_type === 'LIABILITY', totalCurrency: '' },
crypto: { filterFn: (a) => a.account_type === 'CRYPTO', totalCurrency: '' },
};
// --- Reusable card for an account balance ---
function AccountCard({
account,
editingId,
editValue,
setEditValue,
startEditing,
saveBalance,
cancelEditing,
}: {
const BANK_ORDER = ['BAC', 'BCR', 'DAVIVIENDA'];
// --- AccountRow ---
interface AccountRowProps {
account: Account;
editingId: number | null;
editValue: string;
@@ -44,20 +49,29 @@ function AccountCard({
startEditing: (a: Account) => void;
saveBalance: (id: number) => void;
cancelEditing: () => void;
}) {
const badgeLabel = account.account_type === 'CRYPTO' ? account.label : (account.bank === 'DAVIVIENDA' ? 'DAV' : account.bank);
}
function AccountRow({
account,
editingId,
editValue,
setEditValue,
startEditing,
saveBalance,
cancelEditing,
}: AccountRowProps) {
const isLiability = account.account_type === 'LIABILITY';
const isCrypto = account.account_type === 'CRYPTO';
const label = isCrypto ? account.label : (account.bank === 'DAVIVIENDA' ? 'DAV' : account.bank);
const isEditing = editingId === account.id;
return (
<div className="group animate-fade-in bg-surface dark:bg-slate-900 border border-border dark:border-slate-700 rounded-xl p-5 shadow-sm dark:shadow-none hover:bg-surface-hover dark:hover:bg-slate-800/60 transition-colors h-[104px] flex flex-col justify-between">
<div className="flex items-center justify-end">
<span className="text-sm font-bold font-mono text-text-secondary dark:text-slate-300 bg-surface-secondary dark:bg-slate-800 px-2.5 py-0.5 rounded">
{badgeLabel}
</span>
</div>
<div className="flex items-center justify-between px-3 py-2.5 hover:bg-muted/30 transition-colors group">
<span className="text-sm font-medium text-muted-foreground">{label}</span>
{isEditing ? (
<div className="flex items-center gap-2">
<input
<Input
type="number"
step="0.01"
value={editValue}
@@ -67,36 +81,40 @@ function AccountCard({
if (e.key === 'Escape') cancelEditing();
}}
autoFocus
className="w-full text-2xl font-bold font-mono tracking-tight bg-input-bg border border-[#606C38]/40 rounded-lg px-2 py-1 focus:outline-none focus:border-[#606C38] transition-colors"
className="text-sm font-bold font-mono tracking-tight h-auto py-1 w-40"
/>
<button onClick={() => saveBalance(account.id)} className="p-1 text-[#606C38] dark:text-[#7a8a4a]">
<Check className="w-4 h-4" />
</button>
<button onClick={cancelEditing} className="p-1 text-text-muted hover:text-text-secondary">
<X className="w-4 h-4" />
</button>
<Button variant="ghost" size="icon-xs" onClick={() => saveBalance(account.id)} title="Save" aria-label="Save balance">
<Check className="w-3.5 h-3.5" />
</Button>
<Button variant="ghost" size="icon-xs" onClick={cancelEditing} title="Cancel" aria-label="Cancel editing">
<X className="w-3.5 h-3.5" />
</Button>
</div>
) : (
<div className="flex items-center gap-2 group/balance cursor-pointer" onClick={() => startEditing(account)}>
<p className="text-2xl font-bold font-mono tracking-tight">
<div className="flex items-center gap-1.5">
<span className={cn('text-lg font-bold font-mono tracking-tight', isLiability && 'text-destructive')}>
{formatAmount(account.balance, account.currency)}
</p>
<Pencil className="w-3.5 h-3.5 text-text-faint opacity-0 group-hover/balance:opacity-100 hover:text-[#606C38] dark:hover:text-[#7a8a4a] transition-all" />
</span>
<button
onClick={() => startEditing(account)}
className="opacity-0 group-hover:opacity-100 transition-opacity text-muted-foreground hover:text-foreground cursor-pointer"
title="Edit balance"
aria-label="Edit balance"
>
<Pencil className="w-3.5 h-3.5" />
</button>
{isLiability && account.next_payment != null && (
<span className="text-xs font-mono text-destructive/60 ml-2">
Next: {formatAmount(account.next_payment, account.currency)}
</span>
)}
</div>
)}
</div>
);
}
// --- Total card ---
function TotalCard({ total, currency }: { total: number; currency: string }) {
return (
<div className="border rounded-xl p-5 shadow-sm dark:shadow-none h-[104px] flex flex-col justify-between bg-[#fdf3e3] dark:bg-[#BC6C25]/10 border-[#e8c08a] dark:border-[#BC6C25]/20 text-[#8a5218] dark:text-[#DDA15E]">
<span className="text-xs font-bold uppercase tracking-wider opacity-80">Total</span>
<p className="text-2xl font-bold font-mono tracking-tight">{formatAmount(total, currency)}</p>
</div>
);
}
// --- Dashboard ---
export default function Dashboard() {
const [accounts, setAccounts] = useState<Account[]>([]);
@@ -105,6 +123,9 @@ export default function Dashboard() {
const [editingId, setEditingId] = useState<number | null>(null);
const [editValue, setEditValue] = useState('');
const [exchangeRate, setExchangeRate] = useState<{ buy_rate: number; sell_rate: number } | null>(null);
const [configSection, setConfigSection] = useState<string | null>(null);
const { settings, patchSection } = useSettings();
const fetchData = async () => {
setLoading(true);
@@ -141,222 +162,170 @@ export default function Dashboard() {
} catch (e) { console.error(e); }
};
const cardProps = { editingId, editValue, setEditValue, startEditing, saveBalance, cancelEditing };
const rowProps = { editingId, editValue, setEditValue, startEditing, saveBalance, cancelEditing };
// Group accounts by type
const bankAccounts = accounts.filter((a) => a.account_type === 'BANK');
const pensionAccounts = accounts.filter((a) => a.account_type === 'PENSION');
const savingsAccounts = accounts.filter((a) => a.account_type === 'SAVINGS');
const liabilityAccounts = accounts.filter((a) => a.account_type === 'LIABILITY');
const cryptoAccounts = accounts.filter((a) => a.account_type === 'CRYPTO');
// Sort sections by order, filter by visible
const sortedSections = useMemo(() => {
const sections = settings.dashboard.sections;
return Object.entries(sections)
.filter(([, s]) => s.visible)
.sort(([, a], [, b]) => a.order - b.order);
}, [settings]);
const bankOrder = ['BAC', 'BCR', 'DAVIVIENDA'];
// Bank totals for exchange rate combined total
const bankCRC = bankAccounts.filter((a) => a.currency === 'CRC').reduce((s, a) => s + a.balance, 0);
const bankUSD = bankAccounts.filter((a) => a.currency === 'USD').reduce((s, a) => s + a.balance, 0);
// Net worth calculation
const netWorthBreakdown = useMemo(() => {
if (accounts.length === 0) return null;
let assets = 0;
let liabilities = 0;
for (const a of accounts) {
const isLiability = a.account_type === 'LIABILITY';
let crcValue = 0;
if (a.currency === 'USD') {
crcValue = Math.abs(a.balance) * (exchangeRate?.sell_rate ?? 0);
} else if (a.currency === 'CRC') {
crcValue = Math.abs(a.balance);
}
if (isLiability) {
liabilities += crcValue;
} else {
assets += crcValue;
}
}
return { assets, liabilities, net: assets - liabilities };
}, [accounts, exchangeRate]);
return (
<div className="space-y-6">
{/* Header */}
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl font-bold">Dashboard</h1>
<p className="text-sm text-text-muted mt-1">Financial overview</p>
<h1 className="text-2xl font-bold font-heading">Dashboard</h1>
<p className="text-sm text-muted-foreground mt-1">Financial overview</p>
</div>
<button
onClick={fetchData}
className="p-2 rounded-lg text-text-muted hover:text-text-primary hover:bg-surface-hover transition-colors"
>
<RefreshCw className={`w-4 h-4 ${loading ? 'animate-spin' : ''}`} />
</button>
<Button variant="ghost" size="icon" onClick={fetchData} title="Refresh" aria-label="Refresh">
<RefreshCw className={cn('w-4 h-4', loading && 'animate-spin')} />
</Button>
</div>
{/* Bank accounts — grouped by currency */}
{(['CRC', 'USD'] as const).map((currency) => {
const accts = bankAccounts
.filter((a) => a.currency === currency)
.sort((a, b) => bankOrder.indexOf(a.bank) - bankOrder.indexOf(b.bank));
{/* Net Worth */}
{netWorthBreakdown != null && (
<Card>
<CardContent className="px-4 py-3">
<div className="flex items-center justify-between text-sm font-mono text-muted-foreground">
<span>Net <span className="text-foreground font-bold">{netWorthBreakdown.net < 0 ? '-' : ''}{formatAmount(netWorthBreakdown.net, 'CRC')}</span></span>
<div className="flex gap-4">
<span>Assets <span className="text-foreground">{formatAmount(netWorthBreakdown.assets, 'CRC')}</span></span>
<span>Liabilities <span className="text-foreground">{formatAmount(netWorthBreakdown.liabilities, 'CRC')}</span></span>
</div>
</div>
</CardContent>
</Card>
)}
{/* Account sections */}
{sortedSections.map(([sectionId, sectionSettings]) => {
const def = SECTION_DEFS[sectionId];
if (!def) return null;
let accts = accounts.filter(def.filterFn);
if (accts.length === 0) return null;
// Sort bank accounts by bank order
if (sectionId === 'crc_accounts' || sectionId === 'usd_accounts') {
accts = accts.sort((a, b) => BANK_ORDER.indexOf(a.bank) - BANK_ORDER.indexOf(b.bank));
}
const total = accts.reduce((s, a) => s + a.balance, 0);
return (
<div key={currency} className="space-y-2">
<h2 className="text-xs font-semibold text-text-muted uppercase tracking-wider">{currency} Accounts</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
{accts.map((a) => <AccountCard key={a.id} account={a} {...cardProps} />)}
<TotalCard total={total} currency={currency} />
</div>
</div>
<DashboardSection
key={sectionId}
sectionId={sectionId}
settings={sectionSettings}
total={def.totalCurrency ? total : undefined}
totalCurrency={def.totalCurrency || undefined}
onToggleExpanded={(expanded) => patchSection(sectionId, { expanded })}
onOpenConfig={() => setConfigSection(sectionId)}
>
{accts.map((a) => (
<AccountRow key={a.id} account={a} {...rowProps} />
))}
</DashboardSection>
);
})}
{/* Pension accounts */}
{pensionAccounts.length > 0 && (
<div className="space-y-2">
<h2 className="text-xs font-semibold text-text-muted uppercase tracking-wider">Pension</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
{pensionAccounts.map((a) => <AccountCard key={a.id} account={a} {...cardProps} />)}
<TotalCard
total={pensionAccounts.reduce((s, a) => s + a.balance, 0)}
currency="CRC"
/>
</div>
</div>
)}
{/* Savings accounts */}
{savingsAccounts.length > 0 && (
<div className="space-y-2">
<h2 className="text-xs font-semibold text-text-muted uppercase tracking-wider">Savings</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
{savingsAccounts.map((a) => <AccountCard key={a.id} account={a} {...cardProps} />)}
<TotalCard
total={savingsAccounts.reduce((s, a) => s + a.balance, 0)}
currency="CRC"
/>
</div>
</div>
)}
{/* Liabilities */}
{liabilityAccounts.length > 0 && (
<div className="space-y-2">
<h2 className="text-xs font-semibold text-text-muted uppercase tracking-wider">Liabilities</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
{liabilityAccounts.map((account) => {
const bankShort = account.bank === 'DAVIVIENDA' ? 'DAV' : account.bank;
return (
<div
key={account.id}
className="animate-fade-in bg-red-50 dark:bg-red-500/5 border border-red-200 dark:border-red-500/20 rounded-xl p-5 shadow-sm dark:shadow-none hover:bg-red-100/50 dark:hover:bg-red-500/10 transition-colors"
>
<div className="flex items-center justify-between mb-2">
<span className="text-xs font-bold text-red-700 dark:text-red-400/80 uppercase tracking-wider">
Balance
</span>
<span className="text-sm font-bold font-mono text-red-600 dark:text-red-400 bg-red-100 dark:bg-red-500/10 px-2.5 py-0.5 rounded">
{bankShort}
</span>
</div>
<p
className="text-2xl font-bold font-mono tracking-tight text-red-700 dark:text-red-400 cursor-pointer group/balance"
onClick={() => startEditing(account)}
>
{editingId === account.id ? (
<span className="flex items-center gap-2">
<input
type="number"
step="0.01"
value={editValue}
onChange={(e) => setEditValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') saveBalance(account.id);
if (e.key === 'Escape') cancelEditing();
}}
autoFocus
className="w-full text-2xl font-bold font-mono tracking-tight bg-input-bg border border-red-500/40 rounded-lg px-2 py-1 focus:outline-none focus:border-red-500 transition-colors text-text-primary"
onClick={(e) => e.stopPropagation()}
/>
<button onClick={(e) => { e.stopPropagation(); saveBalance(account.id); }} className="p-1 text-red-500">
<Check className="w-4 h-4" />
</button>
<button onClick={(e) => { e.stopPropagation(); cancelEditing(); }} className="p-1 text-text-muted">
<X className="w-4 h-4" />
</button>
</span>
) : (
formatAmount(account.balance, account.currency)
)}
</p>
{account.next_payment != null && (
<p className="text-sm font-mono text-red-600/70 dark:text-red-400/60 mt-2">
Next payment: {formatAmount(account.next_payment, account.currency)}
</p>
)}
</div>
);
})}
</div>
</div>
)}
{/* Crypto */}
{cryptoAccounts.length > 0 && (
<div className="space-y-2">
<h2 className="text-xs font-semibold text-text-muted uppercase tracking-wider">Crypto</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
{cryptoAccounts.map((a) => <AccountCard key={a.id} account={a} {...cardProps} />)}
</div>
</div>
)}
{/* Exchange rate + combined total */}
{/* Exchange rate */}
{exchangeRate && (
<div className="flex flex-col sm:flex-row gap-4">
<div className="flex-1 bg-surface-card border border-border rounded-xl p-4">
<span className="text-xs font-medium text-text-muted uppercase tracking-wider">USD/CRC Exchange Rate</span>
<Card>
<CardContent className="p-4">
<span className="text-xs font-medium text-muted-foreground uppercase tracking-wider">USD/CRC Exchange Rate</span>
<div className="flex items-baseline gap-3 mt-1">
<span className="text-lg font-bold font-mono">Buy: {exchangeRate.buy_rate.toFixed(2)}</span>
<span className="text-lg font-bold font-mono text-text-secondary">Sell: {exchangeRate.sell_rate.toFixed(2)}</span>
<span className="text-lg font-bold font-mono text-muted-foreground">Sell: {exchangeRate.sell_rate.toFixed(2)}</span>
</div>
</div>
{accounts.length > 0 && (
<div className="flex-1 bg-gradient-to-br from-violet-500/10 to-[#606C38]/5 border border-violet-500/20 rounded-xl p-4">
<span className="text-xs font-medium text-violet-600 dark:text-violet-400/80 uppercase tracking-wider">Combined Total (CRC)</span>
<p className="text-2xl font-bold font-mono tracking-tight text-violet-600 dark:text-violet-400 mt-1">
{formatAmount(bankCRC + bankUSD * exchangeRate.sell_rate, 'CRC')}
</p>
</div>
)}
</div>
</CardContent>
</Card>
)}
{/* Recent transactions */}
<div className="bg-surface-card border border-border rounded-xl">
<div className="flex items-center justify-between px-5 py-4 border-b border-border-subtle">
<Card>
<CardHeader className="border-b flex-row items-center justify-between">
<div className="flex items-center gap-2">
<CreditCard className="w-4 h-4 text-text-muted" />
<h2 className="font-semibold text-sm">Recent Charges</h2>
<CreditCard className="w-4 h-4 text-muted-foreground" />
<CardTitle className="text-sm">Recent Charges</CardTitle>
</div>
<Link
to="/transactions"
className="flex items-center gap-1 text-xs font-medium text-[#606C38] dark:text-[#7a8a4a] hover:text-[#4a5a2a] dark:hover:text-[#8a9462] transition-colors"
className="flex items-center gap-1 text-xs font-medium text-primary hover:text-primary/80 transition-colors"
>
View all
<ArrowRight className="w-3 h-3" />
</Link>
</div>
{recent.length === 0 && !loading ? (
<div className="px-5 py-12 text-center text-text-faint text-sm">No transactions yet. Add your first one!</div>
) : (
<div className="divide-y divide-border-subtle">
{recent.map((tx) => (
<div key={tx.id} className="flex items-center justify-between px-5 py-3.5 hover:bg-surface-hover transition-colors animate-fade-in">
<div className="flex items-center gap-3 min-w-0">
<div className={`w-8 h-8 rounded-lg flex items-center justify-center flex-shrink-0 ${
tx.transaction_type === 'DEVOLUCION' ? 'bg-[#606C38]/10 text-[#606C38] dark:text-[#7a8a4a]' : 'bg-red-500/10 text-red-500 dark:text-red-400'
}`}>
{tx.transaction_type === 'DEVOLUCION' ? <TrendingUp className="w-4 h-4" /> : <TrendingDown className="w-4 h-4" />}
</div>
<div className="min-w-0">
<p className="text-sm font-medium truncate">{tx.merchant}</p>
<p className="text-xs text-text-muted">
{formatDate(tx.date)}
{tx.category && <span className="ml-2 text-text-faint">{tx.category.name}</span>}
</p>
</CardHeader>
<CardContent className="p-0">
{recent.length === 0 && !loading ? (
<div className="px-5 py-12 text-center text-muted-foreground text-sm">No transactions yet. Add your first one!</div>
) : (
<div className="divide-y divide-border">
{recent.map((tx) => (
<div key={tx.id} className="flex items-center justify-between px-5 py-3.5 hover:bg-muted/50 transition-colors">
<div className="flex items-center gap-3 min-w-0">
<div className={cn(
'w-8 h-8 rounded-lg flex items-center justify-center shrink-0',
tx.transaction_type === 'DEVOLUCION' ? 'bg-primary/10 text-primary' : 'bg-destructive/10 text-destructive'
)}>
{tx.transaction_type === 'DEVOLUCION' ? <TrendingUp className="w-4 h-4" /> : <TrendingDown className="w-4 h-4" />}
</div>
<div className="min-w-0">
<p className="text-sm font-medium truncate">{tx.merchant}</p>
<p className="text-xs text-muted-foreground">
{formatDate(tx.date)}
{tx.category && <span className="ml-2 text-muted-foreground/60">{tx.category.name}</span>}
</p>
</div>
</div>
<span className={cn(
'font-mono text-sm font-medium shrink-0 ml-4',
tx.transaction_type === 'DEVOLUCION' && 'text-primary'
)}>
{tx.transaction_type === 'DEVOLUCION' ? '+' : '-'}{formatAmount(tx.amount, tx.currency)}
</span>
</div>
<span className={`font-mono text-sm font-medium flex-shrink-0 ml-4 ${
tx.transaction_type === 'DEVOLUCION' ? 'text-[#606C38] dark:text-[#7a8a4a]' : ''
}`}>
{tx.transaction_type === 'DEVOLUCION' ? '+' : '-'}{formatAmount(tx.amount, tx.currency)}
</span>
</div>
))}
</div>
)}
</div>
))}
</div>
)}
</CardContent>
</Card>
{/* Section config dialog */}
{configSection && settings.dashboard.sections[configSection] && (
<SectionConfigDialog
sectionId={configSection}
settings={settings.dashboard.sections[configSection]}
open={!!configSection}
onOpenChange={(open) => { if (!open) setConfigSection(null); }}
onSave={(id, partial) => patchSection(id, partial)}
/>
)}
</div>
);
}

View File

@@ -4,6 +4,10 @@ import { Wallet, ArrowRight, AlertCircle } from 'lucide-react';
import { login } from '../api';
import { useAuth } from '../AuthContext';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
export default function Login() {
const [username, setUsername] = useState('');
@@ -29,60 +33,61 @@ export default function Login() {
};
return (
<div className="min-h-screen bg-surface flex items-center justify-center px-4">
<div className="w-full max-w-sm animate-fade-in">
<div className="min-h-screen bg-background flex items-center justify-center px-4">
<div className="w-full max-w-sm">
<div className="flex items-center justify-center gap-2.5 mb-8">
<div className="w-10 h-10 rounded-lg bg-gradient-to-br from-[#606C38] to-[#DDA15E] flex items-center justify-center">
<Wallet className="w-6 h-6 text-[#FEFAE0]" strokeWidth={2.5} />
<div className="w-10 h-10 rounded-lg bg-primary flex items-center justify-center">
<Wallet className="w-6 h-6 text-primary-foreground" strokeWidth={2.5} />
</div>
<span className="text-2xl font-bold tracking-tight">
Wealthy<span className="text-[#606C38] dark:text-[#7a8a4a]">Smart</span>
<span className="text-2xl font-bold tracking-tight font-heading">
Wealthy<span className="text-primary">Smart</span>
</span>
</div>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label className="block text-xs font-medium text-text-muted mb-1.5 uppercase tracking-wider">
Username
</label>
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
className="w-full bg-input-bg border border-border rounded-lg px-4 py-3 text-text-primary placeholder-text-faint focus:outline-none focus:border-[#606C38]/50 focus:ring-1 focus:ring-[#606C38]/20 transition-colors"
placeholder="Enter username"
autoFocus
/>
</div>
<div>
<label className="block text-xs font-medium text-text-muted mb-1.5 uppercase tracking-wider">
Password
</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full bg-input-bg border border-border rounded-lg px-4 py-3 text-text-primary placeholder-text-faint focus:outline-none focus:border-[#606C38]/50 focus:ring-1 focus:ring-[#606C38]/20 transition-colors"
placeholder="Enter password"
/>
</div>
<Card>
<CardHeader>
<CardTitle>Sign in</CardTitle>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="username">Username</Label>
<Input
id="username"
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Enter username"
autoFocus
className="h-10"
/>
</div>
<div className="space-y-2">
<Label htmlFor="password">Password</Label>
<Input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Enter password"
className="h-10"
/>
</div>
{error && (
<div className="flex items-center gap-2 text-red-500 dark:text-red-400 text-sm">
<AlertCircle className="w-4 h-4" />
{error}
</div>
)}
{error && (
<div className="flex items-center gap-2 text-destructive text-sm">
<AlertCircle className="w-4 h-4" />
{error}
</div>
)}
<button
type="submit"
disabled={loading}
className="w-full flex items-center justify-center gap-2 bg-[#606C38] hover:bg-[#7a8a4a] disabled:opacity-50 text-white dark:text-[#FEFAE0] font-semibold px-6 py-3 rounded-lg transition-colors"
>
{loading ? 'Signing in...' : 'Sign in'}
{!loading && <ArrowRight className="w-4 h-4" />}
</button>
</form>
<Button type="submit" disabled={loading} className="w-full h-10">
{loading ? 'Signing in...' : 'Sign in'}
{!loading && <ArrowRight className="w-4 h-4" />}
</Button>
</form>
</CardContent>
</Card>
</div>
</div>
);

View File

@@ -1,20 +1,18 @@
import { useEffect, useState, useCallback } from 'react';
import {
Plus,
Search,
Pencil,
Trash2,
TrendingUp,
TrendingDown,
ChevronDown,
ClipboardPaste,
} from 'lucide-react';
import { Plus, ClipboardPaste } from 'lucide-react';
import api, { type Transaction, type Category } from '../api';
import TransactionModal from '../components/TransactionModal';
import PasteImportModal from '../components/PasteImportModal';
import ConfirmDialog from '../components/ConfirmDialog';
import BillingCycleSelector from '../components/BillingCycleSelector';
import TransactionList from '../components/TransactionList';
import { Button } from '@/components/ui/button';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
function formatAmount(amount: number, currency: string) {
const abs = Math.abs(amount);
@@ -30,11 +28,7 @@ export default function Transactions() {
const [search, setSearch] = useState('');
const [categoryFilter, setCategoryFilter] = useState('');
const [loading, setLoading] = useState(true);
const [modalOpen, setModalOpen] = useState(false);
const [importOpen, setImportOpen] = useState(false);
const [editing, setEditing] = useState<Transaction | null>(null);
const [deleteId, setDeleteId] = useState<number | null>(null);
const [deleting, setDeleting] = useState(false);
const [cycle, setCycle] = useState<{ year: number; month: number } | null>(null);
const fetchTransactions = useCallback(async () => {
@@ -63,18 +57,6 @@ export default function Transactions() {
return () => clearTimeout(timer);
}, [fetchTransactions]);
const handleDelete = async () => {
if (deleteId === null) return;
setDeleting(true);
try {
await api.delete(`/transactions/${deleteId}`);
setDeleteId(null);
fetchTransactions();
} finally {
setDeleting(false);
}
};
const totalCRC = transactions
.filter((tx) => tx.currency === 'CRC')
.reduce((sum, tx) => sum + (tx.transaction_type === 'DEVOLUCION' ? -tx.amount : tx.amount), 0);
@@ -86,187 +68,57 @@ export default function Transactions() {
<div className="space-y-5">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
<h1 className="text-2xl font-bold">Credit Card Transactions</h1>
<p className="text-sm text-text-muted mt-1">
<h1 className="text-2xl font-bold font-heading">Credit Card Transactions</h1>
<p className="text-sm text-muted-foreground mt-1">
{transactions.length} transactions
{totalCRC !== 0 && (
<> &middot; <span className="font-mono text-text-primary">{formatAmount(totalCRC, 'CRC')}</span></>
<> &middot; <span className="font-mono text-foreground">{formatAmount(totalCRC, 'CRC')}</span></>
)}
{totalUSD !== 0 && (
<> &middot; <span className="font-mono text-text-primary">{formatAmount(totalUSD, 'USD')}</span></>
<> &middot; <span className="font-mono text-foreground">{formatAmount(totalUSD, 'USD')}</span></>
)}
</p>
</div>
<div className="flex gap-2">
<button
onClick={() => setImportOpen(true)}
className="flex items-center gap-2 border border-border hover:bg-surface-hover text-text-secondary font-semibold px-4 py-2.5 rounded-lg text-sm transition-colors"
>
<Button variant="outline" onClick={() => setImportOpen(true)}>
<ClipboardPaste className="w-4 h-4" />
Import
</button>
<button
onClick={() => {
setEditing(null);
setModalOpen(true);
}}
className="flex items-center gap-2 bg-[#606C38] hover:bg-[#7a8a4a] text-white dark:text-[#FEFAE0] font-semibold px-4 py-2.5 rounded-lg text-sm transition-colors"
>
<Plus className="w-4 h-4" />
Add Transaction
</button>
</Button>
</div>
</div>
{/* Billing cycle */}
<BillingCycleSelector value={cycle} onChange={setCycle} />
{/* Filters */}
<div className="flex flex-col sm:flex-row gap-3">
<div className="relative flex-1">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted" />
<input
value={search}
onChange={(e) => setSearch(e.target.value)}
className="w-full bg-input-bg border border-border rounded-lg pl-10 pr-4 py-2.5 text-sm text-text-primary placeholder-text-faint focus:outline-none focus:border-[#606C38]/50 transition-colors"
placeholder="Search merchants..."
/>
</div>
<div className="relative">
<select
value={categoryFilter}
onChange={(e) => setCategoryFilter(e.target.value)}
className="appearance-none bg-input-bg border border-border rounded-lg pl-4 pr-10 py-2.5 text-sm text-text-primary focus:outline-none focus:border-[#606C38]/50 transition-colors"
>
<option value="">All Categories</option>
{/* Category filter */}
<div className="flex items-center gap-3">
<Select
value={categoryFilter || 'all'}
onValueChange={(v) => setCategoryFilter(v === 'all' ? '' : v)}
>
<SelectTrigger className="w-[180px]">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="all">All Categories</SelectItem>
{categories.map((c) => (
<option key={c.id} value={c.id}>
<SelectItem key={c.id} value={String(c.id)}>
{c.name}
</option>
</SelectItem>
))}
</select>
<ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted pointer-events-none" />
</div>
</SelectContent>
</Select>
</div>
{/* Table */}
<div className="bg-surface-card border border-border rounded-xl overflow-hidden">
<div className="overflow-x-auto">
<table className="w-full text-sm">
<thead>
<tr className="border-b border-border-subtle">
<th className="text-left px-5 py-3 text-xs font-medium text-text-muted uppercase tracking-wider">
Date
</th>
<th className="text-left px-5 py-3 text-xs font-medium text-text-muted uppercase tracking-wider">
Merchant
</th>
<th className="text-left px-5 py-3 text-xs font-medium text-text-muted uppercase tracking-wider hidden md:table-cell">
Category
</th>
<th className="text-right px-5 py-3 text-xs font-medium text-text-muted uppercase tracking-wider">
Amount
</th>
<th className="text-right px-5 py-3 text-xs font-medium text-text-muted uppercase tracking-wider w-20">
Actions
</th>
</tr>
</thead>
<tbody className="divide-y divide-border-subtle">
{transactions.map((tx) => (
<tr
key={tx.id}
className="hover:bg-surface-hover transition-colors group"
>
<td className="px-5 py-3 whitespace-nowrap">
<span className="font-mono text-text-secondary text-xs">
{new Date(tx.date).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
})}
</span>
</td>
<td className="px-5 py-3">
<div className="flex items-center gap-2">
<div
className={`w-6 h-6 rounded flex items-center justify-center flex-shrink-0 ${
tx.transaction_type === 'DEVOLUCION'
? 'bg-[#606C38]/10 text-[#606C38] dark:text-[#7a8a4a]'
: 'bg-red-500/10 text-red-500 dark:text-red-400'
}`}
>
{tx.transaction_type === 'DEVOLUCION' ? (
<TrendingUp className="w-3 h-3" />
) : (
<TrendingDown className="w-3 h-3" />
)}
</div>
<span className="truncate max-w-[200px] sm:max-w-none">{tx.merchant}</span>
</div>
</td>
<td className="px-5 py-3 hidden md:table-cell">
{tx.category ? (
<span className="text-xs bg-surface-hover text-text-secondary px-2 py-1 rounded">
{tx.category.name}
</span>
) : (
<span className="text-xs text-text-faint"></span>
)}
</td>
<td className="px-5 py-3 text-right whitespace-nowrap">
<span
className={`font-mono font-medium ${
tx.transaction_type === 'DEVOLUCION'
? 'text-[#606C38] dark:text-[#7a8a4a]'
: ''
}`}
>
{tx.transaction_type === 'DEVOLUCION' ? '+' : '-'}
{formatAmount(tx.amount, tx.currency)}
</span>
</td>
<td className="px-5 py-3 text-right">
<div className="flex items-center justify-end gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<button
onClick={() => {
setEditing(tx);
setModalOpen(true);
}}
className="p-1.5 rounded hover:bg-surface-hover text-text-muted hover:text-text-primary transition-colors"
>
<Pencil className="w-3.5 h-3.5" />
</button>
<button
onClick={() => setDeleteId(tx.id)}
className="p-1.5 rounded hover:bg-red-500/10 text-text-muted hover:text-red-500 dark:hover:text-red-400 transition-colors"
>
<Trash2 className="w-3.5 h-3.5" />
</button>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
{transactions.length === 0 && !loading && (
<div className="px-5 py-16 text-center text-text-faint text-sm">
No transactions found
</div>
)}
</div>
{modalOpen && (
<TransactionModal
transaction={editing}
source="CREDIT_CARD"
onClose={() => setModalOpen(false)}
onSaved={fetchTransactions}
/>
)}
<TransactionList
transactions={transactions}
loading={loading}
source="CREDIT_CARD"
search={search}
onSearchChange={setSearch}
onRefresh={fetchTransactions}
showCategory
/>
{importOpen && (
<PasteImportModal
@@ -274,16 +126,6 @@ export default function Transactions() {
onImported={fetchTransactions}
/>
)}
{deleteId !== null && (
<ConfirmDialog
title="Delete Transaction"
message="This transaction will be permanently deleted. This action cannot be undone."
onConfirm={handleDelete}
onCancel={() => setDeleteId(null)}
loading={deleting}
/>
)}
</div>
);
}

View File

@@ -1,17 +1,9 @@
import { useEffect, useState, useCallback } from 'react';
import { Plus, Search, Pencil, Trash2, ArrowLeftRight } from 'lucide-react';
import { ArrowLeftRight } from 'lucide-react';
import api, { type Transaction } from '../api';
import TransactionModal from '../components/TransactionModal';
import ConfirmDialog from '../components/ConfirmDialog';
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 })}`;
}
import TransactionList from '../components/TransactionList';
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs';
type SourceTab = 'CASH' | 'TRANSFER';
@@ -20,10 +12,6 @@ export default function Transfers() {
const [search, setSearch] = useState('');
const [sourceTab, setSourceTab] = useState<SourceTab>('CASH');
const [loading, setLoading] = useState(true);
const [modalOpen, setModalOpen] = useState(false);
const [editing, setEditing] = useState<Transaction | null>(null);
const [deleteId, setDeleteId] = useState<number | null>(null);
const [deleting, setDeleting] = useState(false);
const fetchTransactions = useCallback(async () => {
setLoading(true);
@@ -42,142 +30,36 @@ export default function Transfers() {
return () => clearTimeout(timer);
}, [fetchTransactions]);
const handleDelete = async () => {
if (deleteId === null) return;
setDeleting(true);
try {
await api.delete(`/transactions/${deleteId}`);
setDeleteId(null);
fetchTransactions();
} finally {
setDeleting(false);
}
};
return (
<div className="space-y-5">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
<h1 className="text-2xl font-bold">Cash & Transfers</h1>
<p className="text-sm text-text-muted mt-1">
Track non-credit-card expenses
</p>
</div>
<button
onClick={() => {
setEditing(null);
setModalOpen(true);
}}
className="flex items-center gap-2 bg-[#606C38] hover:bg-[#7a8a4a] text-white dark:text-[#FEFAE0] font-semibold px-4 py-2.5 rounded-lg text-sm transition-colors"
>
<Plus className="w-4 h-4" />
Add {sourceTab === 'CASH' ? 'Cash Expense' : 'Transfer'}
</button>
<div>
<h1 className="text-2xl font-bold font-heading">Cash & Transfers</h1>
<p className="text-sm text-muted-foreground mt-1">
Track non-credit-card expenses
</p>
</div>
{/* Source tabs */}
<div className="flex gap-1 bg-surface-card border border-border rounded-lg p-1 w-fit">
{(['CASH', 'TRANSFER'] as const).map((tab) => (
<button
key={tab}
onClick={() => setSourceTab(tab)}
className={`px-4 py-2 rounded-md text-sm font-medium transition-colors ${
sourceTab === tab
? 'bg-[#606C38]/10 text-[#606C38] dark:text-[#7a8a4a]'
: 'text-text-muted hover:text-text-primary'
}`}
>
{tab === 'CASH' ? 'Cash' : 'Transfers'}
</button>
))}
</div>
<Tabs value={sourceTab} onValueChange={(v) => setSourceTab(v as SourceTab)}>
<TabsList>
<TabsTrigger value="CASH">Cash</TabsTrigger>
<TabsTrigger value="TRANSFER">Transfers</TabsTrigger>
</TabsList>
{/* Search */}
<div className="relative max-w-sm">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted" />
<input
value={search}
onChange={(e) => setSearch(e.target.value)}
className="w-full bg-input-bg border border-border rounded-lg pl-10 pr-4 py-2.5 text-sm text-text-primary placeholder-text-faint focus:outline-none focus:border-[#606C38]/50 transition-colors"
placeholder="Search..."
/>
</div>
{/* List */}
<div className="bg-surface-card border border-border rounded-xl divide-y divide-border-subtle">
{transactions.length === 0 && !loading ? (
<div className="px-5 py-16 text-center text-text-faint text-sm">
<ArrowLeftRight className="w-8 h-8 mx-auto mb-3 text-text-faint" />
No {sourceTab.toLowerCase()} transactions yet
</div>
) : (
<>
{transactions.map((tx) => (
<div
key={tx.id}
className="flex items-center justify-between px-5 py-4 hover:bg-surface-hover transition-colors group"
>
<div className="min-w-0">
<p className="text-sm font-medium truncate">{tx.merchant}</p>
<p className="text-xs text-text-muted mt-0.5">
{new Date(tx.date).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
})}
{tx.category && (
<span className="ml-2 bg-surface-hover text-text-secondary px-2 py-0.5 rounded">
{tx.category.name}
</span>
)}
</p>
</div>
<div className="flex items-center gap-3 flex-shrink-0 ml-4">
<span className="font-mono text-sm font-medium">
{formatAmount(tx.amount, tx.currency)}
</span>
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<button
onClick={() => {
setEditing(tx);
setModalOpen(true);
}}
className="p-1.5 rounded hover:bg-surface-hover text-text-muted hover:text-text-primary transition-colors"
>
<Pencil className="w-3.5 h-3.5" />
</button>
<button
onClick={() => setDeleteId(tx.id)}
className="p-1.5 rounded hover:bg-red-500/10 text-text-muted hover:text-red-500 dark:hover:text-red-400 transition-colors"
>
<Trash2 className="w-3.5 h-3.5" />
</button>
</div>
</div>
</div>
))}
</>
)}
</div>
{modalOpen && (
<TransactionModal
transaction={editing}
source={sourceTab}
onClose={() => setModalOpen(false)}
onSaved={fetchTransactions}
/>
)}
{deleteId !== null && (
<ConfirmDialog
title="Delete Transaction"
message="This transaction will be permanently deleted. This action cannot be undone."
onConfirm={handleDelete}
onCancel={() => setDeleteId(null)}
loading={deleting}
/>
)}
<TabsContent value={sourceTab} className="mt-5 space-y-5">
<TransactionList
transactions={transactions}
loading={loading}
source={sourceTab}
search={search}
onSearchChange={setSearch}
onRefresh={fetchTransactions}
showCategory={false}
addLabel={sourceTab === 'CASH' ? 'Add Cash Expense' : 'Add Transfer'}
emptyIcon={<ArrowLeftRight className="w-8 h-8 mx-auto mb-3 text-muted-foreground/50" />}
emptyMessage={`No ${sourceTab.toLowerCase()} transactions yet`}
/>
</TabsContent>
</Tabs>
</div>
);
}