import { useState, useEffect } from "react"; import { getGroceryLists, updateGroceryList } from "../groceryApi"; function PinnedGroceryLists({ onShowToast }) { const [pinnedLists, setPinnedLists] = useState([]); useEffect(() => { loadPinnedLists(); }, []); const loadPinnedLists = async () => { try { const allLists = await getGroceryLists(); const pinned = allLists.filter((list) => list.is_pinned); setPinnedLists(pinned); } catch (error) { console.error("Failed to load pinned lists", error); } }; const handleToggleItem = async (listId, itemIndex) => { const list = pinnedLists.find((l) => l.id === listId); if (!list || !list.can_edit) return; const updatedItems = [...list.items]; const item = updatedItems[itemIndex]; if (item.startsWith("✓ ")) { updatedItems[itemIndex] = item.substring(2); } else { updatedItems[itemIndex] = "✓ " + item; } try { await updateGroceryList(listId, { items: updatedItems }); setPinnedLists( pinnedLists.map((l) => l.id === listId ? { ...l, items: updatedItems } : l ) ); } catch (error) { onShowToast?.(error.message, "error"); } }; if (pinnedLists.length === 0) { return null; } return (