'use client'; import { useTransition } from 'react'; import { removePantryItem } from './actions'; type PantryItem = { id: number; product: { id: number; name: string; canonicalName: string | null; category: string | null }; }; type Props = { items: PantryItem[]; }; export default function PantryList({ items }: Props) { const [isPending, startTransition] = useTransition(); function handleRemove(id: number, name: string) { if (!confirm(`Ta bort "${name}" från baslagret?`)) return; startTransition(async () => { await removePantryItem(id); }); } if (items.length === 0) { return (

Baslagret är tomt. Lägg till produkter ovan.

); } // Gruppera per kategori const grouped = items.reduce>((acc, item) => { const cat = item.product.category || 'Övrigt'; if (!acc[cat]) acc[cat] = []; acc[cat].push(item); return acc; }, {}); const sortedCategories = Object.keys(grouped).sort((a, b) => { if (a === 'Övrigt') return 1; if (b === 'Övrigt') return -1; return a.localeCompare(b, 'sv'); }); return (
{sortedCategories.map((category) => (

{category}

{grouped[category].map((item) => { const displayName = item.product.canonicalName || item.product.name; return (
{displayName}
); })}
))}
); }