import type { Metadata } from "next"; import { notFound } from "next/navigation"; import { headers } from "next/headers"; import Link from "next/link"; import { Globe } from "lucide-react"; import { auth } from "@/lib/auth/server"; import { db, shoppingLists, eq, and } from "@epicure/db"; import { buttonVariants } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { getMessages, formatMessage } from "@/lib/i18n/server"; type Params = { params: Promise<{ id: string }> }; export async function generateMetadata({ params }: Params): Promise { const { id } = await params; const list = await db.query.shoppingLists.findFirst({ where: and(eq(shoppingLists.id, id), eq(shoppingLists.isPublic, true)), }); if (!list) return {}; return { title: list.name }; } export default async function PublicShoppingListPage({ params }: Params) { const { id } = await params; const session = await auth.api.getSession({ headers: await headers() }).catch(() => null); const m = getMessages((session?.user as { locale?: string } | undefined)?.locale); const OTHER = m.mealPlan.aisleOther; const list = await db.query.shoppingLists.findFirst({ where: and(eq(shoppingLists.id, id), eq(shoppingLists.isPublic, true)), with: { items: { orderBy: (t, { asc }) => [asc(t.aisle), asc(t.sortOrder), asc(t.rawName)] } }, }); if (!list) notFound(); const byAisle = list.items.reduce>((acc, item) => { const key = item.aisle ?? OTHER; (acc[key] ??= []).push(item); return acc; }, {}); const aisles = Object.keys(byAisle).sort((a, b) => (a === OTHER ? 1 : b === OTHER ? -1 : a.localeCompare(b))); return (
{m.publicShoppingList.sharedList} {!session && (
{m.publicRecipe.signUpFree} {m.publicRecipe.logIn}
)}

{list.name}

{formatMessage(m.shoppingLists.itemsChecked, { checked: list.items.filter((i) => i.checked).length, total: list.items.length, })}

{aisles.map((aisle) => (
{aisles.length > 1 && (

{aisle}

)}
    {byAisle[aisle]!.map((item) => (
  • {[item.quantity, item.unit].filter(Boolean).join(" ")} {item.rawName}
  • ))}
))} {!session && (

{m.publicShoppingList.wantYourOwn}

{m.publicRecipe.wantToSaveDescription}

{m.publicRecipe.getStartedFree}
)}
); }