feat: cooking history/gallery, unit conversion, nutrition diary, pantry scan, digest cron, nutrition-targeted meal plans
Six M-sized items from HANDOFF.md's new-features backlog: - Profile tabs: cooking-history stats (total cooked, last-cooked, streak) and a "cooked it" photo gallery, both owner-only - Display-time unit conversion (metric<->imperial) for recipe ingredients, respecting each user's unitPref; original value always shown alongside the conversion - Nutrition daily diary: per-day macro totals computed from cooking history x recipe nutritionData, compared against user goals - Pantry scan: real barcode lookup (zxing + Open Food Facts, no API key) with an AI-vision fallback for unbarcoded items, always confirm-before- insert, both paths tier/rate-limited like other AI features - Weekly digest email: new followers/comments/ratings + trending recipes, sent via a new `cron` Docker stage (alpine+crond+curl) and `digest-cron` compose service hitting a bearer-token-protected internal route - Meal-plan generation can now target a user's nutrition goals as a prompt-level nudge (recipes are AI-invented, not DB-sourced, so this can't be a hard macro constraint) Caught a real deploy-breaking issue while adding the cron stage: appending it after `runner` silently changed the Dockerfile's default build target, and `web`'s compose config didn't pin one — fixed by pinning `target: runner` explicitly. Verified with typecheck, lint, and three separate `docker build --target` runs (runner/cron/migrator) plus `docker compose config` validation. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,10 +9,13 @@ import {
|
||||
recipes,
|
||||
userFollows,
|
||||
userBlocks,
|
||||
cookingHistory,
|
||||
ratings,
|
||||
eq,
|
||||
and,
|
||||
desc,
|
||||
count,
|
||||
isNotNull,
|
||||
} from "@epicure/db";
|
||||
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
@@ -20,14 +23,32 @@ import { FollowButton } from "@/components/social/follow-button";
|
||||
import { BlockButton } from "@/components/social/block-button";
|
||||
import { MessageButton } from "@/components/social/message-button";
|
||||
import { getPublicUrl } from "@/lib/storage";
|
||||
import { ProfileTabs, type HistoryData, type PhotosData } from "@/components/profile/profile-tabs";
|
||||
|
||||
const PAGE_SIZE = 24;
|
||||
|
||||
type Params = {
|
||||
params: Promise<{ username: string }>;
|
||||
searchParams: Promise<{ page?: string }>;
|
||||
searchParams: Promise<{ page?: string; tab?: string; ppage?: string }>;
|
||||
};
|
||||
|
||||
/** Consecutive-day streak (UTC calendar days) ending today or yesterday. */
|
||||
function computeStreak(dates: Date[]): number {
|
||||
const daySet = new Set(dates.map((d) => d.toISOString().slice(0, 10)));
|
||||
const cursor = new Date();
|
||||
cursor.setUTCHours(0, 0, 0, 0);
|
||||
if (!daySet.has(cursor.toISOString().slice(0, 10))) {
|
||||
cursor.setUTCDate(cursor.getUTCDate() - 1);
|
||||
if (!daySet.has(cursor.toISOString().slice(0, 10))) return 0;
|
||||
}
|
||||
let streak = 0;
|
||||
while (daySet.has(cursor.toISOString().slice(0, 10))) {
|
||||
streak++;
|
||||
cursor.setUTCDate(cursor.getUTCDate() - 1);
|
||||
}
|
||||
return streak;
|
||||
}
|
||||
|
||||
export async function generateMetadata({ params }: Params) {
|
||||
const { username } = await params;
|
||||
const user = await db.query.users.findFirst({ where: eq(users.username, username) });
|
||||
@@ -36,9 +57,12 @@ export async function generateMetadata({ params }: Params) {
|
||||
|
||||
export default async function UserProfilePage({ params, searchParams }: Params) {
|
||||
const { username } = await params;
|
||||
const { page: pageParam } = await searchParams;
|
||||
const { page: pageParam, tab: tabParam, ppage: ppageParam } = await searchParams;
|
||||
const page = Math.max(1, parseInt(pageParam ?? "1", 10) || 1);
|
||||
const offset = (page - 1) * PAGE_SIZE;
|
||||
const photoPage = Math.max(1, parseInt(ppageParam ?? "1", 10) || 1);
|
||||
const photoOffset = (photoPage - 1) * PAGE_SIZE;
|
||||
const activeTab = tabParam === "history" || tabParam === "photos" ? tabParam : "recipes";
|
||||
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
|
||||
@@ -96,6 +120,89 @@ export default async function UserProfilePage({ params, searchParams }: Params)
|
||||
isBlocked = !!blockRow;
|
||||
}
|
||||
|
||||
// Cooking history and "cooked it" photo gallery are only meaningful (and visible) to the
|
||||
// profile owner: history reveals private behavioral/timing patterns, and photos may be
|
||||
// attached to reviews of recipes that aren't visible to other viewers (private recipes),
|
||||
// so surfacing them to non-owners could leak the existence of content they can't access.
|
||||
let historyData: HistoryData | null = null;
|
||||
let photosData: PhotosData | null = null;
|
||||
|
||||
if (isOwnProfile) {
|
||||
const [totalCookedRow, cookedDatesRows, lastCookedRow, recentEntries, totalPhotosRow, photoRows] =
|
||||
await Promise.all([
|
||||
db.select({ count: count() }).from(cookingHistory).where(eq(cookingHistory.userId, user.id)),
|
||||
db.query.cookingHistory.findMany({
|
||||
where: eq(cookingHistory.userId, user.id),
|
||||
columns: { cookedAt: true },
|
||||
orderBy: desc(cookingHistory.cookedAt),
|
||||
limit: 3650,
|
||||
}),
|
||||
db.query.cookingHistory.findFirst({
|
||||
where: eq(cookingHistory.userId, user.id),
|
||||
orderBy: desc(cookingHistory.cookedAt),
|
||||
with: { recipe: { columns: { id: true, title: true } } },
|
||||
}),
|
||||
db.query.cookingHistory.findMany({
|
||||
where: eq(cookingHistory.userId, user.id),
|
||||
orderBy: desc(cookingHistory.cookedAt),
|
||||
limit: 20,
|
||||
with: { recipe: { columns: { id: true, title: true } } },
|
||||
}),
|
||||
db
|
||||
.select({ count: count() })
|
||||
.from(ratings)
|
||||
.where(and(eq(ratings.userId, user.id), isNotNull(ratings.photoKey))),
|
||||
db.query.ratings.findMany({
|
||||
where: and(eq(ratings.userId, user.id), isNotNull(ratings.photoKey)),
|
||||
orderBy: desc(ratings.createdAt),
|
||||
limit: PAGE_SIZE,
|
||||
offset: photoOffset,
|
||||
with: { recipe: { columns: { id: true, title: true } } },
|
||||
}),
|
||||
]);
|
||||
|
||||
const totalCooked = totalCookedRow[0]?.count ?? 0;
|
||||
const streak = computeStreak(cookedDatesRows.map((r) => r.cookedAt));
|
||||
const totalPhotos = totalPhotosRow[0]?.count ?? 0;
|
||||
|
||||
historyData = {
|
||||
totalCooked,
|
||||
streak,
|
||||
lastCooked:
|
||||
lastCookedRow && lastCookedRow.recipe
|
||||
? {
|
||||
recipeId: lastCookedRow.recipe.id,
|
||||
recipeTitle: lastCookedRow.recipe.title,
|
||||
cookedAt: lastCookedRow.cookedAt.toISOString(),
|
||||
}
|
||||
: null,
|
||||
recentEntries: recentEntries
|
||||
.filter((e) => e.recipe)
|
||||
.map((e) => ({
|
||||
id: e.id,
|
||||
recipeId: e.recipe!.id,
|
||||
recipeTitle: e.recipe!.title,
|
||||
cookedAt: e.cookedAt.toISOString(),
|
||||
servings: e.servings,
|
||||
})),
|
||||
};
|
||||
|
||||
photosData = {
|
||||
items: photoRows
|
||||
.filter((r) => r.recipe && r.photoKey)
|
||||
.map((r) => ({
|
||||
id: r.id,
|
||||
recipeId: r.recipe!.id,
|
||||
recipeTitle: r.recipe!.title,
|
||||
photoKey: r.photoKey!,
|
||||
createdAt: r.createdAt.toISOString(),
|
||||
})),
|
||||
page: photoPage,
|
||||
totalPages: Math.max(1, Math.ceil(totalPhotos / PAGE_SIZE)),
|
||||
total: totalPhotos,
|
||||
};
|
||||
}
|
||||
|
||||
const initials = user.name
|
||||
.split(" ")
|
||||
.slice(0, 2)
|
||||
@@ -103,6 +210,72 @@ export default async function UserProfilePage({ params, searchParams }: Params)
|
||||
.join("")
|
||||
.toUpperCase();
|
||||
|
||||
const recipesSection =
|
||||
publicRecipes.length > 0 ? (
|
||||
<div className="space-y-4">
|
||||
<h2 className="text-xl font-semibold">Recipes</h2>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-4">
|
||||
{publicRecipes.map((recipe) => {
|
||||
const cover = recipe.photos[0];
|
||||
return (
|
||||
<Link
|
||||
key={recipe.id}
|
||||
href={`/recipes/${recipe.id}`}
|
||||
className="group block rounded-xl overflow-hidden border bg-card hover:shadow-md transition-shadow"
|
||||
>
|
||||
<div className="relative aspect-square bg-muted overflow-hidden">
|
||||
{cover ? (
|
||||
<Image
|
||||
src={getPublicUrl(cover.storageKey)}
|
||||
alt={recipe.title}
|
||||
fill
|
||||
sizes="(max-width: 640px) 50vw, (max-width: 768px) 33vw, 25vw"
|
||||
className="object-cover group-hover:scale-105 transition-transform duration-200"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-full h-full flex items-center justify-center text-muted-foreground text-3xl">
|
||||
🍽️
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="p-2">
|
||||
<p className="text-sm font-medium leading-tight line-clamp-2">{recipe.title}</p>
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{totalPages > 1 && (
|
||||
<div className="flex items-center justify-center gap-2 pt-2">
|
||||
{page > 1 && (
|
||||
<Link
|
||||
href={`/u/${username}${page - 1 > 1 ? `?page=${page - 1}` : ""}`}
|
||||
className="rounded-md border px-3 py-1.5 text-sm hover:bg-accent"
|
||||
>
|
||||
Previous
|
||||
</Link>
|
||||
)}
|
||||
<span className="text-sm text-muted-foreground px-2">
|
||||
Page {page} of {totalPages}
|
||||
</span>
|
||||
{page < totalPages && (
|
||||
<Link
|
||||
href={`/u/${username}?page=${page + 1}`}
|
||||
className="rounded-md border px-3 py-1.5 text-sm hover:bg-accent"
|
||||
>
|
||||
Next
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center py-16 text-muted-foreground">
|
||||
<p className="text-lg">No public recipes yet.</p>
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="max-w-4xl mx-auto space-y-10">
|
||||
{/* Profile header */}
|
||||
@@ -149,70 +322,17 @@ export default async function UserProfilePage({ params, searchParams }: Params)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Recipe grid */}
|
||||
{publicRecipes.length > 0 ? (
|
||||
<div className="space-y-4">
|
||||
<h2 className="text-xl font-semibold">Recipes</h2>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-4">
|
||||
{publicRecipes.map((recipe) => {
|
||||
const cover = recipe.photos[0];
|
||||
return (
|
||||
<Link
|
||||
key={recipe.id}
|
||||
href={`/recipes/${recipe.id}`}
|
||||
className="group block rounded-xl overflow-hidden border bg-card hover:shadow-md transition-shadow"
|
||||
>
|
||||
<div className="relative aspect-square bg-muted overflow-hidden">
|
||||
{cover ? (
|
||||
<Image
|
||||
src={getPublicUrl(cover.storageKey)}
|
||||
alt={recipe.title}
|
||||
fill
|
||||
sizes="(max-width: 640px) 50vw, (max-width: 768px) 33vw, 25vw"
|
||||
className="object-cover group-hover:scale-105 transition-transform duration-200"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-full h-full flex items-center justify-center text-muted-foreground text-3xl">
|
||||
🍽️
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="p-2">
|
||||
<p className="text-sm font-medium leading-tight line-clamp-2">{recipe.title}</p>
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{totalPages > 1 && (
|
||||
<div className="flex items-center justify-center gap-2 pt-2">
|
||||
{page > 1 && (
|
||||
<Link
|
||||
href={`/u/${username}${page - 1 > 1 ? `?page=${page - 1}` : ""}`}
|
||||
className="rounded-md border px-3 py-1.5 text-sm hover:bg-accent"
|
||||
>
|
||||
Previous
|
||||
</Link>
|
||||
)}
|
||||
<span className="text-sm text-muted-foreground px-2">
|
||||
Page {page} of {totalPages}
|
||||
</span>
|
||||
{page < totalPages && (
|
||||
<Link
|
||||
href={`/u/${username}?page=${page + 1}`}
|
||||
className="rounded-md border px-3 py-1.5 text-sm hover:bg-accent"
|
||||
>
|
||||
Next
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{/* Recipes / cooking history / photos */}
|
||||
{isOwnProfile && historyData && photosData ? (
|
||||
<ProfileTabs
|
||||
username={username}
|
||||
defaultTab={activeTab}
|
||||
recipesContent={recipesSection}
|
||||
history={historyData}
|
||||
photos={photosData}
|
||||
/>
|
||||
) : (
|
||||
<div className="text-center py-16 text-muted-foreground">
|
||||
<p className="text-lg">No public recipes yet.</p>
|
||||
</div>
|
||||
recipesSection
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user