import Link from "next/link"; import { notFound } from "next/navigation"; import Image from "next/image"; import { createClient } from "@/lib/supabase/server"; import { getCurrentUser } from "@/lib/dal"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { OfferForm } from "@/components/offers/offer-form"; import { OfferList } from "@/components/offers/offer-list"; export default async function NeedDetailPage({ params, }: { params: Promise<{ id: string }>; }) { const { id } = await params; const supabase = await createClient(); const user = await getCurrentUser(); const { data: need } = await supabase .from("needs") .select( "id, title, description, city, region, budget_min, budget_max, status, author_id, categories(name), need_images(storage_path, position)" ) .eq("id", id) .single(); if (!need) { notFound(); } const images = [...(need.need_images ?? [])].sort( (a, b) => a.position - b.position ); const isOwner = user?.id === need.author_id; const category = Array.isArray(need.categories) ? need.categories[0] : need.categories; const { data: offers } = await supabase .from("offers") .select( "id, title, description, price, status, merchant_id, profiles(full_name), offer_images(storage_path, position)" ) .eq("need_id", id) .order("created_at", { ascending: true }); const offerList = offers ?? []; const myOffer = user ? offerList.find((o) => o.merchant_id === user.id) : undefined; function getOfferImageUrl(path: string) { return supabase.storage.from("offer-images").getPublicUrl(path).data .publicUrl; } return (
{need.city} {need.region ? `, ${need.region}` : ""}
{(need.budget_min || need.budget_max) && (Budget : {need.budget_min ?? "?"}€ – {need.budget_max ?? "?"}€
)} {images.length > 0 && ({need.description}
Connecte-toi {" "} pour proposer une offre sur ce besoin.
) : myOffer ? ( <>Ce besoin n'accepte plus d'offres.
)})}