import { useTranslation } from "react-i18next"; import { getTagsForHobbies } from "@/shared/hobbyConfig"; import { usePopularItems } from "../../hooks/useOnboarding"; import { SelectableItemCard } from "./SelectableItemCard"; interface OnboardingItemBrowserProps { selectedHobbies: string[]; selectedItemIds: Set; onToggleItem: (itemId: number) => void; onContinue: () => void; onSkip: () => void; } export function OnboardingItemBrowser({ selectedHobbies, selectedItemIds, onToggleItem, onContinue, onSkip, }: OnboardingItemBrowserProps) { const { t } = useTranslation("onboarding"); const tags = getTagsForHobbies(selectedHobbies); const { data: items, isLoading } = usePopularItems(tags); const hasItems = items && items.length > 0; // Group items by category, cap at 5 categories with 4 items each const MAX_CATEGORIES = 5; const MAX_PER_CATEGORY = 4; const allGrouped = hasItems ? items.reduce>((acc, item) => { const cat = item.category || "other"; const label = cat.charAt(0).toUpperCase() + cat.slice(1); if (!acc[label]) acc[label] = []; acc[label].push(item); return acc; }, {}) : {}; // Take top categories by item count, limit items per category const categories = Object.keys(allGrouped) .sort((a, b) => allGrouped[b].length - allGrouped[a].length) .slice(0, MAX_CATEGORIES); const grouped = Object.fromEntries( categories.map((cat) => [cat, allGrouped[cat].slice(0, MAX_PER_CATEGORY)]), ); return (

{selectedHobbies.length === 1 ? t("items.title", { hobby: selectedHobbies[0] }) : t("items.titleMultiple")}

{t("items.subtitle")}

{isLoading && (
)} {!isLoading && !hasItems && (

{t("items.noCatalog")}

{t("items.noCatalogDescription")}

)} {!isLoading && hasItems && categories.map((cat) => (

{cat}

{grouped[cat].map((item) => ( onToggleItem(item.id)} /> ))}
))}
{hasItems && selectedItemIds.size > 0 && ( )}
); }