Files
Epicure/apps/web/components/recipe/serving-scaler.tsx
T
Arnaud b0849c3989 feat: cooking history/gallery, unit conversion, nutrition diary, pantry scan, digest cron, nutrition-targeted meal plans
Six M-sized items from HANDOFF.md's new-features backlog:

- Profile tabs: cooking-history stats (total cooked, last-cooked, streak)
  and a "cooked it" photo gallery, both owner-only
- Display-time unit conversion (metric<->imperial) for recipe ingredients,
  respecting each user's unitPref; original value always shown alongside
  the conversion
- Nutrition daily diary: per-day macro totals computed from cooking history
  x recipe nutritionData, compared against user goals
- Pantry scan: real barcode lookup (zxing + Open Food Facts, no API key)
  with an AI-vision fallback for unbarcoded items, always confirm-before-
  insert, both paths tier/rate-limited like other AI features
- Weekly digest email: new followers/comments/ratings + trending recipes,
  sent via a new `cron` Docker stage (alpine+crond+curl) and `digest-cron`
  compose service hitting a bearer-token-protected internal route
- Meal-plan generation can now target a user's nutrition goals as a
  prompt-level nudge (recipes are AI-invented, not DB-sourced, so this
  can't be a hard macro constraint)

Caught a real deploy-breaking issue while adding the cron stage: appending
it after `runner` silently changed the Dockerfile's default build target,
and `web`'s compose config didn't pin one — fixed by pinning `target:
runner` explicitly. Verified with typecheck, lint, and three separate
`docker build --target` runs (runner/cron/migrator) plus `docker compose
config` validation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-10 08:06:28 +02:00

161 lines
5.0 KiB
TypeScript

"use client";
import { useState } from "react";
import { useTranslations } from "next-intl";
import { Minus, Plus, Sparkles, X } from "lucide-react";
import { Button } from "@/components/ui/button";
import { formatIngredientQuantity, type UnitPref } from "@/lib/unit-conversion";
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,
unitPref = "metric",
}: {
baseServings: number;
ingredients: Ingredient[];
recipeTitle?: string;
recipeId?: string;
onAiScale?: (ingredients: ScaledIngredient[] | null) => void;
unitPref?: UnitPref;
}) {
const t = useTranslations("servingScaler");
const [servings, setServings] = useState(baseServings);
const [aiScaledIngredients, setAiScaledIngredients] = useState<ScaledIngredient[] | null>(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 (
<div className="space-y-4">
<div className="flex items-center gap-3 flex-wrap">
<span className="text-sm font-medium text-muted-foreground">{t("servings")}</span>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="sm"
className="h-7 w-7 p-0"
onClick={() => setServings((s) => Math.max(min, s - 1))}
disabled={servings <= min}
>
<Minus className="h-3 w-3" />
</Button>
<span className="w-8 text-center font-semibold tabular-nums">{servings}</span>
<Button
variant="outline"
size="sm"
className="h-7 w-7 p-0"
onClick={() => setServings((s) => Math.min(max, s + 1))}
disabled={servings >= max}
>
<Plus className="h-3 w-3" />
</Button>
</div>
{servings !== baseServings && (
<button
className="text-xs text-muted-foreground hover:text-foreground underline"
onClick={() => setServings(baseServings)}
>
{t("reset")}
</button>
)}
{recipeId && servings !== baseServings && (
<Button
variant="outline"
size="sm"
className="h-7 gap-1 text-xs"
onClick={handleAiScale}
disabled={aiScaling}
>
<Sparkles className="h-3 w-3" />
{aiScaling ? t("scaling") : t("aiScale")}
</Button>
)}
</div>
{aiScaledIngredients && (
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<Sparkles className="h-3 w-3 shrink-0" />
<span>{t("aiScaledNote")}</span>
<button
className="ml-auto hover:text-foreground"
onClick={dismissAiScale}
aria-label={t("dismissAiScaling")}
>
<X className="h-3 w-3" />
</button>
</div>
)}
<ul className="space-y-2">
{ingredients
.sort((a, b) => a.order - b.order)
.map((ing) => {
const aiIng = aiScaledIngredients?.find((s) => s.rawName === ing.rawName);
return (
<li key={ing.id} className="flex gap-2 text-sm group">
<span className="font-medium tabular-nums min-w-[3rem] text-right">
{aiIng
? formatIngredientQuantity(aiIng.quantity, aiIng.unit, unitPref)
: formatIngredientQuantity(ing.quantity, ing.unit, unitPref, {
base: baseServings,
desired: servings,
})}
</span>
<span className="flex items-center gap-1">
{ing.rawName}
{(aiIng?.note ?? ing.note) && (
<span className="text-muted-foreground">, {aiIng?.note ?? ing.note}</span>
)}
{recipeTitle && <SubstituteIngredientPopover ingredient={ing.rawName} recipeTitle={recipeTitle} />}
</span>
</li>
);
})}
</ul>
</div>
);
}