"use client" import { useState, useEffect, useCallback } from "react" import Image from "next/image" import Link from "next/link" import { useRouter } from "next/navigation" import { createClient } from "@/lib/supabase/client" import { posterUrl } from "@/lib/tmdb" import type { TMDBMovie } from "@/lib/tmdb" import { ArrowLeft, Plus, Search, Film, Trash2, X, Loader2, Share2, } from "lucide-react" interface ListItem { id: string tmdb_id: number title: string poster_path: string | null } interface ListDetailContentProps { list: { id: string; name: string; description: string | null } items: ListItem[] } export function ListDetailContent({ list, items: initialItems, }: ListDetailContentProps) { const [items, setItems] = useState(initialItems) const [showSearch, setShowSearch] = useState(false) const [query, setQuery] = useState("") const [searchResults, setSearchResults] = useState([]) const [searching, setSearching] = useState(false) const router = useRouter() const doSearch = useCallback(async (q: string) => { if (!q.trim()) { setSearchResults([]) return } setSearching(true) try { const res = await fetch(`/api/tmdb/search?q=${encodeURIComponent(q)}`) const data = await res.json() setSearchResults(data.results || []) } catch { setSearchResults([]) } setSearching(false) }, []) useEffect(() => { const timer = setTimeout(() => doSearch(query), 400) return () => clearTimeout(timer) }, [query, doSearch]) async function addToList(movie: TMDBMovie) { const supabase = createClient() const { data, error } = await supabase .from("list_items") .insert({ list_id: list.id, tmdb_id: movie.id, title: movie.title, poster_path: movie.poster_path, }) .select() .single() if (!error && data) { setItems([data, ...items]) setShowSearch(false) setQuery("") } } async function removeItem(id: string) { const supabase = createClient() await supabase.from("list_items").delete().eq("id", id) setItems(items.filter((i) => i.id !== id)) } async function handleShare() { const text = `Schau dir meine Filmliste "${list.name}" an: ${items.map((i) => i.title).join(", ")}` if (navigator.share) { try { await navigator.share({ title: list.name, text, url: window.location.href }) } catch { /* cancelled */ } } else { await navigator.clipboard.writeText(text) } } return (

{list.name}

{/* Add movie */} {showSearch ? (
setQuery(e.target.value)} placeholder="Film zur Liste hinzufuegen..." className="h-10 w-full rounded-lg border border-border bg-secondary pl-9 pr-9 text-sm text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring" autoFocus />
{searching && (
)} {searchResults.map((movie) => { const url = posterUrl(movie.poster_path, "w185") const alreadyAdded = items.some((i) => i.tmdb_id === movie.id) return ( ) })}
) : ( )} {/* List items */} {items.length === 0 ? (

Diese Liste ist noch leer

) : (
{items.map((item) => { const url = posterUrl(item.poster_path, "w185") return (
{url ? ( {item.title} ) : (
)}

{item.title}

) })}
)}
) }