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

@@ -1,6 +1,13 @@
import { useEffect, useState } from 'react';
import { Calendar, ChevronDown } from 'lucide-react';
import { Calendar } from 'lucide-react';
import api from '../api';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
export interface CycleOption {
year: number;
@@ -22,33 +29,34 @@ export default function BillingCycleSelector({ value, onChange }: Props) {
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 (
<div className="flex items-center gap-2">
<Calendar className="w-4 h-4 text-text-muted" />
<div className="relative">
<select
value={selectedKey}
onChange={(e) => {
if (!e.target.value) {
onChange(null);
} else {
const [y, m] = e.target.value.split('-').map(Number);
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"
>
<option value="">All time</option>
<Calendar className="w-4 h-4 text-muted-foreground" />
<Select
value={selectedKey}
onValueChange={(val) => {
if (val === 'all') {
onChange(null);
} else {
const [y, m] = val.split('-').map(Number);
onChange({ year: y, month: m });
}
}}
>
<SelectTrigger className="w-[180px]">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="all">All time</SelectItem>
{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})
</option>
</SelectItem>
))}
</select>
<ChevronDown className="absolute right-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted pointer-events-none" />
</div>
</SelectContent>
</Select>
</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 {
title: string;
@@ -11,45 +22,26 @@ interface Props {
export default function ConfirmDialog({ title, message, confirmLabel = 'Delete', onConfirm, onCancel, loading }: Props) {
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4" onClick={onCancel}>
<div
className="bg-surface border border-border rounded-xl w-full max-w-sm animate-fade-in"
onClick={(e) => e.stopPropagation()}
>
<div className="flex items-center justify-between px-5 py-4 border-b border-border">
<h3 className="font-semibold">{title}</h3>
<button onClick={onCancel} className="text-text-muted hover:text-text-primary transition-colors">
<X className="w-5 h-5" />
</button>
</div>
<div className="px-5 py-5">
<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"
<AlertDialog open onOpenChange={(open) => { if (!open) onCancel(); }}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogMedia className="bg-destructive/10">
<AlertTriangle className="text-destructive" />
</AlertDialogMedia>
<AlertDialogTitle>{title}</AlertDialogTitle>
<AlertDialogDescription>{message}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={onCancel}>Cancel</AlertDialogCancel>
<AlertDialogAction
variant="destructive"
onClick={onConfirm}
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}
</button>
</div>
</div>
</div>
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</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,
Wallet,
Menu,
X,
Sun,
Moon,
} from 'lucide-react';
import { useState } from 'react';
import { useAuth } from '../AuthContext';
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 = [
{ to: '/', icon: LayoutDashboard, label: 'Dashboard' },
@@ -34,16 +43,16 @@ export default function Layout() {
};
return (
<div className="min-h-screen bg-surface text-text-primary">
<div className="min-h-screen bg-background text-foreground">
{/* 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="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">
<Wallet className="w-4 h-4 text-[#FEFAE0]" strokeWidth={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="text-lg font-bold tracking-tight hidden sm:inline">
Wealthy<span className="text-[#606C38] dark:text-[#7a8a4a]">Smart</span>
<span className="text-lg font-bold tracking-tight hidden sm:inline font-heading">
Wealthy<span className="text-primary">Smart</span>
</span>
</div>
@@ -55,11 +64,12 @@ export default function Layout() {
to={to}
end={to === '/'}
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
? 'bg-[#606C38]/10 text-[#606C38] dark:text-[#7a8a4a]'
: '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" />
@@ -68,59 +78,80 @@ export default function Layout() {
))}
</nav>
<div className="flex items-center gap-2">
<button
onClick={toggleTheme}
className="p-2 rounded-lg text-text-muted hover:text-text-primary hover:bg-surface-hover transition-colors"
>
<div className="flex items-center gap-1">
<Button variant="ghost" size="icon" onClick={toggleTheme} title="Toggle theme" aria-label="Toggle theme">
{theme === 'dark' ? <Sun className="w-4 h-4" /> : <Moon className="w-4 h-4" />}
</button>
<button
</Button>
<Button
variant="ghost"
size="icon"
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" />
</button>
<button
onClick={() => setMobileOpen(!mobileOpen)}
className="md:hidden text-text-muted"
</Button>
<Button
variant="ghost"
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" />}
</button>
<Menu className="w-5 h-5" />
</Button>
</div>
</div>
</header>
{/* Mobile nav */}
{mobileOpen && (
<div className="md:hidden border-t border-border px-4 pb-4 space-y-1">
{/* Mobile nav sheet */}
<Sheet open={mobileOpen} onOpenChange={setMobileOpen}>
<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 }) => (
<NavLink
key={to}
to={to}
end={to === '/'}
onClick={() => setMobileOpen(false)}
className={({ isActive }) =>
`flex items-center gap-3 px-4 py-3 rounded-lg text-sm font-medium transition-colors ${
isActive
? 'bg-[#606C38]/10 text-[#606C38] dark:text-[#7a8a4a]'
: 'text-text-muted hover:text-text-primary hover:bg-surface-hover'
}`
}
>
<Icon className="w-4 h-4" />
{label}
</NavLink>
<SheetClose key={to} render={<span />}>
<NavLink
to={to}
end={to === '/'}
onClick={() => setMobileOpen(false)}
className={({ isActive }) =>
cn(
'flex items-center gap-3 px-4 py-3 rounded-lg text-sm font-medium transition-colors',
isActive
? 'bg-primary/10 text-primary'
: 'text-muted-foreground hover:text-foreground hover:bg-muted'
)
}
>
<Icon className="w-4 h-4" />
{label}
</NavLink>
</SheetClose>
))}
<Separator className="my-2" />
<button
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" />
Sign out
</button>
</div>
)}
</header>
</nav>
</SheetContent>
</Sheet>
<main className="max-w-7xl mx-auto px-4 sm:px-6 py-6">
<Outlet />

View File

@@ -1,6 +1,24 @@
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 { 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 {
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 (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
<div className="bg-surface border border-border rounded-xl w-full max-w-2xl max-h-[90vh] overflow-y-auto">
<div className="flex items-center justify-between px-5 py-4 border-b border-border">
<div className="flex items-center gap-2">
<ClipboardPaste className="w-4 h-4 text-[#606C38] dark:text-[#7a8a4a]" />
<h3 className="font-semibold">Import Bank Statement</h3>
<Dialog open onOpenChange={(open) => { if (!open) onClose(); }}>
<DialogContent className="sm:max-w-2xl max-h-[90vh] overflow-y-auto">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<ClipboardPaste className="w-4 h-4 text-primary" />
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>
<button onClick={onClose} className="text-text-muted hover:text-text-primary transition-colors">
<X className="w-5 h-5" />
</button>
</div>
) : (
<div className="space-y-4">
<Alert>
<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 ? (
<>
<div className="grid grid-cols-2 gap-4">
<div>
<label className={labelClass}>Bank</label>
<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.length > 0 && (
<Alert variant="destructive">
<AlertTriangle className="h-4 w-4" />
<AlertTitle>{result.errors.length} errors</AlertTitle>
<AlertDescription>
<ul className="text-xs font-mono max-h-32 overflow-y-auto space-y-1 mt-1">
{result.errors.map((err, i) => (
<li key={i}>{err}</li>
))}
</ul>
</div>
)}
</AlertDescription>
</Alert>
)}
<button
onClick={onClose}
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"
>
Done
</button>
</div>
)}
</div>
</div>
</div>
<Button onClick={onClose} className="w-full">
Done
</Button>
</div>
)}
</DialogContent>
</Dialog>
);
}

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