'use client'; import { useState } from 'react'; import Link from 'next/link'; import type { Recipe } from '../../features/inventory/types'; function RecipePlaceholder({ name }: { name: string }) { const initial = name.trim().charAt(0).toUpperCase() || '?'; return (
{initial}
); } export default function RecipeGrid({ recipes }: { recipes: Recipe[] }) { const [search, setSearch] = useState(''); const [sort, setSort] = useState<'name' | 'newest' | 'oldest' | 'ingredients'>('newest'); const [onlyWithImage, setOnlyWithImage] = useState(false); const filtered = recipes .filter((r) => r.name.toLowerCase().includes(search.toLowerCase())) .filter((r) => !onlyWithImage || !!r.imageUrl) .sort((a, b) => { if (sort === 'name') return a.name.localeCompare(b.name, 'sv'); if (sort === 'newest') return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); if (sort === 'oldest') return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(); if (sort === 'ingredients') return (b.ingredients?.length ?? 0) - (a.ingredients?.length ?? 0); return 0; }); return (
setSearch(e.target.value)} style={{ flex: '1 1 200px', padding: '0.6rem 1rem', fontSize: '1rem', border: '1px solid #ced4da', borderRadius: '24px', outline: 'none', boxSizing: 'border-box', }} />
{filtered.length === 0 && (

{search || onlyWithImage ? 'Inga recept matchar filtren.' : 'Inga recept tillagda ännu.'}

)}
{filtered.map((recipe) => (
((e.currentTarget as HTMLDivElement).style.boxShadow = '0 4px 12px rgba(0,0,0,0.12)') } onMouseLeave={(e) => ((e.currentTarget as HTMLDivElement).style.boxShadow = 'none') } > {recipe.imageUrl ? ( {recipe.name} ) : ( )}

{recipe.name}

{recipe.description && (

{recipe.description}

)}
{recipe.ingredients?.length > 0 && ( {recipe.ingredients.length} ingredienser )} {new Date(recipe.createdAt).toLocaleDateString('sv-SE')}
))}
); }