Files
Epicure/apps/web/components/recipe/serving-scaler.tsx
T
Arnaud 08ab9ac71f i18n: translate meal/drink pairing, nutrition, comments, DMs, people search, URL import
Full sweep of hardcoded English strings across:
- Meal pairing and drink pairing dialogs (titles, descriptions, role/
  type labels, regenerate/generate buttons, progress labels) — also
  fixed drink type labels that had French text hardcoded regardless
  of locale ("Sans alcool"/"Chaud").
- Nutrition panel — had no useTranslations at all.
- Comments: header, empty/loading state, post/reply/cancel/delete
  buttons, relative timestamps (just now/Xm ago/Xh ago/Xd ago), and
  comment-reactions' toasts + aria-labels.
- Rating stars toasts.
- Direct messages: thread, conversation list, message button, both
  /messages pages.
- People search page and component.
- URL import dialog (title, description, buttons, toasts).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-09 03:20:49 +02:00

159 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 { 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 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);
const scaled = scaleQuantity(ing.quantity, baseServings, servings);
return (
<li key={ing.id} className="flex gap-2 text-sm group">
<span className="font-medium tabular-nums min-w-[3rem] text-right">
{aiIng
? `${hasQuantity(aiIng.quantity) ? aiIng.quantity : ""}${aiIng.unit ? ` ${aiIng.unit}` : ""}`
: scaled
? `${scaled}${ing.unit ? ` ${ing.unit}` : ""}`
: ing.unit ?? ""}
</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>
);
}