"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; } export function NutritionPanel({ recipeId, initialData }: NutritionPanelProps) { const t = useTranslations("nutritionPanel"); const [nutrition, setNutrition] = useState(initialData ?? null); 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); } catch (err) { setError(err instanceof Error ? err.message : t("error")); } finally { setLoading(false); } } if (!nutrition && !loading) { return (
{error &&

{error}

}
); } return (
{t("title")}
{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}
); }