afff6cf9eb
- Substitute finder never applied the user's BYOK/admin AI key (only fell back to raw process.env), so it silently failed whenever the key was stored via settings rather than a literal env var. Now resolves via getDefaultProviderWithKey like every other AI route. Popover also surfaces real error messages instead of swallowing failures. - Ingredients with quantity 0 (salt, pepper, "to taste") rendered the literal digit "0" in cooking mode, print view, public recipe page, shopping lists, and serving scaler — several sites relied on generic truthiness/filter(Boolean), which doesn't catch a stored "0" string. Added a shared hasQuantity() helper and applied it everywhere quantity is rendered, plus in the AI recipe-chat context sent to the model. - Recipe chat panel rendered a duplicate close button on top of shadcn Sheet's own built-in close X, producing a garbled overlapping glyph. Removed the duplicate. - Recipe chat assistant replies are markdown from the model but were rendered as raw text; added react-markdown so formatting actually renders. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
121 lines
4.2 KiB
TypeScript
121 lines
4.2 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
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;
|
|
};
|
|
|
|
export function ShoppingListView({
|
|
listId,
|
|
initialItems,
|
|
}: {
|
|
listId: string;
|
|
initialItems: Item[];
|
|
}) {
|
|
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("Failed to move items to pantry"); return; }
|
|
toast.success(`${checkedItems.length} item${checkedItems.length !== 1 ? "s" : ""} added to pantry`);
|
|
} finally {
|
|
setMovingToPantry(false);
|
|
}
|
|
}
|
|
|
|
async function toggleItem(item: Item) {
|
|
const next = !item.checked;
|
|
setItems((prev) => prev.map((i) => i.id === item.id ? { ...i, checked: next } : i));
|
|
await fetch(`/api/v1/shopping-lists/${listId}/items/${item.id}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ checked: next }),
|
|
});
|
|
}
|
|
|
|
const grouped = items.reduce<Record<string, Item[]>>((acc, item) => {
|
|
const key = item.aisle ?? "Other";
|
|
(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">This list is empty.</p>;
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="flex items-center justify-between">
|
|
<p className="text-sm text-muted-foreground">{checkedCount}/{items.length} checked</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" />}
|
|
Move {checkedCount} to pantry
|
|
</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)}
|
|
className="w-full flex items-center gap-3 px-4 py-3 hover:bg-muted/30 text-left transition-colors"
|
|
>
|
|
<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>
|
|
{(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>
|
|
);
|
|
}
|