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

@@ -56,6 +56,34 @@ export interface ImportResult {
errors: string[]; errors: string[];
} }
// --- User Settings ---
export interface SectionSettings {
label: string;
color: string;
cardColor: string;
visible: boolean;
order: number;
expanded: boolean;
}
export interface DashboardSettings {
sections: Record<string, SectionSettings>;
}
export interface UserSettingsData {
dashboard: DashboardSettings;
}
export interface UserSettingsResponse {
key: string;
data: UserSettingsData;
updated_at: string;
}
export const getSettings = () => api.get<UserSettingsResponse>('/settings/');
export const updateSettings = (data: UserSettingsData) => api.patch<UserSettingsResponse>('/settings/', { data });
export interface Transaction { export interface Transaction {
id: number; id: number;
amount: number; amount: number;

View File

@@ -1,6 +1,13 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { Calendar, ChevronDown } from 'lucide-react'; import { Calendar } from 'lucide-react';
import api from '../api'; import api from '../api';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
export interface CycleOption { export interface CycleOption {
year: number; year: number;
@@ -22,33 +29,34 @@ export default function BillingCycleSelector({ value, onChange }: Props) {
api.get('/transactions/cycles').then((r) => setCycles(r.data)); api.get('/transactions/cycles').then((r) => setCycles(r.data));
}, []); }, []);
const selectedKey = value ? `${value.year}-${value.month}` : ''; const selectedKey = value ? `${value.year}-${value.month}` : 'all';
return ( return (
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<Calendar className="w-4 h-4 text-text-muted" /> <Calendar className="w-4 h-4 text-muted-foreground" />
<div className="relative"> <Select
<select value={selectedKey}
value={selectedKey} onValueChange={(val) => {
onChange={(e) => { if (val === 'all') {
if (!e.target.value) { onChange(null);
onChange(null); } else {
} else { const [y, m] = val.split('-').map(Number);
const [y, m] = e.target.value.split('-').map(Number); onChange({ year: y, month: m });
onChange({ year: y, month: m }); }
} }}
}} >
className="appearance-none bg-input-bg border border-border rounded-lg pl-3 pr-9 py-2 text-sm text-text-primary focus:outline-none focus:border-[#606C38]/50 transition-colors" <SelectTrigger className="w-[180px]">
> <SelectValue />
<option value="">All time</option> </SelectTrigger>
<SelectContent>
<SelectItem value="all">All time</SelectItem>
{cycles.map((c) => ( {cycles.map((c) => (
<option key={`${c.year}-${c.month}`} value={`${c.year}-${c.month}`}> <SelectItem key={`${c.year}-${c.month}`} value={`${c.year}-${c.month}`}>
{c.label} ({c.count}) {c.label} ({c.count})
</option> </SelectItem>
))} ))}
</select> </SelectContent>
<ChevronDown className="absolute right-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted pointer-events-none" /> </Select>
</div>
</div> </div>
); );
} }

View File

@@ -1,4 +1,15 @@
import { AlertTriangle, X } from 'lucide-react'; import { AlertTriangle } from 'lucide-react';
import {
AlertDialog,
AlertDialogContent,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
AlertDialogMedia,
} from '@/components/ui/alert-dialog';
interface Props { interface Props {
title: string; title: string;
@@ -11,45 +22,26 @@ interface Props {
export default function ConfirmDialog({ title, message, confirmLabel = 'Delete', onConfirm, onCancel, loading }: Props) { export default function ConfirmDialog({ title, message, confirmLabel = 'Delete', onConfirm, onCancel, loading }: Props) {
return ( return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4" onClick={onCancel}> <AlertDialog open onOpenChange={(open) => { if (!open) onCancel(); }}>
<div <AlertDialogContent>
className="bg-surface border border-border rounded-xl w-full max-w-sm animate-fade-in" <AlertDialogHeader>
onClick={(e) => e.stopPropagation()} <AlertDialogMedia className="bg-destructive/10">
> <AlertTriangle className="text-destructive" />
<div className="flex items-center justify-between px-5 py-4 border-b border-border"> </AlertDialogMedia>
<h3 className="font-semibold">{title}</h3> <AlertDialogTitle>{title}</AlertDialogTitle>
<button onClick={onCancel} className="text-text-muted hover:text-text-primary transition-colors"> <AlertDialogDescription>{message}</AlertDialogDescription>
<X className="w-5 h-5" /> </AlertDialogHeader>
</button> <AlertDialogFooter>
</div> <AlertDialogCancel onClick={onCancel}>Cancel</AlertDialogCancel>
<AlertDialogAction
<div className="px-5 py-5"> variant="destructive"
<div className="flex gap-3 items-start">
<div className="w-10 h-10 rounded-full bg-red-500/10 flex items-center justify-center flex-shrink-0">
<AlertTriangle className="w-5 h-5 text-red-500 dark:text-red-400" />
</div>
<p className="text-sm text-text-secondary pt-2">{message}</p>
</div>
</div>
<div className="flex gap-3 px-5 pb-5">
<button
type="button"
onClick={onCancel}
className="flex-1 px-4 py-2.5 rounded-lg text-sm font-medium text-text-secondary border border-border hover:bg-surface-hover transition-colors"
>
Cancel
</button>
<button
type="button"
onClick={onConfirm} onClick={onConfirm}
disabled={loading} disabled={loading}
className="flex-1 px-4 py-2.5 rounded-lg text-sm font-medium bg-red-500 hover:bg-red-600 text-white transition-colors disabled:opacity-50"
> >
{loading ? 'Deleting...' : confirmLabel} {loading ? 'Deleting...' : confirmLabel}
</button> </AlertDialogAction>
</div> </AlertDialogFooter>
</div> </AlertDialogContent>
</div> </AlertDialog>
); );
} }

View File

@@ -0,0 +1,76 @@
import { Settings } from 'lucide-react';
import type { SectionSettings } from '../api';
import { formatAmount } from '@/lib/format';
import { Card } from '@/components/ui/card';
import {
Accordion,
AccordionItem,
AccordionTrigger,
AccordionContent,
} from '@/components/ui/accordion';
import { getColorClasses } from '@/lib/colors';
import { cn } from '@/lib/utils';
interface Props {
sectionId: string;
settings: SectionSettings;
total?: number;
totalCurrency?: string;
onToggleExpanded: (expanded: boolean) => void;
onOpenConfig: () => void;
children: React.ReactNode;
}
export default function DashboardSection({
sectionId,
settings,
total,
totalCurrency,
onToggleExpanded,
onOpenConfig,
children,
}: Props) {
const colors = getColorClasses(settings.color);
return (
<Card className={cn('relative overflow-hidden border-l-4', colors.borderLeft)}>
{/* Settings icon — outside accordion trigger to avoid button-in-button */}
<button
onClick={onOpenConfig}
className="absolute top-2.5 right-3 z-10 p-1 rounded text-muted-foreground hover:text-foreground hover:bg-muted/50 transition-colors cursor-pointer"
title="Section settings"
aria-label="Section settings"
>
<Settings className="w-3.5 h-3.5" />
</button>
<Accordion
value={settings.expanded ? [sectionId] : []}
onValueChange={(value: string[]) => onToggleExpanded(value.includes(sectionId))}
>
<AccordionItem value={sectionId} className="border-none">
<AccordionTrigger
className="px-4 py-3 hover:no-underline cursor-pointer"
aria-label={`Expand ${settings.label}`}
>
<div className="flex items-center justify-between w-full pr-8">
<span className="text-sm font-semibold text-foreground">
{settings.label}
</span>
{total != null && totalCurrency && (
<span className="text-sm font-bold font-mono text-foreground">
{formatAmount(total, totalCurrency)}
</span>
)}
</div>
</AccordionTrigger>
<AccordionContent>
<div className="divide-y divide-border mx-4 mb-4 rounded-lg overflow-hidden">
{children}
</div>
</AccordionContent>
</AccordionItem>
</Accordion>
</Card>
);
}

View File

@@ -7,13 +7,22 @@ import {
LogOut, LogOut,
Wallet, Wallet,
Menu, Menu,
X,
Sun, Sun,
Moon, Moon,
} from 'lucide-react'; } from 'lucide-react';
import { useState } from 'react'; import { useState } from 'react';
import { useAuth } from '../AuthContext'; import { useAuth } from '../AuthContext';
import { useTheme } from '../ThemeContext'; import { useTheme } from '../ThemeContext';
import { Button } from '@/components/ui/button';
import {
Sheet,
SheetContent,
SheetHeader,
SheetTitle,
SheetClose,
} from '@/components/ui/sheet';
import { Separator } from '@/components/ui/separator';
import { cn } from '@/lib/utils';
const navItems = [ const navItems = [
{ to: '/', icon: LayoutDashboard, label: 'Dashboard' }, { to: '/', icon: LayoutDashboard, label: 'Dashboard' },
@@ -34,16 +43,16 @@ export default function Layout() {
}; };
return ( return (
<div className="min-h-screen bg-surface text-text-primary"> <div className="min-h-screen bg-background text-foreground">
{/* Top bar */} {/* Top bar */}
<header className="border-b border-border backdrop-blur-sm sticky top-0 z-50 bg-surface/90"> <header className="border-b border-border backdrop-blur-sm sticky top-0 z-50 bg-background/90">
<div className="max-w-7xl mx-auto px-4 sm:px-6 py-3 flex items-center justify-between"> <div className="max-w-7xl mx-auto px-4 sm:px-6 py-3 flex items-center justify-between">
<div className="flex items-center gap-2.5"> <div className="flex items-center gap-2.5">
<div className="w-8 h-8 rounded-lg bg-gradient-to-br from-[#606C38] to-[#DDA15E] flex items-center justify-center"> <div className="w-8 h-8 rounded-lg bg-primary flex items-center justify-center">
<Wallet className="w-4 h-4 text-[#FEFAE0]" strokeWidth={2.5} /> <Wallet className="w-4 h-4 text-primary-foreground" strokeWidth={2.5} />
</div> </div>
<span className="text-lg font-bold tracking-tight hidden sm:inline"> <span className="text-lg font-bold tracking-tight hidden sm:inline font-heading">
Wealthy<span className="text-[#606C38] dark:text-[#7a8a4a]">Smart</span> Wealthy<span className="text-primary">Smart</span>
</span> </span>
</div> </div>
@@ -55,11 +64,12 @@ export default function Layout() {
to={to} to={to}
end={to === '/'} end={to === '/'}
className={({ isActive }) => className={({ isActive }) =>
`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition-colors ${ cn(
'flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition-colors',
isActive isActive
? 'bg-[#606C38]/10 text-[#606C38] dark:text-[#7a8a4a]' ? 'bg-primary/10 text-primary'
: 'text-text-muted hover:text-text-primary hover:bg-surface-hover' : 'text-muted-foreground hover:text-foreground hover:bg-muted'
}` )
} }
> >
<Icon className="w-4 h-4" /> <Icon className="w-4 h-4" />
@@ -68,59 +78,80 @@ export default function Layout() {
))} ))}
</nav> </nav>
<div className="flex items-center gap-2"> <div className="flex items-center gap-1">
<button <Button variant="ghost" size="icon" onClick={toggleTheme} title="Toggle theme" aria-label="Toggle theme">
onClick={toggleTheme}
className="p-2 rounded-lg text-text-muted hover:text-text-primary hover:bg-surface-hover transition-colors"
>
{theme === 'dark' ? <Sun className="w-4 h-4" /> : <Moon className="w-4 h-4" />} {theme === 'dark' ? <Sun className="w-4 h-4" /> : <Moon className="w-4 h-4" />}
</button> </Button>
<button <Button
variant="ghost"
size="icon"
onClick={handleLogout} onClick={handleLogout}
className="hidden md:flex items-center gap-2 text-text-muted hover:text-text-secondary text-sm transition-colors" title="Sign out"
aria-label="Sign out"
className="hidden md:inline-flex"
> >
<LogOut className="w-4 h-4" /> <LogOut className="w-4 h-4" />
</button> </Button>
<button <Button
onClick={() => setMobileOpen(!mobileOpen)} variant="ghost"
className="md:hidden text-text-muted" size="icon"
onClick={() => setMobileOpen(true)}
title="Open menu"
aria-label="Open menu"
className="md:hidden"
> >
{mobileOpen ? <X className="w-5 h-5" /> : <Menu className="w-5 h-5" />} <Menu className="w-5 h-5" />
</button> </Button>
</div> </div>
</div> </div>
</header>
{/* Mobile nav */} {/* Mobile nav sheet */}
{mobileOpen && ( <Sheet open={mobileOpen} onOpenChange={setMobileOpen}>
<div className="md:hidden border-t border-border px-4 pb-4 space-y-1"> <SheetContent side="left" className="p-0">
<SheetHeader className="p-4">
<SheetTitle className="flex items-center gap-2.5">
<div className="w-8 h-8 rounded-lg bg-primary flex items-center justify-center">
<Wallet className="w-4 h-4 text-primary-foreground" strokeWidth={2.5} />
</div>
<span className="font-heading">
Wealthy<span className="text-primary">Smart</span>
</span>
</SheetTitle>
</SheetHeader>
<Separator />
<nav className="flex flex-col gap-1 p-4">
{navItems.map(({ to, icon: Icon, label }) => ( {navItems.map(({ to, icon: Icon, label }) => (
<NavLink <SheetClose key={to} render={<span />}>
key={to} <NavLink
to={to} to={to}
end={to === '/'} end={to === '/'}
onClick={() => setMobileOpen(false)} onClick={() => setMobileOpen(false)}
className={({ isActive }) => className={({ isActive }) =>
`flex items-center gap-3 px-4 py-3 rounded-lg text-sm font-medium transition-colors ${ cn(
isActive 'flex items-center gap-3 px-4 py-3 rounded-lg text-sm font-medium transition-colors',
? 'bg-[#606C38]/10 text-[#606C38] dark:text-[#7a8a4a]' isActive
: 'text-text-muted hover:text-text-primary hover:bg-surface-hover' ? 'bg-primary/10 text-primary'
}` : 'text-muted-foreground hover:text-foreground hover:bg-muted'
} )
> }
<Icon className="w-4 h-4" /> >
{label} <Icon className="w-4 h-4" />
</NavLink> {label}
</NavLink>
</SheetClose>
))} ))}
<Separator className="my-2" />
<button <button
onClick={handleLogout} onClick={handleLogout}
className="flex items-center gap-3 px-4 py-3 rounded-lg text-sm font-medium text-text-muted hover:text-text-primary hover:bg-surface-hover w-full" className="flex items-center gap-3 px-4 py-3 rounded-lg text-sm font-medium text-muted-foreground hover:text-foreground hover:bg-muted w-full"
> >
<LogOut className="w-4 h-4" /> <LogOut className="w-4 h-4" />
Sign out Sign out
</button> </button>
</div> </nav>
)} </SheetContent>
</header> </Sheet>
<main className="max-w-7xl mx-auto px-4 sm:px-6 py-6"> <main className="max-w-7xl mx-auto px-4 sm:px-6 py-6">
<Outlet /> <Outlet />

View File

@@ -1,6 +1,24 @@
import { useState } from 'react'; import { useState } from 'react';
import { X, ClipboardPaste, CheckCircle, AlertTriangle } from 'lucide-react'; import { ClipboardPaste, CheckCircle, AlertTriangle } from 'lucide-react';
import api, { type ImportResult } from '../api'; import api, { type ImportResult } from '../api';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogFooter,
} from '@/components/ui/dialog';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
interface Props { interface Props {
onClose: () => void; onClose: () => void;
@@ -28,114 +46,100 @@ export default function PasteImportModal({ onClose, onImported }: Props) {
} }
}; };
const inputClass =
'w-full bg-input-bg border border-border rounded-lg px-3 py-2.5 text-sm text-text-primary placeholder-text-faint focus:outline-none focus:border-[#606C38]/50 focus:ring-1 focus:ring-[#606C38]/20 transition-colors';
const labelClass = 'block text-xs font-medium text-text-secondary mb-1 uppercase tracking-wider';
return ( return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4"> <Dialog open onOpenChange={(open) => { if (!open) onClose(); }}>
<div className="bg-surface border border-border rounded-xl w-full max-w-2xl max-h-[90vh] overflow-y-auto"> <DialogContent className="sm:max-w-2xl max-h-[90vh] overflow-y-auto">
<div className="flex items-center justify-between px-5 py-4 border-b border-border"> <DialogHeader>
<div className="flex items-center gap-2"> <DialogTitle className="flex items-center gap-2">
<ClipboardPaste className="w-4 h-4 text-[#606C38] dark:text-[#7a8a4a]" /> <ClipboardPaste className="w-4 h-4 text-primary" />
<h3 className="font-semibold">Import Bank Statement</h3> Import Bank Statement
</DialogTitle>
</DialogHeader>
{!result ? (
<div className="space-y-4">
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label>Bank</Label>
<Select value={bank} onValueChange={setBank}>
<SelectTrigger className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="BAC">BAC</SelectItem>
<SelectItem value="BCR">BCR</SelectItem>
<SelectItem value="DAVIVIENDA">Davivienda</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label>Source</Label>
<Select value={source} onValueChange={setSource}>
<SelectTrigger className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="CREDIT_CARD">Credit Card</SelectItem>
<SelectItem value="CASH">Cash</SelectItem>
<SelectItem value="TRANSFER">Transfer</SelectItem>
</SelectContent>
</Select>
</div>
</div>
<div className="space-y-2">
<Label>Statement Text</Label>
<Textarea
className="h-48 font-mono text-xs resize-y"
value={text}
onChange={(e) => setText(e.target.value)}
placeholder={`Paste bank statement lines here...\n\nFormat: DD/MM/YYYY\tMERCHANT\\CITY\\COUNTRY\tAMOUNT CRC`}
/>
<p className="text-xs text-muted-foreground">
One transaction per line. Tab-separated columns.
</p>
</div>
<DialogFooter>
<Button variant="outline" onClick={onClose}>
Cancel
</Button>
<Button onClick={handleImport} disabled={importing || !text.trim()}>
{importing ? 'Importing...' : 'Import'}
</Button>
</DialogFooter>
</div> </div>
<button onClick={onClose} className="text-text-muted hover:text-text-primary transition-colors"> ) : (
<X className="w-5 h-5" /> <div className="space-y-4">
</button> <Alert>
</div> <CheckCircle className="h-4 w-4 text-primary" />
<AlertTitle className="text-primary">Import Complete</AlertTitle>
<AlertDescription>
{result.imported} imported
{result.duplicates > 0 && ` · ${result.duplicates} duplicates skipped`}
</AlertDescription>
</Alert>
<div className="p-5 space-y-4"> {result.errors.length > 0 && (
{!result ? ( <Alert variant="destructive">
<> <AlertTriangle className="h-4 w-4" />
<div className="grid grid-cols-2 gap-4"> <AlertTitle>{result.errors.length} errors</AlertTitle>
<div> <AlertDescription>
<label className={labelClass}>Bank</label> <ul className="text-xs font-mono max-h-32 overflow-y-auto space-y-1 mt-1">
<select className={inputClass} value={bank} onChange={(e) => setBank(e.target.value)}>
<option value="BAC">BAC</option>
<option value="BCR">BCR</option>
<option value="DAVIVIENDA">Davivienda</option>
</select>
</div>
<div>
<label className={labelClass}>Source</label>
<select className={inputClass} value={source} onChange={(e) => setSource(e.target.value)}>
<option value="CREDIT_CARD">Credit Card</option>
<option value="CASH">Cash</option>
<option value="TRANSFER">Transfer</option>
</select>
</div>
</div>
<div>
<label className={labelClass}>Statement Text</label>
<textarea
className={`${inputClass} h-48 font-mono text-xs resize-y`}
value={text}
onChange={(e) => setText(e.target.value)}
placeholder={`Paste bank statement lines here...\n\nFormat: DD/MM/YYYY\tMERCHANT\\CITY\\COUNTRY\tAMOUNT CRC`}
/>
<p className="text-xs text-text-faint mt-1">
One transaction per line. Tab-separated columns.
</p>
</div>
<div className="flex gap-3 pt-2">
<button
type="button"
onClick={onClose}
className="flex-1 px-4 py-2.5 rounded-lg text-sm font-medium text-text-secondary border border-border hover:bg-surface-hover transition-colors"
>
Cancel
</button>
<button
onClick={handleImport}
disabled={importing || !text.trim()}
className="flex-1 px-4 py-2.5 rounded-lg text-sm font-medium bg-[#606C38] hover:bg-[#7a8a4a] text-white dark:text-[#FEFAE0] transition-colors disabled:opacity-50"
>
{importing ? 'Importing...' : 'Import'}
</button>
</div>
</>
) : (
<div className="space-y-4">
<div className="flex items-center gap-3 p-4 bg-[#606C38]/10 border border-[#606C38]/20 rounded-lg">
<CheckCircle className="w-5 h-5 text-[#606C38] dark:text-[#7a8a4a] flex-shrink-0" />
<div>
<p className="font-medium text-[#606C38] dark:text-[#7a8a4a]">Import Complete</p>
<p className="text-sm text-text-secondary mt-1">
{result.imported} imported
{result.duplicates > 0 && ` · ${result.duplicates} duplicates skipped`}
</p>
</div>
</div>
{result.errors.length > 0 && (
<div className="p-4 bg-amber-500/10 border border-amber-500/20 rounded-lg">
<div className="flex items-center gap-2 mb-2">
<AlertTriangle className="w-4 h-4 text-amber-500 dark:text-amber-400" />
<span className="text-sm font-medium text-amber-600 dark:text-amber-400">
{result.errors.length} errors
</span>
</div>
<ul className="text-xs text-text-secondary space-y-1 font-mono max-h-32 overflow-y-auto">
{result.errors.map((err, i) => ( {result.errors.map((err, i) => (
<li key={i}>{err}</li> <li key={i}>{err}</li>
))} ))}
</ul> </ul>
</div> </AlertDescription>
)} </Alert>
)}
<button <Button onClick={onClose} className="w-full">
onClick={onClose} Done
className="w-full px-4 py-2.5 rounded-lg text-sm font-medium bg-[#606C38] hover:bg-[#7a8a4a] text-white dark:text-[#FEFAE0] transition-colors" </Button>
> </div>
Done )}
</button> </DialogContent>
</div> </Dialog>
)}
</div>
</div>
</div>
); );
} }

View File

@@ -0,0 +1,137 @@
import { useState } from 'react';
import type { SectionSettings } from '../api';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogFooter,
} from '@/components/ui/dialog';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { COLOR_OPTIONS, getColorClasses } from '@/lib/colors';
import { cn } from '@/lib/utils';
interface Props {
sectionId: string;
settings: SectionSettings;
open: boolean;
onOpenChange: (open: boolean) => void;
onSave: (sectionId: string, updated: Partial<SectionSettings>) => void;
}
function ColorSwatch({ color }: { color: string }) {
const classes = getColorClasses(color);
return (
<span className="flex items-center gap-2">
<span className={cn('w-3 h-3 rounded-full', classes.bg, classes.ring, 'ring-1')} />
{color}
</span>
);
}
export default function SectionConfigDialog({ sectionId, settings, open, onOpenChange, onSave }: Props) {
const [label, setLabel] = useState(settings.label);
const [color, setColor] = useState(settings.color);
const [cardColor, setCardColor] = useState(settings.cardColor);
const [visible, setVisible] = useState(settings.visible);
const [order, setOrder] = useState(String(settings.order));
const handleSave = () => {
onSave(sectionId, {
label,
color,
cardColor,
visible,
order: parseInt(order) || 0,
});
onOpenChange(false);
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Configure Section</DialogTitle>
</DialogHeader>
<div className="space-y-4">
<div className="space-y-2">
<Label>Label</Label>
<Input value={label} onChange={(e) => setLabel(e.target.value)} />
</div>
<div className="space-y-2">
<Label>Section Color</Label>
<Select value={color} onValueChange={setColor}>
<SelectTrigger className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
{COLOR_OPTIONS.map((c) => (
<SelectItem key={c} value={c}>
<ColorSwatch color={c} />
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label>Card Color</Label>
<Select value={cardColor} onValueChange={setCardColor}>
<SelectTrigger className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
{COLOR_OPTIONS.map((c) => (
<SelectItem key={c} value={c}>
<ColorSwatch color={c} />
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="flex items-center gap-3">
<Label htmlFor={`visible-${sectionId}`}>Visible</Label>
<input
id={`visible-${sectionId}`}
type="checkbox"
checked={visible}
onChange={(e) => setVisible(e.target.checked)}
className="h-4 w-4 rounded border-input accent-primary cursor-pointer"
/>
</div>
<div className="space-y-2">
<Label>Order</Label>
<Input
type="number"
min="0"
max="10"
value={order}
onChange={(e) => setOrder(e.target.value)}
className="w-20"
/>
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)}>
Cancel
</Button>
<Button onClick={handleSave}>Save</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}

View File

@@ -0,0 +1,195 @@
import { useState, useMemo } from 'react';
import {
Plus,
Search,
Pencil,
Trash2,
TrendingUp,
TrendingDown,
ArrowLeftRight,
} from 'lucide-react';
import api, { type Transaction } from '../api';
import TransactionModal from './TransactionModal';
import ConfirmDialog from './ConfirmDialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Card, CardContent } from '@/components/ui/card';
import { DataTable } from '@/components/ui/data-table';
import { getTransactionColumns } from '@/components/transactions/transaction-columns';
import { formatAmount } from '@/lib/format';
import { cn } from '@/lib/utils';
export interface TransactionListProps {
transactions: Transaction[];
loading: boolean;
source: 'CREDIT_CARD' | 'CASH' | 'TRANSFER';
search: string;
onSearchChange: (value: string) => void;
onRefresh: () => void;
emptyIcon?: React.ReactNode;
emptyMessage?: string;
showCategory?: boolean;
addLabel?: string;
}
export default function TransactionList({
transactions,
loading,
source,
search,
onSearchChange,
onRefresh,
emptyIcon,
emptyMessage = 'No transactions found',
showCategory = true,
addLabel = 'Add Transaction',
}: TransactionListProps) {
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 handleEdit = (tx: Transaction) => {
setEditing(tx);
setModalOpen(true);
};
const handleDelete = async () => {
if (deleteId === null) return;
setDeleting(true);
try {
await api.delete(`/transactions/${deleteId}`);
setDeleteId(null);
onRefresh();
} finally {
setDeleting(false);
}
};
const columns = useMemo(
() => getTransactionColumns({ showCategory, onEdit: handleEdit, onDelete: (id) => setDeleteId(id) }),
[showCategory],
);
const empty = transactions.length === 0 && !loading;
return (
<>
{/* Search + Add */}
<div className="flex items-center gap-3">
<div className="relative flex-1 max-w-sm">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
<Input
value={search}
onChange={(e) => onSearchChange(e.target.value)}
className="pl-10"
placeholder="Search merchants..."
/>
</div>
<Button onClick={() => { setEditing(null); setModalOpen(true); }}>
<Plus className="w-4 h-4" />
{addLabel}
</Button>
</div>
{/* Mobile list */}
<Card className="md:hidden">
<CardContent className="p-0 divide-y divide-border">
{empty ? (
<div className="px-5 py-16 text-center text-muted-foreground text-sm">
{emptyIcon || <ArrowLeftRight className="w-8 h-8 mx-auto mb-3 text-muted-foreground/50" />}
{emptyMessage}
</div>
) : (
transactions.map((tx) => (
<div key={tx.id} className="flex items-center gap-3 px-4 py-3">
<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 flex-1">
<p className="text-sm font-medium truncate">{tx.merchant}</p>
<p className="text-xs text-muted-foreground">
{new Date(tx.date).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}
{showCategory && tx.category && (
<span className="ml-1.5 text-muted-foreground/60">{tx.category.name}</span>
)}
</p>
</div>
<span
className={cn(
'font-mono text-sm font-medium shrink-0',
tx.transaction_type === 'DEVOLUCION' && 'text-primary'
)}
>
{tx.transaction_type === 'DEVOLUCION' ? '+' : '-'}
{formatAmount(tx.amount, tx.currency)}
</span>
<div className="flex items-center gap-0.5 shrink-0">
<Button variant="ghost" size="icon" title="Edit transaction" aria-label="Edit transaction" onClick={() => handleEdit(tx)}>
<Pencil className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
title="Delete transaction"
aria-label="Delete transaction"
onClick={() => setDeleteId(tx.id)}
className="hover:text-destructive"
>
<Trash2 className="w-4 h-4" />
</Button>
</div>
</div>
))
)}
</CardContent>
</Card>
{/* Desktop table */}
<Card className="hidden md:block">
<CardContent className="p-0">
<DataTable
columns={columns}
data={transactions}
pagination
pageSize={25}
initialSorting={[{ id: 'date', desc: true }]}
emptyMessage={emptyMessage}
/>
</CardContent>
</Card>
{/* Modals */}
{modalOpen && (
<TransactionModal
transaction={editing}
source={source}
onClose={() => setModalOpen(false)}
onSaved={onRefresh}
/>
)}
{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}
/>
)}
</>
);
}

View File

@@ -1,6 +1,24 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { X } from 'lucide-react';
import api, { type Category, type Transaction } from '../api'; import api, { type Category, type Transaction } from '../api';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogFooter,
} from '@/components/ui/dialog';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { AlertCircle } from 'lucide-react';
interface Props { interface Props {
transaction?: Transaction | null; transaction?: Transaction | null;
@@ -83,43 +101,35 @@ export default function TransactionModal({ transaction, source, onClose, onSaved
} }
}; };
const inputClass =
'w-full bg-input-bg border border-border rounded-lg px-3 py-2.5 text-sm text-text-primary placeholder-text-faint focus:outline-none focus:border-[#606C38]/50 focus:ring-1 focus:ring-[#606C38]/20 transition-colors';
const labelClass = 'block text-xs font-medium text-text-secondary mb-1 uppercase tracking-wider';
return ( return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4"> <Dialog open onOpenChange={(open) => { if (!open) onClose(); }}>
<div className="bg-surface border border-border rounded-xl w-full max-w-lg max-h-[90vh] overflow-y-auto"> <DialogContent className="sm:max-w-lg max-h-[90vh] overflow-y-auto">
<div className="flex items-center justify-between px-5 py-4 border-b border-border"> <DialogHeader>
<h3 className="font-semibold"> <DialogTitle>
{transaction ? 'Edit Transaction' : 'New Transaction'} {transaction ? 'Edit Transaction' : 'New Transaction'}
</h3> </DialogTitle>
<button onClick={onClose} className="text-text-muted hover:text-text-primary transition-colors"> </DialogHeader>
<X className="w-5 h-5" />
</button>
</div>
<form onSubmit={handleSubmit} className="p-5 space-y-4"> <form onSubmit={handleSubmit} className="space-y-4">
{error && ( {error && (
<div className="p-3 bg-red-500/10 border border-red-500/20 rounded-lg text-sm text-red-500 dark:text-red-400"> <Alert variant="destructive">
{error} <AlertCircle className="h-4 w-4" />
</div> <AlertDescription>{error}</AlertDescription>
</Alert>
)} )}
<div className="grid grid-cols-2 gap-4"> <div className="grid grid-cols-2 gap-4">
<div className="col-span-2"> <div className="col-span-2 space-y-2">
<label className={labelClass}>Merchant</label> <Label>Merchant</Label>
<input <Input
className={inputClass}
value={form.merchant} value={form.merchant}
onChange={(e) => setForm({ ...form, merchant: e.target.value })} onChange={(e) => setForm({ ...form, merchant: e.target.value })}
placeholder="e.g. AUTO MERCADO ON LINE" placeholder="e.g. AUTO MERCADO ON LINE"
required required
/> />
</div> </div>
<div> <div className="space-y-2">
<label className={labelClass}>Amount</label> <Label>Amount</Label>
<input <Input
className={inputClass}
type="number" type="number"
step="0.01" step="0.01"
value={form.amount} value={form.amount}
@@ -128,69 +138,74 @@ export default function TransactionModal({ transaction, source, onClose, onSaved
required required
/> />
</div> </div>
<div> <div className="space-y-2">
<label className={labelClass}>Currency</label> <Label>Currency</Label>
<select <Select value={form.currency} onValueChange={(v) => setForm({ ...form, currency: v })}>
className={inputClass} <SelectTrigger className="w-full">
value={form.currency} <SelectValue />
onChange={(e) => setForm({ ...form, currency: e.target.value })} </SelectTrigger>
> <SelectContent>
<option value="CRC">CRC ()</option> <SelectItem value="CRC">CRC ()</SelectItem>
<option value="USD">USD ($)</option> <SelectItem value="USD">USD ($)</SelectItem>
</select> </SelectContent>
</Select>
</div> </div>
<div> <div className="space-y-2">
<label className={labelClass}>Date</label> <Label>Date</Label>
<input <Input
className={inputClass}
type="datetime-local" type="datetime-local"
value={form.date} value={form.date}
onChange={(e) => setForm({ ...form, date: e.target.value })} onChange={(e) => setForm({ ...form, date: e.target.value })}
required required
/> />
</div> </div>
<div> <div className="space-y-2">
<label className={labelClass}>Type</label> <Label>Type</Label>
<select <Select value={form.transaction_type} onValueChange={(v) => setForm({ ...form, transaction_type: v })}>
className={inputClass} <SelectTrigger className="w-full">
value={form.transaction_type} <SelectValue />
onChange={(e) => setForm({ ...form, transaction_type: e.target.value })} </SelectTrigger>
> <SelectContent>
<option value="COMPRA">Compra</option> <SelectItem value="COMPRA">Compra</SelectItem>
<option value="DEVOLUCION">Devolución</option> <SelectItem value="DEVOLUCION">Devolución</SelectItem>
</select> </SelectContent>
</Select>
</div> </div>
<div> <div className="space-y-2">
<label className={labelClass}>Category</label> <Label>Category</Label>
<select <Select
className={inputClass} value={form.category_id ? String(form.category_id) : 'auto'}
value={form.category_id} onValueChange={(v) => setForm({ ...form, category_id: v === 'auto' ? '' : v })}
onChange={(e) => setForm({ ...form, category_id: e.target.value })}
> >
<option value="">Auto-detect</option> <SelectTrigger className="w-full">
{categories.map((c) => ( <SelectValue />
<option key={c.id} value={c.id}> </SelectTrigger>
{c.name} <SelectContent>
</option> <SelectItem value="auto">Auto-detect</SelectItem>
))} {categories.map((c) => (
</select> <SelectItem key={c.id} value={String(c.id)}>
{c.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div> </div>
<div> <div className="space-y-2">
<label className={labelClass}>Bank</label> <Label>Bank</Label>
<select <Select value={form.bank} onValueChange={(v) => setForm({ ...form, bank: v })}>
className={inputClass} <SelectTrigger className="w-full">
value={form.bank} <SelectValue />
onChange={(e) => setForm({ ...form, bank: e.target.value })} </SelectTrigger>
> <SelectContent>
<option value="BAC">BAC</option> <SelectItem value="BAC">BAC</SelectItem>
<option value="BCR">BCR</option> <SelectItem value="BCR">BCR</SelectItem>
<option value="DAVIVIENDA">Davivienda</option> <SelectItem value="DAVIVIENDA">Davivienda</SelectItem>
</select> </SelectContent>
</Select>
</div> </div>
<div> <div className="space-y-2">
<label className={labelClass}>City</label> <Label>City</Label>
<input <Input
className={inputClass}
value={form.city} value={form.city}
onChange={(e) => setForm({ ...form, city: e.target.value })} onChange={(e) => setForm({ ...form, city: e.target.value })}
placeholder="SAN JOSE, Costa Rica" placeholder="SAN JOSE, Costa Rica"
@@ -198,19 +213,17 @@ export default function TransactionModal({ transaction, source, onClose, onSaved
</div> </div>
{source === 'CREDIT_CARD' && ( {source === 'CREDIT_CARD' && (
<> <>
<div> <div className="space-y-2">
<label className={labelClass}>Card Type</label> <Label>Card Type</Label>
<input <Input
className={inputClass}
value={form.card_type} value={form.card_type}
onChange={(e) => setForm({ ...form, card_type: e.target.value })} onChange={(e) => setForm({ ...form, card_type: e.target.value })}
placeholder="MASTER" placeholder="MASTER"
/> />
</div> </div>
<div> <div className="space-y-2">
<label className={labelClass}>Card Last 4</label> <Label>Card Last 4</Label>
<input <Input
className={inputClass}
value={form.card_last4} value={form.card_last4}
onChange={(e) => setForm({ ...form, card_last4: e.target.value })} onChange={(e) => setForm({ ...form, card_last4: e.target.value })}
placeholder="6585" placeholder="6585"
@@ -219,10 +232,9 @@ export default function TransactionModal({ transaction, source, onClose, onSaved
</div> </div>
</> </>
)} )}
<div className="col-span-2"> <div className="col-span-2 space-y-2">
<label className={labelClass}>Notes</label> <Label>Notes</Label>
<input <Input
className={inputClass}
value={form.notes} value={form.notes}
onChange={(e) => setForm({ ...form, notes: e.target.value })} onChange={(e) => setForm({ ...form, notes: e.target.value })}
placeholder="Optional notes" placeholder="Optional notes"
@@ -230,24 +242,16 @@ export default function TransactionModal({ transaction, source, onClose, onSaved
</div> </div>
</div> </div>
<div className="flex gap-3 pt-2"> <DialogFooter>
<button <Button type="button" variant="outline" onClick={onClose}>
type="button"
onClick={onClose}
className="flex-1 px-4 py-2.5 rounded-lg text-sm font-medium text-text-secondary border border-border hover:bg-surface-hover transition-colors"
>
Cancel Cancel
</button> </Button>
<button <Button type="submit" disabled={saving}>
type="submit"
disabled={saving}
className="flex-1 px-4 py-2.5 rounded-lg text-sm font-medium bg-[#606C38] hover:bg-[#7a8a4a] text-white dark:text-[#FEFAE0] transition-colors disabled:opacity-50"
>
{saving ? 'Saving...' : transaction ? 'Update' : 'Create'} {saving ? 'Saving...' : transaction ? 'Update' : 'Create'}
</button> </Button>
</div> </DialogFooter>
</form> </form>
</div> </DialogContent>
</div> </Dialog>
); );
} }

View File

@@ -0,0 +1,137 @@
import { type ColumnDef } from '@tanstack/react-table';
import { Pencil, Trash2, TrendingDown, TrendingUp } from 'lucide-react';
import { type Transaction } from '@/api';
import { formatAmount } from '@/lib/format';
import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { DataTableColumnHeader } from '@/components/ui/data-table-column-header';
interface TransactionColumnOptions {
showCategory: boolean;
onEdit: (tx: Transaction) => void;
onDelete: (txId: number) => void;
}
export function getTransactionColumns({
showCategory,
onEdit,
onDelete,
}: TransactionColumnOptions): ColumnDef<Transaction, unknown>[] {
const columns: ColumnDef<Transaction, unknown>[] = [
{
accessorKey: 'date',
header: ({ column }) => <DataTableColumnHeader column={column} title="Date" />,
cell: ({ row }) => (
<span className="font-mono text-muted-foreground text-xs whitespace-nowrap">
{new Date(row.original.date).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
})}
</span>
),
},
{
accessorKey: 'merchant',
header: ({ column }) => <DataTableColumnHeader column={column} title="Merchant" />,
cell: ({ row }) => {
const tx = row.original;
return (
<div className="flex items-center gap-2">
<div
className={cn(
'w-6 h-6 rounded 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-3 h-3" />
) : (
<TrendingDown className="w-3 h-3" />
)}
</div>
<span className="truncate">{tx.merchant}</span>
</div>
);
},
},
];
if (showCategory) {
columns.push({
accessorFn: (row) => row.category?.name ?? '',
id: 'category',
header: ({ column }) => <DataTableColumnHeader column={column} title="Category" />,
cell: ({ row }) => {
const category = row.original.category;
return category ? (
<Badge variant="secondary">{category.name}</Badge>
) : (
<span className="text-xs text-muted-foreground">&mdash;</span>
);
},
});
}
columns.push(
{
accessorKey: 'amount',
meta: { className: 'text-right' },
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Amount" className="justify-end" />
),
cell: ({ row }) => {
const tx = row.original;
return (
<span
className={cn(
'font-mono font-medium',
tx.transaction_type === 'DEVOLUCION' && 'text-primary',
)}
>
{tx.transaction_type === 'DEVOLUCION' ? '+' : '-'}
{formatAmount(tx.amount, tx.currency)}
</span>
);
},
},
{
id: 'actions',
meta: { className: 'text-right' },
size: 80,
enableSorting: false,
cell: ({ row }) => {
const tx = row.original;
return (
<div className="flex items-center justify-end gap-1">
<Button
variant="ghost"
size="icon"
title="Edit transaction"
aria-label="Edit transaction"
onClick={() => onEdit(tx)}
>
<Pencil className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
title="Delete transaction"
aria-label="Delete transaction"
onClick={() => onDelete(tx.id)}
className="hover:text-destructive"
>
<Trash2 className="w-4 h-4" />
</Button>
</div>
);
},
},
);
return columns;
}

View File

@@ -0,0 +1,41 @@
import { type Column } from '@tanstack/react-table';
import { ArrowDown, ArrowUp, ArrowUpDown } from 'lucide-react';
import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button';
interface DataTableColumnHeaderProps<TData, TValue> {
column: Column<TData, TValue>;
title: string;
className?: string;
}
export function DataTableColumnHeader<TData, TValue>({
column,
title,
className,
}: DataTableColumnHeaderProps<TData, TValue>) {
if (!column.getCanSort()) {
return <div className={cn(className)}>{title}</div>;
}
const sorted = column.getIsSorted();
return (
<Button
variant="ghost"
size="sm"
className={cn('-ml-3 h-8', className)}
onClick={() => column.toggleSorting(sorted === 'asc')}
>
{title}
{sorted === 'desc' ? (
<ArrowDown className="ml-1 h-3.5 w-3.5" />
) : sorted === 'asc' ? (
<ArrowUp className="ml-1 h-3.5 w-3.5" />
) : (
<ArrowUpDown className="ml-1 h-3.5 w-3.5 text-muted-foreground/50" />
)}
</Button>
);
}

View File

@@ -0,0 +1,128 @@
import { useState } from 'react';
import {
type ColumnDef,
type SortingState,
flexRender,
getCoreRowModel,
getPaginationRowModel,
getSortedRowModel,
useReactTable,
} from '@tanstack/react-table';
import { ChevronLeft, ChevronRight } from 'lucide-react';
import { Button } from '@/components/ui/button';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[];
pagination?: boolean;
pageSize?: number;
emptyMessage?: React.ReactNode;
initialSorting?: SortingState;
}
export function DataTable<TData, TValue>({
columns,
data,
pagination = false,
pageSize = 25,
emptyMessage = 'No results.',
initialSorting = [],
}: DataTableProps<TData, TValue>) {
const [sorting, setSorting] = useState<SortingState>(initialSorting);
const table = useReactTable({
data,
columns,
state: { sorting },
onSortingChange: setSorting,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
...(pagination && {
getPaginationRowModel: getPaginationRowModel(),
initialState: { pagination: { pageSize } },
}),
});
return (
<div>
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<TableHead
key={header.id}
className={(header.column.columnDef.meta as Record<string, string>)?.className}
style={{ width: header.getSize() !== 150 ? header.getSize() : undefined }}
>
{header.isPlaceholder
? null
: flexRender(header.column.columnDef.header, header.getContext())}
</TableHead>
))}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows.length ? (
table.getRowModel().rows.map((row) => (
<TableRow key={row.id}>
{row.getVisibleCells().map((cell) => (
<TableCell
key={cell.id}
className={(cell.column.columnDef.meta as Record<string, string>)?.className}
>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center text-muted-foreground">
{emptyMessage}
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
{pagination && table.getPageCount() > 1 && (
<div className="flex items-center justify-between px-4 py-3 border-t">
<p className="text-sm text-muted-foreground">
Page {table.getState().pagination.pageIndex + 1} of {table.getPageCount()}
</p>
<div className="flex items-center gap-1">
<Button
variant="outline"
size="sm"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
<ChevronLeft className="h-4 w-4" />
Previous
</Button>
<Button
variant="outline"
size="sm"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
Next
<ChevronRight className="h-4 w-4" />
</Button>
</div>
</div>
)}
</div>
);
}

View File

@@ -7,8 +7,6 @@ import {
Bar, Bar,
XAxis, XAxis,
YAxis, YAxis,
Tooltip,
ResponsiveContainer,
LineChart, LineChart,
Line, Line,
} from 'recharts'; } from 'recharts';
@@ -16,7 +14,13 @@ import { BarChart3 } from 'lucide-react';
import api from '../api'; import api from '../api';
import BillingCycleSelector from '../components/BillingCycleSelector'; 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 { interface CategorySpending {
category_id: number | null; category_id: number | null;
@@ -42,18 +46,30 @@ interface DailySpending {
} }
const COLORS = [ const COLORS = [
'#606C38', '#BC6C25', '#8b5cf6', '#DDA15E', '#ef4444', 'var(--chart-1)', 'var(--chart-2)', 'var(--chart-3)', 'var(--chart-4)', 'var(--chart-5)',
'#ec4899', '#283618', '#f97316', '#6366f1', '#8a9462', 'oklch(0.7 0.15 30)', 'oklch(0.65 0.2 300)', 'oklch(0.6 0.15 150)',
'#e879f9', '#c2c9a7', '#fb923c', '#a78bfa', '#7a8a4a', 'oklch(0.75 0.12 60)', 'oklch(0.55 0.18 250)',
'#fbbf24',
]; ];
function formatCRC(value: number) { function formatCRC(value: number) {
return `${Math.round(value).toLocaleString('es-CR')}`; 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() { export default function Analytics() {
const { theme } = useTheme();
const [cycle, setCycle] = useState<{ year: number; month: number } | null>(null); const [cycle, setCycle] = useState<{ year: number; month: number } | null>(null);
const [byCategory, setByCategory] = useState<CategorySpending[]>([]); const [byCategory, setByCategory] = useState<CategorySpending[]>([]);
const [trend, setTrend] = useState<MonthlyTrend[]>([]); const [trend, setTrend] = useState<MonthlyTrend[]>([]);
@@ -79,194 +95,216 @@ export default function Analytics() {
.catch(console.error); .catch(console.error);
}, [cycle]); }, [cycle]);
const tooltipStyle = { // Build dynamic chart config for pie chart
background: theme === 'dark' ? '#1e293b' : '#FEFAE0', const pieChartConfig = byCategory.reduce<ChartConfig>((acc, cat, i) => {
border: `1px solid ${theme === 'dark' ? '#334155' : 'rgba(96,108,56,0.25)'}`, acc[cat.category_name] = {
borderRadius: '8px', label: cat.category_name,
fontSize: '12px', color: COLORS[i % COLORS.length],
color: theme === 'dark' ? '#e2e8f0' : '#283618', };
}; return acc;
}, {});
const tickColor = theme === 'dark' ? '#64748b' : '#8a9462';
return ( return (
<div className="space-y-6"> <div className="space-y-6">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4"> <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div> <div>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<BarChart3 className="w-5 h-5 text-[#606C38] dark:text-[#7a8a4a]" /> <BarChart3 className="w-5 h-5 text-primary" />
<h1 className="text-2xl font-bold">Analytics</h1> <h1 className="text-2xl font-bold font-heading">Analytics</h1>
</div> </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> </div>
<BillingCycleSelector value={cycle} onChange={setCycle} /> <BillingCycleSelector value={cycle} onChange={setCycle} />
</div> </div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6"> <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
{/* Spending by Category - Donut */} {/* Spending by Category - Donut */}
<div className="bg-surface-card border border-border rounded-xl p-5"> <Card>
<h2 className="text-sm font-semibold text-text-secondary uppercase tracking-wider mb-4"> <CardHeader>
Spending by Category <CardTitle className="text-sm uppercase tracking-wider text-muted-foreground">
</h2> Spending by Category
{byCategory.length === 0 ? ( </CardTitle>
<div className="h-64 flex items-center justify-center text-text-faint text-sm"> </CardHeader>
No data for this period <CardContent>
</div> {byCategory.length === 0 ? (
) : ( <div className="h-64 flex items-center justify-center text-muted-foreground text-sm">
<div className="flex flex-col items-center"> No data for this period
<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>
))}
</div> </div>
</div> ) : (
)} <div className="flex flex-col items-center">
</div> <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 */} {/* Monthly Trend - Bar */}
<div className="bg-surface-card border border-border rounded-xl p-5"> <Card>
<h2 className="text-sm font-semibold text-text-secondary uppercase tracking-wider mb-4"> <CardHeader>
Monthly Spending (CRC) <CardTitle className="text-sm uppercase tracking-wider text-muted-foreground">
</h2> Monthly Spending (CRC)
{trend.length === 0 ? ( </CardTitle>
<div className="h-64 flex items-center justify-center text-text-faint text-sm"> </CardHeader>
No data <CardContent>
</div> {trend.length === 0 ? (
) : ( <div className="h-64 flex items-center justify-center text-muted-foreground text-sm">
<ResponsiveContainer width="100%" height={300}> No data
<BarChart data={trend}> </div>
<XAxis ) : (
dataKey="label" <ChartContainer config={trendChartConfig} className="h-[300px] w-full">
tick={{ fill: tickColor, fontSize: 11 }} <BarChart data={trend}>
axisLine={false} <XAxis
tickLine={false} dataKey="label"
/> axisLine={false}
<YAxis tickLine={false}
tick={{ fill: tickColor, fontSize: 11 }} />
axisLine={false} <YAxis
tickLine={false} axisLine={false}
tickFormatter={(v) => `${(v / 1000).toFixed(0)}k`} tickLine={false}
/> tickFormatter={(v) => `${(v / 1000).toFixed(0)}k`}
<Tooltip />
contentStyle={tooltipStyle} <ChartTooltip
formatter={(value: any) => formatCRC(Number(value))} content={
/> <ChartTooltipContent
<Bar dataKey="total_crc" fill="#606C38" radius={[4, 4, 0, 0]} /> formatter={(value) => formatCRC(Number(value))}
</BarChart> />
</ResponsiveContainer> }
)} />
</div> <Bar dataKey="total_crc" fill="var(--color-total_crc)" radius={[4, 4, 0, 0]} />
</BarChart>
</ChartContainer>
)}
</CardContent>
</Card>
{/* Daily Spending - Line */} {/* Daily Spending - Line */}
<div className="bg-surface-card border border-border rounded-xl p-5 lg:col-span-2"> <Card className="lg:col-span-2">
<h2 className="text-sm font-semibold text-text-secondary uppercase tracking-wider mb-4"> <CardHeader>
Daily Spending <CardTitle className="text-sm uppercase tracking-wider text-muted-foreground">
</h2> Daily Spending
{daily.length === 0 ? ( </CardTitle>
<div className="h-48 flex items-center justify-center text-text-faint text-sm"> </CardHeader>
No data for this period <CardContent>
</div> {daily.length === 0 ? (
) : ( <div className="h-48 flex items-center justify-center text-muted-foreground text-sm">
<ResponsiveContainer width="100%" height={240}> No data for this period
<LineChart data={daily}> </div>
<XAxis ) : (
dataKey="date" <ChartContainer config={dailyChartConfig} className="h-[240px] w-full">
tick={{ fill: tickColor, fontSize: 10 }} <LineChart data={daily}>
axisLine={false} <XAxis
tickLine={false} dataKey="date"
tickFormatter={(v) => { axisLine={false}
const d = new Date(v); tickLine={false}
return `${d.getMonth() + 1}/${d.getDate()}`; tickFormatter={(v) => {
}} const d = new Date(v);
/> return `${d.getMonth() + 1}/${d.getDate()}`;
<YAxis }}
tick={{ fill: tickColor, fontSize: 11 }} />
axisLine={false} <YAxis
tickLine={false} axisLine={false}
tickFormatter={(v) => `${(v / 1000).toFixed(0)}k`} tickLine={false}
/> tickFormatter={(v) => `${(v / 1000).toFixed(0)}k`}
<Tooltip />
contentStyle={tooltipStyle} <ChartTooltip
formatter={(value: any) => formatCRC(Number(value))} content={
labelFormatter={(label) => new Date(label).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })} <ChartTooltipContent
/> formatter={(value) => formatCRC(Number(value))}
<Line labelFormatter={(label) =>
type="monotone" new Date(label).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })
dataKey="total" }
stroke="#BC6C25" />
strokeWidth={2} }
dot={{ fill: '#BC6C25', r: 3 }} />
activeDot={{ r: 5 }} <Line
/> type="monotone"
</LineChart> dataKey="total"
</ResponsiveContainer> stroke="var(--color-total)"
)} strokeWidth={2}
</div> dot={{ fill: 'var(--color-total)', r: 3 }}
activeDot={{ r: 5 }}
/>
</LineChart>
</ChartContainer>
)}
</CardContent>
</Card>
</div> </div>
{/* Top categories summary */} {/* Top categories summary */}
{byCategory.length > 0 && ( {byCategory.length > 0 && (
<div className="bg-surface-card border border-border rounded-xl p-5"> <Card>
<h2 className="text-sm font-semibold text-text-secondary uppercase tracking-wider mb-4"> <CardHeader>
Top Categories <CardTitle className="text-sm uppercase tracking-wider text-muted-foreground">
</h2> Top Categories
<div className="space-y-3"> </CardTitle>
{byCategory.slice(0, 8).map((cat, i) => ( </CardHeader>
<div key={cat.category_name} className="flex items-center gap-3"> <CardContent>
<div <div className="space-y-3">
className="w-3 h-3 rounded-full flex-shrink-0" {byCategory.slice(0, 8).map((cat, i) => (
style={{ background: COLORS[i % COLORS.length] }} <div key={cat.category_name} className="flex items-center gap-3">
/>
<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">
<div <div
className="h-1.5 rounded-full" className="w-3 h-3 rounded-full flex-shrink-0"
style={{ style={{ background: COLORS[i % COLORS.length] }}
width: `${cat.percentage}%`,
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>
</div> </Card>
)} )}
</div> </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 { Link } from 'react-router-dom';
import { import {
ArrowRight, ArrowRight,
@@ -12,31 +12,36 @@ import {
} from 'lucide-react'; } from 'lucide-react';
import api, { type Account, type Transaction } from '../api'; 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) { // --- Section definitions ---
const abs = Math.abs(amount);
if (currency === 'BTC') return abs.toFixed(8); interface SectionDef {
if (currency === 'XMR') return abs.toFixed(4); filterFn: (a: Account) => boolean;
if (currency === 'USD') { totalCurrency: string; // empty string = no total
return `$${abs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
return `${abs.toLocaleString('es-CR', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
} }
function formatDate(dateStr: string) { const SECTION_DEFS: Record<string, SectionDef> = {
return new Date(dateStr).toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); 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 --- const BANK_ORDER = ['BAC', 'BCR', 'DAVIVIENDA'];
function AccountCard({
account, // --- AccountRow ---
editingId,
editValue, interface AccountRowProps {
setEditValue,
startEditing,
saveBalance,
cancelEditing,
}: {
account: Account; account: Account;
editingId: number | null; editingId: number | null;
editValue: string; editValue: string;
@@ -44,20 +49,29 @@ function AccountCard({
startEditing: (a: Account) => void; startEditing: (a: Account) => void;
saveBalance: (id: number) => void; saveBalance: (id: number) => void;
cancelEditing: () => 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; const isEditing = editingId === account.id;
return ( 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-between px-3 py-2.5 hover:bg-muted/30 transition-colors group">
<div className="flex items-center justify-end"> <span className="text-sm font-medium text-muted-foreground">{label}</span>
<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>
{isEditing ? ( {isEditing ? (
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<input <Input
type="number" type="number"
step="0.01" step="0.01"
value={editValue} value={editValue}
@@ -67,36 +81,40 @@ function AccountCard({
if (e.key === 'Escape') cancelEditing(); if (e.key === 'Escape') cancelEditing();
}} }}
autoFocus 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]"> <Button variant="ghost" size="icon-xs" onClick={() => saveBalance(account.id)} title="Save" aria-label="Save balance">
<Check className="w-4 h-4" /> <Check className="w-3.5 h-3.5" />
</button> </Button>
<button onClick={cancelEditing} className="p-1 text-text-muted hover:text-text-secondary"> <Button variant="ghost" size="icon-xs" onClick={cancelEditing} title="Cancel" aria-label="Cancel editing">
<X className="w-4 h-4" /> <X className="w-3.5 h-3.5" />
</button> </Button>
</div> </div>
) : ( ) : (
<div className="flex items-center gap-2 group/balance cursor-pointer" onClick={() => startEditing(account)}> <div className="flex items-center gap-1.5">
<p className="text-2xl font-bold font-mono tracking-tight"> <span className={cn('text-lg font-bold font-mono tracking-tight', isLiability && 'text-destructive')}>
{formatAmount(account.balance, account.currency)} {formatAmount(account.balance, account.currency)}
</p> </span>
<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" /> <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>
)} )}
</div> </div>
); );
} }
// --- Total card --- // --- Dashboard ---
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>
);
}
export default function Dashboard() { export default function Dashboard() {
const [accounts, setAccounts] = useState<Account[]>([]); const [accounts, setAccounts] = useState<Account[]>([]);
@@ -105,6 +123,9 @@ export default function Dashboard() {
const [editingId, setEditingId] = useState<number | null>(null); const [editingId, setEditingId] = useState<number | null>(null);
const [editValue, setEditValue] = useState(''); const [editValue, setEditValue] = useState('');
const [exchangeRate, setExchangeRate] = useState<{ buy_rate: number; sell_rate: number } | null>(null); 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 () => { const fetchData = async () => {
setLoading(true); setLoading(true);
@@ -141,222 +162,170 @@ export default function Dashboard() {
} catch (e) { console.error(e); } } 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 // Sort sections by order, filter by visible
const bankAccounts = accounts.filter((a) => a.account_type === 'BANK'); const sortedSections = useMemo(() => {
const pensionAccounts = accounts.filter((a) => a.account_type === 'PENSION'); const sections = settings.dashboard.sections;
const savingsAccounts = accounts.filter((a) => a.account_type === 'SAVINGS'); return Object.entries(sections)
const liabilityAccounts = accounts.filter((a) => a.account_type === 'LIABILITY'); .filter(([, s]) => s.visible)
const cryptoAccounts = accounts.filter((a) => a.account_type === 'CRYPTO'); .sort(([, a], [, b]) => a.order - b.order);
}, [settings]);
const bankOrder = ['BAC', 'BCR', 'DAVIVIENDA']; // Net worth calculation
const netWorthBreakdown = useMemo(() => {
// Bank totals for exchange rate combined total if (accounts.length === 0) return null;
const bankCRC = bankAccounts.filter((a) => a.currency === 'CRC').reduce((s, a) => s + a.balance, 0); let assets = 0;
const bankUSD = bankAccounts.filter((a) => a.currency === 'USD').reduce((s, a) => s + a.balance, 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 ( return (
<div className="space-y-6"> <div className="space-y-6">
{/* Header */} {/* Header */}
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<div> <div>
<h1 className="text-2xl font-bold">Dashboard</h1> <h1 className="text-2xl font-bold font-heading">Dashboard</h1>
<p className="text-sm text-text-muted mt-1">Financial overview</p> <p className="text-sm text-muted-foreground mt-1">Financial overview</p>
</div> </div>
<button <Button variant="ghost" size="icon" onClick={fetchData} title="Refresh" aria-label="Refresh">
onClick={fetchData} <RefreshCw className={cn('w-4 h-4', loading && 'animate-spin')} />
className="p-2 rounded-lg text-text-muted hover:text-text-primary hover:bg-surface-hover transition-colors" </Button>
>
<RefreshCw className={`w-4 h-4 ${loading ? 'animate-spin' : ''}`} />
</button>
</div> </div>
{/* Bank accounts — grouped by currency */} {/* Net Worth */}
{(['CRC', 'USD'] as const).map((currency) => { {netWorthBreakdown != null && (
const accts = bankAccounts <Card>
.filter((a) => a.currency === currency) <CardContent className="px-4 py-3">
.sort((a, b) => bankOrder.indexOf(a.bank) - bankOrder.indexOf(b.bank)); <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; 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); const total = accts.reduce((s, a) => s + a.balance, 0);
return ( return (
<div key={currency} className="space-y-2"> <DashboardSection
<h2 className="text-xs font-semibold text-text-muted uppercase tracking-wider">{currency} Accounts</h2> key={sectionId}
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4"> sectionId={sectionId}
{accts.map((a) => <AccountCard key={a.id} account={a} {...cardProps} />)} settings={sectionSettings}
<TotalCard total={total} currency={currency} /> total={def.totalCurrency ? total : undefined}
</div> totalCurrency={def.totalCurrency || undefined}
</div> onToggleExpanded={(expanded) => patchSection(sectionId, { expanded })}
onOpenConfig={() => setConfigSection(sectionId)}
>
{accts.map((a) => (
<AccountRow key={a.id} account={a} {...rowProps} />
))}
</DashboardSection>
); );
})} })}
{/* Pension accounts */} {/* Exchange rate */}
{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 */}
{exchangeRate && ( {exchangeRate && (
<div className="flex flex-col sm:flex-row gap-4"> <Card>
<div className="flex-1 bg-surface-card border border-border rounded-xl p-4"> <CardContent className="p-4">
<span className="text-xs font-medium text-text-muted uppercase tracking-wider">USD/CRC Exchange Rate</span> <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"> <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">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>
</div> </CardContent>
{accounts.length > 0 && ( </Card>
<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>
)} )}
{/* Recent transactions */} {/* Recent transactions */}
<div className="bg-surface-card border border-border rounded-xl"> <Card>
<div className="flex items-center justify-between px-5 py-4 border-b border-border-subtle"> <CardHeader className="border-b flex-row items-center justify-between">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<CreditCard className="w-4 h-4 text-text-muted" /> <CreditCard className="w-4 h-4 text-muted-foreground" />
<h2 className="font-semibold text-sm">Recent Charges</h2> <CardTitle className="text-sm">Recent Charges</CardTitle>
</div> </div>
<Link <Link
to="/transactions" 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 View all
<ArrowRight className="w-3 h-3" /> <ArrowRight className="w-3 h-3" />
</Link> </Link>
</div> </CardHeader>
{recent.length === 0 && !loading ? ( <CardContent className="p-0">
<div className="px-5 py-12 text-center text-text-faint text-sm">No transactions yet. Add your first one!</div> {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-subtle"> ) : (
{recent.map((tx) => ( <div className="divide-y divide-border">
<div key={tx.id} className="flex items-center justify-between px-5 py-3.5 hover:bg-surface-hover transition-colors animate-fade-in"> {recent.map((tx) => (
<div className="flex items-center gap-3 min-w-0"> <div key={tx.id} className="flex items-center justify-between px-5 py-3.5 hover:bg-muted/50 transition-colors">
<div className={`w-8 h-8 rounded-lg flex items-center justify-center flex-shrink-0 ${ <div className="flex items-center gap-3 min-w-0">
tx.transaction_type === 'DEVOLUCION' ? 'bg-[#606C38]/10 text-[#606C38] dark:text-[#7a8a4a]' : 'bg-red-500/10 text-red-500 dark:text-red-400' <div className={cn(
}`}> 'w-8 h-8 rounded-lg flex items-center justify-center shrink-0',
{tx.transaction_type === 'DEVOLUCION' ? <TrendingUp className="w-4 h-4" /> : <TrendingDown className="w-4 h-4" />} tx.transaction_type === 'DEVOLUCION' ? 'bg-primary/10 text-primary' : 'bg-destructive/10 text-destructive'
</div> )}>
<div className="min-w-0"> {tx.transaction_type === 'DEVOLUCION' ? <TrendingUp className="w-4 h-4" /> : <TrendingDown className="w-4 h-4" />}
<p className="text-sm font-medium truncate">{tx.merchant}</p> </div>
<p className="text-xs text-text-muted"> <div className="min-w-0">
{formatDate(tx.date)} <p className="text-sm font-medium truncate">{tx.merchant}</p>
{tx.category && <span className="ml-2 text-text-faint">{tx.category.name}</span>} <p className="text-xs text-muted-foreground">
</p> {formatDate(tx.date)}
{tx.category && <span className="ml-2 text-muted-foreground/60">{tx.category.name}</span>}
</p>
</div>
</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> </div>
<span className={`font-mono text-sm font-medium flex-shrink-0 ml-4 ${ ))}
tx.transaction_type === 'DEVOLUCION' ? 'text-[#606C38] dark:text-[#7a8a4a]' : '' </div>
}`}> )}
{tx.transaction_type === 'DEVOLUCION' ? '+' : '-'}{formatAmount(tx.amount, tx.currency)} </CardContent>
</span> </Card>
</div>
))} {/* Section config dialog */}
</div> {configSection && settings.dashboard.sections[configSection] && (
)} <SectionConfigDialog
</div> sectionId={configSection}
settings={settings.dashboard.sections[configSection]}
open={!!configSection}
onOpenChange={(open) => { if (!open) setConfigSection(null); }}
onSave={(id, partial) => patchSection(id, partial)}
/>
)}
</div> </div>
); );
} }

View File

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

View File

@@ -1,20 +1,18 @@
import { useEffect, useState, useCallback } from 'react'; import { useEffect, useState, useCallback } from 'react';
import { import { Plus, ClipboardPaste } from 'lucide-react';
Plus,
Search,
Pencil,
Trash2,
TrendingUp,
TrendingDown,
ChevronDown,
ClipboardPaste,
} from 'lucide-react';
import api, { type Transaction, type Category } from '../api'; import api, { type Transaction, type Category } from '../api';
import TransactionModal from '../components/TransactionModal';
import PasteImportModal from '../components/PasteImportModal'; import PasteImportModal from '../components/PasteImportModal';
import ConfirmDialog from '../components/ConfirmDialog';
import BillingCycleSelector from '../components/BillingCycleSelector'; 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) { function formatAmount(amount: number, currency: string) {
const abs = Math.abs(amount); const abs = Math.abs(amount);
@@ -30,11 +28,7 @@ export default function Transactions() {
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
const [categoryFilter, setCategoryFilter] = useState(''); const [categoryFilter, setCategoryFilter] = useState('');
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [modalOpen, setModalOpen] = useState(false);
const [importOpen, setImportOpen] = 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 [cycle, setCycle] = useState<{ year: number; month: number } | null>(null);
const fetchTransactions = useCallback(async () => { const fetchTransactions = useCallback(async () => {
@@ -63,18 +57,6 @@ export default function Transactions() {
return () => clearTimeout(timer); return () => clearTimeout(timer);
}, [fetchTransactions]); }, [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 const totalCRC = transactions
.filter((tx) => tx.currency === 'CRC') .filter((tx) => tx.currency === 'CRC')
.reduce((sum, tx) => sum + (tx.transaction_type === 'DEVOLUCION' ? -tx.amount : tx.amount), 0); .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="space-y-5">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4"> <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div> <div>
<h1 className="text-2xl font-bold">Credit Card Transactions</h1> <h1 className="text-2xl font-bold font-heading">Credit Card Transactions</h1>
<p className="text-sm text-text-muted mt-1"> <p className="text-sm text-muted-foreground mt-1">
{transactions.length} transactions {transactions.length} transactions
{totalCRC !== 0 && ( {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 && ( {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> </p>
</div> </div>
<div className="flex gap-2"> <div className="flex gap-2">
<button <Button variant="outline" onClick={() => setImportOpen(true)}>
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"
>
<ClipboardPaste className="w-4 h-4" /> <ClipboardPaste className="w-4 h-4" />
Import Import
</button> </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>
</div> </div>
</div> </div>
{/* Billing cycle */} {/* Billing cycle */}
<BillingCycleSelector value={cycle} onChange={setCycle} /> <BillingCycleSelector value={cycle} onChange={setCycle} />
{/* Filters */} {/* Category filter */}
<div className="flex flex-col sm:flex-row gap-3"> <div className="flex items-center gap-3">
<div className="relative flex-1"> <Select
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted" /> value={categoryFilter || 'all'}
<input onValueChange={(v) => setCategoryFilter(v === 'all' ? '' : v)}
value={search} >
onChange={(e) => setSearch(e.target.value)} <SelectTrigger className="w-[180px]">
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" <SelectValue />
placeholder="Search merchants..." </SelectTrigger>
/> <SelectContent>
</div> <SelectItem value="all">All Categories</SelectItem>
<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>
{categories.map((c) => ( {categories.map((c) => (
<option key={c.id} value={c.id}> <SelectItem key={c.id} value={String(c.id)}>
{c.name} {c.name}
</option> </SelectItem>
))} ))}
</select> </SelectContent>
<ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted pointer-events-none" /> </Select>
</div>
</div> </div>
{/* Table */} <TransactionList
<div className="bg-surface-card border border-border rounded-xl overflow-hidden"> transactions={transactions}
<div className="overflow-x-auto"> loading={loading}
<table className="w-full text-sm"> source="CREDIT_CARD"
<thead> search={search}
<tr className="border-b border-border-subtle"> onSearchChange={setSearch}
<th className="text-left px-5 py-3 text-xs font-medium text-text-muted uppercase tracking-wider"> onRefresh={fetchTransactions}
Date showCategory
</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}
/>
)}
{importOpen && ( {importOpen && (
<PasteImportModal <PasteImportModal
@@ -274,16 +126,6 @@ export default function Transactions() {
onImported={fetchTransactions} 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> </div>
); );
} }

View File

@@ -1,17 +1,9 @@
import { useEffect, useState, useCallback } from 'react'; 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 api, { type Transaction } from '../api';
import TransactionModal from '../components/TransactionModal'; import TransactionList from '../components/TransactionList';
import ConfirmDialog from '../components/ConfirmDialog'; import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs';
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 })}`;
}
type SourceTab = 'CASH' | 'TRANSFER'; type SourceTab = 'CASH' | 'TRANSFER';
@@ -20,10 +12,6 @@ export default function Transfers() {
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
const [sourceTab, setSourceTab] = useState<SourceTab>('CASH'); const [sourceTab, setSourceTab] = useState<SourceTab>('CASH');
const [loading, setLoading] = useState(true); 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 () => { const fetchTransactions = useCallback(async () => {
setLoading(true); setLoading(true);
@@ -42,142 +30,36 @@ export default function Transfers() {
return () => clearTimeout(timer); return () => clearTimeout(timer);
}, [fetchTransactions]); }, [fetchTransactions]);
const handleDelete = async () => {
if (deleteId === null) return;
setDeleting(true);
try {
await api.delete(`/transactions/${deleteId}`);
setDeleteId(null);
fetchTransactions();
} finally {
setDeleting(false);
}
};
return ( return (
<div className="space-y-5"> <div className="space-y-5">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4"> <div>
<div> <h1 className="text-2xl font-bold font-heading">Cash & Transfers</h1>
<h1 className="text-2xl font-bold">Cash & Transfers</h1> <p className="text-sm text-muted-foreground mt-1">
<p className="text-sm text-text-muted mt-1"> Track non-credit-card expenses
Track non-credit-card expenses </p>
</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> </div>
{/* Source tabs */} <Tabs value={sourceTab} onValueChange={(v) => setSourceTab(v as SourceTab)}>
<div className="flex gap-1 bg-surface-card border border-border rounded-lg p-1 w-fit"> <TabsList>
{(['CASH', 'TRANSFER'] as const).map((tab) => ( <TabsTrigger value="CASH">Cash</TabsTrigger>
<button <TabsTrigger value="TRANSFER">Transfers</TabsTrigger>
key={tab} </TabsList>
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>
{/* Search */} <TabsContent value={sourceTab} className="mt-5 space-y-5">
<div className="relative max-w-sm"> <TransactionList
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted" /> transactions={transactions}
<input loading={loading}
value={search} source={sourceTab}
onChange={(e) => setSearch(e.target.value)} search={search}
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" onSearchChange={setSearch}
placeholder="Search..." onRefresh={fetchTransactions}
/> showCategory={false}
</div> addLabel={sourceTab === 'CASH' ? 'Add Cash Expense' : 'Add Transfer'}
emptyIcon={<ArrowLeftRight className="w-8 h-8 mx-auto mb-3 text-muted-foreground/50" />}
{/* List */} emptyMessage={`No ${sourceTab.toLowerCase()} transactions yet`}
<div className="bg-surface-card border border-border rounded-xl divide-y divide-border-subtle"> />
{transactions.length === 0 && !loading ? ( </TabsContent>
<div className="px-5 py-16 text-center text-text-faint text-sm"> </Tabs>
<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}
/>
)}
</div> </div>
); );
} }