Files
Epicure/apps/web/app/(app)/pantry/page.tsx
T
Arnaud 002f14ced0 feat: batch-cook shopping list (already worked) + leftover expiry reminders
Shopping list add already worked generically for batch-cook recipes —
no code needed there.

New: mark a specific batch-cook dish as "cooked today", track its
fridge expiry (cookingHistory.batchDishId), surface a "Leftovers
expiring soon" widget on the pantry page, and send a daily push+email
reminder via a new /api/internal/cron/leftover-reminders endpoint
(mirrors the weekly-digest cron pattern; doesn't use the social
notifications table, which requires a non-null actor and isn't built
for self-reminders).

Also fixes, from user-reported bugs:
- Recipe cards showed no batch-cook badge/dish-count/prep-time in some
  views — added dishCount + prepMins/cookMins (now generated by the AI
  and persisted) to the card component and /recipes query.
- Batch-cook descriptions occasionally contained raw markdown
  (**bold**) — added explicit "plain prose only" prompt instructions
  and a stripMarkdown() defensive fallback at render time.
- Truncated/cut-off descriptions — the generateObject call had no
  maxOutputTokens set, so long structured responses could get cut off
  mid-field; now capped explicitly at 8000.
- Generate dialogs (batch-cook + the main AI dialog) could show
  buttons unreachable once the progress bar appeared mid-generation —
  restructured so the action row is pinned outside the scrollable
  content area, not affected by content height changes.
- /api/internal/* routes were being redirected to /login by middleware
  before their own CRON_SECRET check ever ran (pre-existing bug,
  affected the weekly-digest cron too) — added to PUBLIC_PATHS.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-12 10:03:52 +02:00

85 lines
3.3 KiB
TypeScript

import type { Metadata } from "next";
import { headers } from "next/headers";
import { auth } from "@/lib/auth/server";
import { db, pantryItems, recipes, cookingHistory, eq, and, or, inArray, isNotNull } from "@epicure/db";
import { asc } from "@epicure/db";
import { PantryManager } from "@/components/meal-plan/pantry-manager";
import { PantryPageHeader } from "@/components/pantry/pantry-page-header";
import { ExpiringSoonSuggestions } from "@/components/pantry/expiring-soon-suggestions";
import { ExpiringLeftovers } from "@/components/pantry/expiring-leftovers";
import { scoreRecipesAgainstPantry } from "@/lib/pantry-match";
import { dishExpiresAt, daysUntil, isLeftoverExpiringSoon } from "@/lib/leftover-match";
import { getPublicUrl } from "@/lib/storage";
export const metadata: Metadata = {};
export default async function PantryPage() {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) return null;
const [items, candidateRecipes, cookedDishes] = await Promise.all([
db.query.pantryItems.findMany({
where: eq(pantryItems.userId, session.user.id),
orderBy: asc(pantryItems.rawName),
}),
db.query.recipes.findMany({
where: or(eq(recipes.authorId, session.user.id), inArray(recipes.visibility, ["public", "unlisted"])),
with: { ingredients: true, photos: true },
}),
db.query.cookingHistory.findMany({
where: and(eq(cookingHistory.userId, session.user.id), isNotNull(cookingHistory.batchDishId)),
with: {
batchDish: { columns: { id: true, name: true, fridgeDays: true } },
recipe: { columns: { id: true, title: true } },
},
}),
]);
const mappedItems = items.map((i) => ({
id: i.id,
rawName: i.rawName,
quantity: i.quantity,
unit: i.unit,
expiresAt: i.expiresAt?.toISOString() ?? null,
}));
const suggestions = scoreRecipesAgainstPantry(candidateRecipes, items)
.filter((s) => s.usesExpiring.length > 0)
.slice(0, 3)
.map((s) => {
const cover = s.recipe.photos?.find((p) => p.isCover) ?? s.recipe.photos?.[0];
return {
id: s.recipe.id,
title: s.recipe.title,
photoUrl: cover ? getPublicUrl(cover.storageKey) : null,
usesExpiring: s.usesExpiring,
};
});
const latestByDish = new Map<string, (typeof cookedDishes)[number]>();
for (const log of cookedDishes) {
if (!log.batchDish) continue;
const existing = latestByDish.get(log.batchDish.id);
if (!existing || log.cookedAt > existing.cookedAt) latestByDish.set(log.batchDish.id, log);
}
const leftovers = [...latestByDish.values()]
.filter((log) => log.batchDish && isLeftoverExpiringSoon(log.cookedAt, log.batchDish.fridgeDays))
.map((log) => ({
recipeId: log.recipe.id,
dishName: log.batchDish!.name,
recipeTitle: log.recipe.title,
expiresAt: dishExpiresAt(log.cookedAt, log.batchDish!.fridgeDays).toISOString(),
daysLeft: daysUntil(dishExpiresAt(log.cookedAt, log.batchDish!.fridgeDays)),
}))
.sort((a, b) => a.daysLeft - b.daysLeft);
return (
<div className="space-y-6">
<PantryPageHeader items={mappedItems} />
<ExpiringLeftovers leftovers={leftovers} />
<ExpiringSoonSuggestions suggestions={suggestions} />
<PantryManager key={mappedItems.map((i) => i.id).join(",")} initialItems={mappedItems} />
</div>
);
}