feat: enhance category filter functionality to include child categories in selection and deselection; update transaction page to expand selected categories based on parent-child relationships

This commit is contained in:
Julien Froidefond
2025-12-21 07:38:57 +01:00
parent b4dace0673
commit 55f0e5c625
2 changed files with 67 additions and 9 deletions

View File

@@ -8,6 +8,7 @@ import {
useDuplicateIds,
} from "@/lib/hooks";
import type { TransactionsPaginatedParams } from "@/services/banking.service";
import type { Category } from "@/lib/types";
type SortField = "date" | "amount" | "description";
type SortOrder = "asc" | "desc";
@@ -74,15 +75,35 @@ export function useTransactionsPage() {
const categoryIdsParam = searchParams.get("categoryIds");
const includeUncategorizedParam = searchParams.get("includeUncategorized");
if (categoryIdsParam) {
if (categoryIdsParam && metadata) {
const categoryIds = categoryIdsParam.split(",");
setSelectedCategories(categoryIds);
// Expand parent categories to include their children
const expandedCategoryIds = new Set<string>(categoryIds);
categoryIds.forEach((categoryId) => {
// Check if this is a parent category
const category = metadata.categories.find(
(c: Category) => c.id === categoryId
);
if (category && category.parentId === null) {
// Find all children of this parent
const children = metadata.categories.filter(
(c: Category) => c.parentId === categoryId
);
children.forEach((child: Category) => {
expandedCategoryIds.add(child.id);
});
}
});
setSelectedCategories(Array.from(expandedCategoryIds));
setPage(0);
} else if (includeUncategorizedParam === "true") {
setSelectedCategories(["uncategorized"]);
setPage(0);
}
}, [searchParams]);
}, [searchParams, metadata]);
// Calculate start date based on period
const startDate = useMemo(() => {