import { useState } from "react"; import { ChevronRight } from "lucide-react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/Card"; import { CategoryBreakdownModal } from "@/components/categories/CategoryBreakdownModal"; import { getCategoryIcon } from "@/lib/icons"; import { formatCurrency } from "@/lib/format"; import { useTranslation } from "@/lib/i18n"; import { translateCategoryName } from "@/lib/categoryLabels"; import type { CategoryRankingItem } from "@/types"; interface CategoryRankingCardProps { items: CategoryRankingItem[]; isLoading: boolean; selectedCategoryId: number | null; onSelectCategory: (categoryId: number) => void; } /** Ranks every expense category by total spent over the currently selected * period — unlike the Dashboard breakdown (locked to one month) or the * detail chart below (locked to one category), this is "which category * costs the most" across the whole range at once. Clicking a row drills * into that category in the detail chart/transaction list below. */ export function CategoryRankingCard({ items, isLoading, selectedCategoryId, onSelectCategory }: CategoryRankingCardProps) { const { t } = useTranslation(); // The subcategory breakdown lives in a modal, not expanded inline — a // category with many subcategories would otherwise push the whole ranking // list taller and shift every row below it. const [breakdownItem, setBreakdownItem] = useState(null); return ( {t("reports.categoryRankingTitle")} {isLoading ? (

{t("common.loading")}

) : items.length === 0 ? (

{t("reports.noRankingData")}

) : (
    {items.map((item, index) => { const Icon = getCategoryIcon(item.icon); const isSelected = item.category_id === selectedCategoryId; const hasChildren = item.children.length > 0; return (
  • {/* Fixed-width slot on every row, populated or not — a chevron shown only on rows with children would shift their amount left relative to every other row's. */} {hasChildren && ( )}
  • ); })}
)}
{breakdownItem && ( setBreakdownItem(null)} categoryId={breakdownItem.category_id} categoryName={breakdownItem.name} totalAmount={breakdownItem.amount} children={breakdownItem.children} /> )}
); }