"use client"; import { useState } from "react"; import { Minus, Plus, Sparkles, X } from "lucide-react"; import { Button } from "@/components/ui/button"; import { scaleQuantity, hasQuantity } from "@/lib/fractions"; import { SubstituteIngredientPopover } from "@/components/recipe/substitute-ingredient-popover"; type Ingredient = { id: string; rawName: string; quantity: string | null; unit: string | null; note: string | null; order: number; }; export type ScaledIngredient = { rawName: string; quantity: string; unit: string | null; note?: string; }; export function ServingScaler({ baseServings, ingredients, recipeTitle, recipeId, onAiScale, }: { baseServings: number; ingredients: Ingredient[]; recipeTitle?: string; recipeId?: string; onAiScale?: (ingredients: ScaledIngredient[] | null) => void; }) { const [servings, setServings] = useState(baseServings); const [aiScaledIngredients, setAiScaledIngredients] = useState(null); const [aiScaling, setAiScaling] = useState(false); const min = 1; const max = 100; async function handleAiScale() { if (!recipeId) return; setAiScaling(true); try { const res = await fetch("/api/v1/ai/scale", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ recipeId, targetServings: servings }), }); if (!res.ok) return; const data = await res.json() as { ingredients: ScaledIngredient[] }; setAiScaledIngredients(data.ingredients); onAiScale?.(data.ingredients); } finally { setAiScaling(false); } } function dismissAiScale() { setAiScaledIngredients(null); onAiScale?.(null); } return (
Servings
{servings}
{servings !== baseServings && ( )} {recipeId && servings !== baseServings && ( )}
{aiScaledIngredients && (
AI-scaled quantities shown below
)}
); }