import Link from "next/link"; import { createClient } from "@/lib/supabase/server"; import { getCategories } from "@/lib/data/categories"; import { NeedFilters } from "@/components/needs/need-filters"; import { NeedCard } from "@/components/needs/need-card"; import { Button } from "@/components/ui/button"; const PAGE_SIZE = 12; export default async function NeedsFeedPage({ searchParams, }: { searchParams: Promise<{ q?: string; city?: string; category?: string; page?: string; }>; }) { const { q, city, category, page } = await searchParams; const currentPage = Math.max(1, Number(page) || 1); const from = (currentPage - 1) * PAGE_SIZE; const to = from + PAGE_SIZE - 1; const supabase = await createClient(); const categories = await getCategories(); let query = supabase .from("needs") .select( "id, title, city, region, budget_min, budget_max, created_at, categories(name), need_images(storage_path, position)", { count: "exact" } ) .eq("status", "open") .order("created_at", { ascending: false }) .range(from, to); if (q) { query = query.ilike("title", `%${q}%`); } if (city) { query = query.ilike("city", `%${city}%`); } if (category) { const matchedCategory = categories.find((c) => c.slug === category); if (matchedCategory) { query = query.eq("category_id", matchedCategory.id); } } const { data: needs, count } = await query; const total = count ?? 0; const hasNextPage = to + 1 < total; function getImageUrl(path: string) { return supabase.storage.from("need-images").getPublicUrl(path).data .publicUrl; } return (
Aucun besoin ne correspond à ta recherche.
)} {(currentPage > 1 || hasNextPage) && (