import { useState, useCallback } from "react"; import type { ShoppingListItem } from "@/lib/types"; import { getShoppingList, saveShoppingList, } from "@/lib/storage"; function showToast(msg: string) { const t = document.getElementById("toast"); if (!t) return; t.textContent = msg; t.className = "toast success"; setTimeout(() => t.classList.add("show"), 10); setTimeout(() => t.classList.remove("show"), 2500); } export default function ShoppingList() { const [list, setList] = useState(() => getShoppingList()); const save = useCallback((next: ShoppingListItem[]) => { setList(next); saveShoppingList(next); }, []); const togglePurchased = (idx: number) => { const next = [...list]; next[idx] = { ...next[idx], purchased: !next[idx].purchased }; save(next); }; const remove = (idx: number) => { const next = list.filter((_, i) => i !== idx); save(next); }; const clearPurchased = () => { save(list.filter((x) => !x.purchased)); showToast("Purchased items cleared"); }; const clearAll = () => { if (list.length === 0) return; save([]); showToast("Shopping list cleared"); }; const unpurchased = list.filter((x) => !x.purchased); const total = unpurchased.reduce((s, x) => s + x.price, 0); if (list.length === 0) { return (
🛒

Your shopping list is empty.
Search for products and add them here.

); } return (
{list.map((item, i) => (
togglePurchased(i)} style={{ width: "20px", height: "20px", borderRadius: "6px", border: item.purchased ? "2px solid var(--green)" : "2px solid var(--border)", background: item.purchased ? "var(--green)" : "transparent", cursor: "pointer", display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0, fontSize: "0.7rem", fontWeight: 700, color: "#0f1117", }} > {item.purchased && "✓"}
{item.name}
{item.store}
${item.price.toFixed(2)} remove(i)} style={{ color: "var(--text3)", fontSize: "1.2rem", cursor: "pointer", padding: "0 4px", }} > ×
))}
Remaining: {unpurchased.length} item {unpurchased.length !== 1 ? "s" : ""} —{" "} ${total.toFixed(2)}
); }