import { useState, useEffect } from "react"; import { UserTopList, UserTopListItem } from "@/types/database"; import { supabase } from "@/lib/supabaseClient"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { GripVertical, Trash2, Plus } from "lucide-react"; import { toast } from "sonner"; import { ListSearch } from "./ListSearch"; import { getErrorMessage } from "@/lib/errorHandler"; interface ListItemEditorProps { list: UserTopList; onUpdate: () => void; onClose: () => void; } export function ListItemEditor({ list, onUpdate, onClose }: ListItemEditorProps) { const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [showSearch, setShowSearch] = useState(false); useEffect(() => { fetchItems(); }, [list.id]); const fetchItems = async () => { setLoading(true); const { data, error } = await supabase .from("user_top_list_items") .select("*") .eq("list_id", list.id) .order("position", { ascending: true }); if (error) { const errorMessage = getErrorMessage(error); toast.error("Failed to load list items", { description: errorMessage }); } else { setItems(data as UserTopListItem[]); } setLoading(false); }; const handleAddItem = async (entityType: string, entityId: string, entityName: string) => { const newPosition = items.length + 1; const { error } = await supabase .from("user_top_list_items") .insert({ list_id: list.id, entity_type: entityType, entity_id: entityId, position: newPosition, }); if (error) { if (error.code === "23505") { toast.error("This item is already in your list"); } else { const errorMessage = getErrorMessage(error); toast.error("Failed to add item", { description: errorMessage }); } } else { toast.success(`Added ${entityName} to list`); fetchItems(); onUpdate(); setShowSearch(false); } }; const handleRemoveItem = async (itemId: string) => { const { error } = await supabase .from("user_top_list_items") .delete() .eq("id", itemId); if (error) { const errorMessage = getErrorMessage(error); toast.error("Failed to remove item", { description: errorMessage }); } else { toast.success("Item removed"); // Reorder remaining items const remainingItems = items.filter(i => i.id !== itemId); await reorderItems(remainingItems); fetchItems(); onUpdate(); } }; const handleUpdateNotes = async (itemId: string, notes: string) => { const { error } = await supabase .from("user_top_list_items") .update({ notes }) .eq("id", itemId); if (error) { const errorMessage = getErrorMessage(error); toast.error("Failed to update notes", { description: errorMessage }); } else { setItems(items.map(i => i.id === itemId ? { ...i, notes } : i)); } }; const handleMoveItem = async (itemId: string, direction: "up" | "down") => { const currentIndex = items.findIndex(i => i.id === itemId); if ( (direction === "up" && currentIndex === 0) || (direction === "down" && currentIndex === items.length - 1) ) { return; } const newItems = [...items]; const swapIndex = direction === "up" ? currentIndex - 1 : currentIndex + 1; [newItems[currentIndex], newItems[swapIndex]] = [newItems[swapIndex], newItems[currentIndex]]; await reorderItems(newItems); setItems(newItems); onUpdate(); }; const reorderItems = async (orderedItems: UserTopListItem[]) => { const updates = orderedItems.map((item, index) => ({ id: item.id, position: index + 1, })); for (const update of updates) { await supabase .from("user_top_list_items") .update({ position: update.position }) .eq("id", update.id); } }; if (loading) { return
Loading items...
; } return (

Edit List Items

{showSearch && ( setShowSearch(false)} /> )} {items.length === 0 ? (
No items in this list yet. Click "Add Item" to get started.
) : (
{items.map((item, index) => (
{index + 1}

{item.entity_type} - {item.entity_id}