cd444d4d23
Pantry page now shows a "Use it up soon" widget with recipes that use soon-to-expire pantry items, across the user's own recipes plus public/ unlisted ones (the existing /recipes/can-cook page only looked at the user's own). Extracted the matching/scoring logic shared by both into lib/pantry-match.ts. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import type { Metadata } from "next";
|
|
import { headers } from "next/headers";
|
|
import { auth } from "@/lib/auth/server";
|
|
import { db, recipes, pantryItems } from "@epicure/db";
|
|
import { eq } from "@epicure/db";
|
|
import { getPublicUrl } from "@/lib/storage";
|
|
import { CanCookContent } from "@/components/recipe/can-cook-content";
|
|
import { scoreRecipesAgainstPantry } from "@/lib/pantry-match";
|
|
|
|
export const metadata: Metadata = {};
|
|
|
|
export default async function CanCookPage() {
|
|
const session = await auth.api.getSession({ headers: await headers() });
|
|
if (!session) return null;
|
|
|
|
const [userRecipes, pantry] = await Promise.all([
|
|
db.query.recipes.findMany({
|
|
where: eq(recipes.authorId, session.user.id),
|
|
with: {
|
|
ingredients: true,
|
|
photos: true,
|
|
},
|
|
}),
|
|
db.query.pantryItems.findMany({
|
|
where: eq(pantryItems.userId, session.user.id),
|
|
}),
|
|
]);
|
|
|
|
const scored = scoreRecipesAgainstPantry(userRecipes, pantry).map((s) => {
|
|
const cover = s.recipe.photos?.find((p) => p.isCover) ?? s.recipe.photos?.[0];
|
|
return {
|
|
...s,
|
|
recipe: {
|
|
id: s.recipe.id,
|
|
title: s.recipe.title,
|
|
description: s.recipe.description,
|
|
photos: cover ? [{ url: getPublicUrl(cover.storageKey) }] : [],
|
|
},
|
|
};
|
|
});
|
|
|
|
return <CanCookContent pantryCount={pantry.length} scored={scored} />;
|
|
}
|