import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { useFinanceData } from "@/context/FinanceDataContext"; import { cn } from "@/lib/utils"; import { useMoney } from "@/hooks/useMoney"; import { refinedMerchantLabel } from "@/lib/merchant"; import { normalizeCategory, UNCATEGORIZED } from "@/lib/categories"; import { Link, useNavigate } from "react-router-dom"; import { MerchantAvatar } from "@/components/elements/CategoryIcon"; import { CollapseToggle } from "@/components/dashboard/CollapseToggle"; import { useCollapsed } from "@/hooks/useCollapsed"; import { useMerchantRefinements } from "@/hooks/useMerchantRefinements"; export function RecentTransactions({ rail = false, limit }: { rail?: boolean; limit?: number }) { const { transactions } = useFinanceData(); const { moneyAbs, date: fmtDate } = useMoney(); const navigate = useNavigate(); const [collapsed, toggle] = useCollapsed("recent"); const { data: refinements } = useMerchantRefinements(); // A transaction with no date (a malformed import/sync row) has no // meaningful recency and would crash the sort below (comparing null). const recent = transactions .filter((t) => t.date) .sort((a, b) => b.date.localeCompare(a.date)) .slice(0, limit ?? (rail ? 14 : 8)); if (recent.length === 0) { return ( No recent transactions. ); } return (
Recent transactions
View all
{recent.map((txn) => (
navigate(`/transactions/${txn.id}`)} > {rail ? ( <>

{refinedMerchantLabel(txn.description, refinements, txn.date, txn.category)}

= 0 ? "text-chart-green" : "text-foreground")}> {txn.amount >= 0 ? "+" : "-"}{moneyAbs(txn.amount)}

{fmtDate(txn.date, "short")} {normalizeCategory(txn.category) !== UNCATEGORIZED && · {normalizeCategory(txn.category)}}

) : ( <>

{refinedMerchantLabel(txn.description, refinements, txn.date, txn.category)}

{fmtDate(txn.date)} {normalizeCategory(txn.category) !== UNCATEGORIZED && ( · {normalizeCategory(txn.category)} )}

= 0 ? "text-chart-green" : "text-foreground")}> {txn.amount >= 0 ? "+" : "-"}{moneyAbs(txn.amount)} )}
))}
); }