"use client"; import { useState } from "react"; import { useTranslations } from "next-intl"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; type NutritionData = { perServing: { calories: number; proteinG: number; carbsG: number; fatG: number; fiberG: number; sodiumMg: number; }; }; interface NutritionPanelProps { recipeId: string; initialData?: NutritionData | null; initialManual?: boolean; /** AI/USDA estimation feature toggled off for this tier — hides the * (re-)estimate action. Previously-stored data (manual or a past * estimate) still displays; there's just no button to refresh it. */ estimateEnabled?: boolean; } export function NutritionPanel({ recipeId, initialData, initialManual, estimateEnabled = true }: NutritionPanelProps) { const t = useTranslations("nutritionPanel"); const [nutrition, setNutrition] = useState(initialData ?? null); const [manual, setManual] = useState(!!initialManual); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); async function handleEstimate() { setLoading(true); setError(null); try { const res = await fetch(`/api/v1/recipes/${recipeId}/nutrition`, { method: "POST", }); if (!res.ok) { throw new Error(t("estimateFailed")); } const data = await res.json() as { nutrition: NutritionData }; setNutrition(data.nutrition); setManual(false); } catch (err) { setError(err instanceof Error ? err.message : t("error")); } finally { setLoading(false); } } if (!nutrition && !loading) { if (!estimateEnabled) return null; return (
{error &&

{error}

}
); } return (
{t("title")} {estimateEnabled && ( )}
{manual && !loading && (

{t("manualBadge")}

)} {error &&

{error}

}
{nutrition && (
{Math.round(nutrition.perServing.calories)} kcal
)} {loading && !nutrition && (

{t("estimatingNutrition")}

)}
); } function MacroCell({ label, value, unit, }: { label: string; value: number; unit: string; }) { return (
{label} {Math.round(value)} {unit}
); }