45b886e398
Five S-sized items from HANDOFF.md's new-features backlog, all wiring up previously-orphaned infra: - createNotification now sends web push + email for every notification type (follow/comment/reply/reaction/rating/mention), not just comments - Personal recipe notes: private per-user notes on any viewable recipe (recipeNotes table had zero API/UI before this) - Recipe fork/clone: deep-copies a viewable recipe into your own library as a private draft, linked via recipeVariations, respects tier quota - Pantry-aware shopping lists: meal-plan-generated lists now subtract on-hand pantry quantities (ingredientId match, falling back to normalized name match) and flag partial/ambiguous matches instead of guessing - GDPR data export: downloadable JSON of a user's own content and activity across every relevant table, secrets/internal tables excluded New migrations 0025 (unique index for recipe-notes upsert) and 0026 (shopping_list_items.in_pantry) generated, left unapplied like 0023/0024. Verified with typecheck, lint, and a full local `docker build`. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
141 lines
5.1 KiB
TypeScript
141 lines
5.1 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { useTranslations } from "next-intl";
|
|
import { cn } from "@/lib/utils";
|
|
import { Check, Package, Loader2 } from "lucide-react";
|
|
import { toast } from "sonner";
|
|
import { Button } from "@/components/ui/button";
|
|
import { hasQuantity } from "@/lib/fractions";
|
|
|
|
type Item = {
|
|
id: string;
|
|
rawName: string;
|
|
quantity: string | null;
|
|
unit: string | null;
|
|
aisle: string | null;
|
|
checked: boolean;
|
|
inPantry?: boolean;
|
|
};
|
|
|
|
export function ShoppingListView({
|
|
listId,
|
|
initialItems,
|
|
readOnly = false,
|
|
}: {
|
|
listId: string;
|
|
initialItems: Item[];
|
|
readOnly?: boolean;
|
|
}) {
|
|
const t = useTranslations("mealPlan");
|
|
const tShopping = useTranslations("shoppingLists");
|
|
const tCommon = useTranslations("common");
|
|
const [items, setItems] = useState<Item[]>(initialItems);
|
|
const [movingToPantry, setMovingToPantry] = useState(false);
|
|
|
|
const checkedItems = items.filter((i) => i.checked);
|
|
|
|
async function moveToPantry() {
|
|
if (checkedItems.length === 0) return;
|
|
setMovingToPantry(true);
|
|
try {
|
|
const res = await fetch("/api/v1/pantry/bulk", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
items: checkedItems.map((i) => ({
|
|
rawName: i.rawName,
|
|
quantity: i.quantity ?? undefined,
|
|
unit: i.unit ?? undefined,
|
|
})),
|
|
}),
|
|
});
|
|
if (!res.ok) { toast.error(t("moveToPantryFailed")); return; }
|
|
toast.success(t("addedToPantry", { count: checkedItems.length }));
|
|
} finally {
|
|
setMovingToPantry(false);
|
|
}
|
|
}
|
|
|
|
async function toggleItem(item: Item) {
|
|
if (readOnly) return;
|
|
const next = !item.checked;
|
|
setItems((prev) => prev.map((i) => i.id === item.id ? { ...i, checked: next } : i));
|
|
try {
|
|
const res = await fetch(`/api/v1/shopping-lists/${listId}/items/${item.id}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ checked: next }),
|
|
});
|
|
if (!res.ok) throw new Error();
|
|
} catch {
|
|
setItems((prev) => prev.map((i) => i.id === item.id ? { ...i, checked: !next } : i));
|
|
toast.error(tCommon("updateFailed"));
|
|
}
|
|
}
|
|
|
|
const grouped = items.reduce<Record<string, Item[]>>((acc, item) => {
|
|
const key = item.aisle ?? t("aisleOther");
|
|
(acc[key] ??= []).push(item);
|
|
return acc;
|
|
}, {});
|
|
|
|
const checkedCount = items.filter((i) => i.checked).length;
|
|
|
|
if (items.length === 0) {
|
|
return <p className="text-muted-foreground text-sm">{t("listEmptyState")}</p>;
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="flex items-center justify-between">
|
|
<p className="text-sm text-muted-foreground">{tShopping("checkedCount", { checked: checkedCount, total: items.length })}</p>
|
|
{checkedCount > 0 && (
|
|
<Button size="sm" variant="outline" onClick={moveToPantry} disabled={movingToPantry}>
|
|
{movingToPantry ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Package className="h-3.5 w-3.5" />}
|
|
{t("moveToPantry", { count: checkedCount })}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
|
|
{Object.entries(grouped).sort(([a], [b]) => a.localeCompare(b)).map(([aisle, aisleItems]) => (
|
|
<div key={aisle} className="space-y-2">
|
|
{Object.keys(grouped).length > 1 && (
|
|
<h2 className="text-xs font-semibold uppercase tracking-widest text-muted-foreground">{aisle}</h2>
|
|
)}
|
|
<div className="rounded-xl border divide-y">
|
|
{aisleItems.map((item) => (
|
|
<button
|
|
key={item.id}
|
|
onClick={() => toggleItem(item)}
|
|
disabled={readOnly}
|
|
className="w-full flex items-center gap-3 px-4 py-3 hover:bg-muted/30 text-left transition-colors disabled:cursor-default disabled:hover:bg-transparent"
|
|
>
|
|
<div className={cn(
|
|
"h-5 w-5 rounded border-2 flex items-center justify-center shrink-0 transition-colors",
|
|
item.checked ? "bg-primary border-primary" : "border-input"
|
|
)}>
|
|
{item.checked && <Check className="h-3 w-3 text-primary-foreground" />}
|
|
</div>
|
|
<span className={cn("flex-1 text-sm", item.checked && "line-through text-muted-foreground")}>
|
|
{item.rawName}
|
|
</span>
|
|
{item.inPantry && (
|
|
<span className="text-[10px] font-medium uppercase tracking-wide text-emerald-600 dark:text-emerald-400 bg-emerald-500/10 rounded px-1.5 py-0.5 shrink-0">
|
|
{tShopping("alreadyInPantry")}
|
|
</span>
|
|
)}
|
|
{(hasQuantity(item.quantity) || item.unit) && (
|
|
<span className={cn("text-xs text-muted-foreground tabular-nums shrink-0", item.checked && "opacity-50")}>
|
|
{hasQuantity(item.quantity) ? item.quantity : ""}{item.unit ? ` ${item.unit}` : ""}
|
|
</span>
|
|
)}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|