import { index as transactionsIndex } from '@/actions/App/Http/Controllers/TransactionController'; import { PercentageTrendIndicator } from '@/components/dashboard/percentage-trend-indicator'; import { AmountDisplay } from '@/components/ui/amount-display'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { Progress } from '@/components/ui/progress'; import { BreakdownData } from '@/hooks/use-cashflow-data'; import { useChartColors } from '@/hooks/use-chart-color-scheme'; import { cn } from '@/lib/utils'; import { type CategoryColor, type CategoryIcon, getCategoryColorClasses, } from '@/types/category'; import { __ } from '@/utils/i18n'; import { Link } from '@inertiajs/react'; import { format } from 'date-fns'; import * as Icons from 'lucide-react'; import { LucideIcon } from 'lucide-react'; interface BreakdownCardProps { type: 'income' | 'expense'; data: BreakdownData; loading?: boolean; currency?: string; period?: { from: Date; to: Date }; } const fallbackCategory = { name: __('Uncategorized'), icon: 'HelpCircle' as CategoryIcon, color: 'gray' as CategoryColor, }; export function BreakdownCard({ type, data, loading, currency = 'USD', period, }: BreakdownCardProps) { const { categoryBarColor } = useChartColors(); const title = type === 'income' ? __('Income Sources') : __('Expense Categories'); const description = type === 'income' ? __('Where your money comes from') : __('Where your money goes'); const emptyMessage = type === 'income' ? __('No income this period') : __('No expenses this period'); if (loading) { return ( {title}
{Array.from({ length: 4 }).map((_, i) => (
))}
); } return (
{title}
{description}
{data.data.map((item, index) => { const category = item.category ?? fallbackCategory; const Icon = (Icons[ category.icon as keyof typeof Icons ] || Icons.HelpCircle) as LucideIcon; const percentageChange = item.previous_amount > 0 ? ((item.amount - item.previous_amount) / item.previous_amount) * 100 : null; const categoryColor = getCategoryColorClasses( category.color, ); const chartColor = categoryBarColor( category.color, index, ); const categoryUrl = period ? transactionsIndex({ query: { category_ids: item.category_id, date_from: format( period.from, 'yyyy-MM-dd', ), date_to: format(period.to, 'yyyy-MM-dd'), }, }).url : null; const rowContent = ( <>
{category.name}
{percentageChange !== null && ( )}
{item.percentage.toFixed(0)}%
); return categoryUrl ? ( {rowContent} ) : (
{rowContent}
); })} {data.data.length === 0 && (
{emptyMessage}
)}
); }