import { notFound } from "next/navigation";
import { headers } from "next/headers";
import { auth } from "@/lib/auth/server";
import { db, collections, eq, and, or } from "@epicure/db";
import { PrintTrigger } from "@/components/recipe/print-trigger";
import { formatIngredientQuantity } from "@/lib/unit-conversion";
import { getMessages, formatMessage } from "@/lib/i18n/server";
type Params = { params: Promise<{ id: string }> };
export default async function CollectionPrintPage({ params }: Params) {
const { id } = await params;
const session = await auth.api.getSession({ headers: await headers() });
if (!session) return null;
const m = getMessages((session.user as { locale?: string }).locale);
const unitPref = (session.user as { unitPref?: string }).unitPref === "imperial" ? "imperial" : "metric";
const col = await db.query.collections.findFirst({
where: and(
eq(collections.id, id),
or(eq(collections.userId, session.user.id), eq(collections.isPublic, true))
),
with: {
recipes: {
with: {
recipe: {
with: {
ingredients: { orderBy: (t, { asc }) => asc(t.order) },
steps: { orderBy: (t, { asc }) => asc(t.order) },
},
},
},
},
},
});
if (!col) notFound();
const recipeEntries = col.recipes.filter((r) => r.recipe !== null);
return (
<>
{col.name}
{col.description &&
{col.description}
}
{recipeEntries.length} recipe{recipeEntries.length !== 1 ? "s" : ""}
{recipeEntries.map(({ recipe }) => {
if (!recipe) return null;
const totalMins = (recipe.prepMins ?? 0) + (recipe.cookMins ?? 0);
return (
{recipe.title}
{recipe.description && {recipe.description}
}
{recipe.baseServings && {formatMessage(m.recipe.servings, { count: recipe.baseServings })}}
{recipe.prepMins && {formatMessage(m.recipe.prep, { mins: recipe.prepMins })}}
{recipe.cookMins && {formatMessage(m.recipe.cook, { mins: recipe.cookMins })}}
{totalMins > 0 && {formatMessage(m.recipe.total, { mins: totalMins })}}
{recipe.difficulty && {recipe.difficulty.charAt(0).toUpperCase() + recipe.difficulty.slice(1)}}
{recipe.ingredients.length > 0 && (
<>
{m.recipe.ingredients}
{recipe.ingredients.map((ing) => (
-
{formatIngredientQuantity(ing.quantity, ing.unit, unitPref)}
{ing.rawName}
{ing.note && ({ing.note})}
))}
>
)}
{recipe.steps.length > 0 && (
<>
{m.recipe.instructions}
{recipe.steps.map((step) => (
-
{step.instruction}
{!!step.timerSeconds && (
⏱ {Math.floor(step.timerSeconds / 60)} min
)}
))}
>
)}
);
})}
>
);
}