import { headers } from "next/headers"; import { auth } from "@/lib/auth/server"; import { db, mealPlans, eq, and } from "@epicure/db"; import { PrintTrigger } from "@/components/recipe/print-trigger"; const DAYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] as const; const DAY_LABELS: Record = { mon: "Monday", tue: "Tuesday", wed: "Wednesday", thu: "Thursday", fri: "Friday", sat: "Saturday", sun: "Sunday", }; const MEAL_ORDER = ["breakfast", "lunch", "dinner", "snack"] as const; const MEAL_LABELS: Record = { breakfast: "Breakfast", lunch: "Lunch", dinner: "Dinner", snack: "Snack", }; function getMonday(dateStr?: string): Date { const d = dateStr ? new Date(dateStr) : new Date(); const day = d.getDay(); const diff = day === 0 ? -6 : 1 - day; d.setDate(d.getDate() + diff); d.setHours(0, 0, 0, 0); return d; } export default async function MealPlanPrintPage({ searchParams, }: { searchParams: Promise<{ week?: string }>; }) { const { week } = await searchParams; const session = await auth.api.getSession({ headers: await headers() }); if (!session) return null; const monday = getMonday(week); const weekStart = monday.toISOString().slice(0, 10); const sunday = new Date(monday); sunday.setDate(sunday.getDate() + 6); const plan = await db.query.mealPlans.findFirst({ where: and(eq(mealPlans.userId, session.user.id), eq(mealPlans.weekStart, weekStart)), with: { entries: { with: { recipe: true } } }, }); const label = `${monday.toLocaleDateString("en-US", { month: "short", day: "numeric" })} โ€“ ${sunday.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })}`; const entries = plan?.entries ?? []; return ( <>

Meal Plan

{label}

{MEAL_ORDER.map((m) => ( ))} {DAYS.map((day) => ( {MEAL_ORDER.map((mealType) => { const entry = entries.find((e) => e.day === day && e.mealType === mealType); return ( ); })} ))}
Day{MEAL_LABELS[m]}
{DAY_LABELS[day]} {entry ? ( <> {entry.recipe?.title ?? entry.note ?? "โ€”"} {entry.servings && ( ยท {entry.servings} srv )} ) : ( โ€” )}
); }