Files
Epicure/apps/web/lib/for-you-ranking.ts
Arnaud e5d1080fb9 feat: implement remaining TODO.md feature ideas + fix mobile headers
Implements the six previously-unscoped feature ideas plus a mobile
layout fix reported via screenshot:

- Mobile: Recipes/Collections/Pantry/Meal Plan/Shopping Lists headers
  now stack and wrap instead of clipping buttons on narrow viewports.
- Recipe diff/compare view: word/list diff against any past version,
  next to Restore in version history.
- Shared meal plans & shopping lists: new shoppingListMembers/
  mealPlanMembers tables (viewer/editor roles, mirrors
  collectionMembers), share dialogs, membership-checked routes.
- PDF cookbook export: /print/collection/[id] renders a whole
  collection with page breaks, using the existing print-CSS pattern
  instead of adding a PDF rendering dependency.
- Grocery delivery handoff: shopping lists can copy-as-text (works
  today) or send to Instacart once INSTACART_API_KEY is configured
  (stub adapter — real API needs a partner agreement).
- Personalized "For You" feed tab: ranks public recipes by tag/
  dietary overlap with the user's favorited/highly-rated history.
- PWA: added manifest.json + icons on top of the existing service
  worker so the app is installable; cook-mode pages were already
  cached for offline use.

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

44 lines
1.7 KiB
TypeScript

export type TaggedRecipe = {
id: string;
tags: string[];
dietaryTags: Record<string, boolean> | null;
createdAt: Date;
};
/** Collapses a recipe's tags + true dietary-tag keys into one flat tag list. */
function tagSet(recipe: Pick<TaggedRecipe, "tags" | "dietaryTags">): string[] {
const dietary = Object.entries(recipe.dietaryTags ?? {})
.filter(([, v]) => v)
.map(([k]) => k);
return [...recipe.tags, ...dietary];
}
/** Builds a tag → frequency map from the recipes a user has favorited/highly rated. */
export function buildPreferenceMap(likedRecipes: Array<Pick<TaggedRecipe, "tags" | "dietaryTags">>): Map<string, number> {
const map = new Map<string, number>();
for (const recipe of likedRecipes) {
for (const tag of tagSet(recipe)) {
map.set(tag, (map.get(tag) ?? 0) + 1);
}
}
return map;
}
/**
* Scores a candidate recipe by how many of its tags overlap with the user's
* preference map (weighted by how often that tag shows up in their history).
* Recency is used only as a tiebreaker (see rankForYou) — an empty preference
* map scores everything 0, which the caller should treat as "fall back to
* trending" rather than a meaningful ranking.
*/
export function scoreCandidate(candidate: Pick<TaggedRecipe, "tags" | "dietaryTags">, preferences: Map<string, number>): number {
return tagSet(candidate).reduce((sum, tag) => sum + (preferences.get(tag) ?? 0), 0);
}
export function rankForYou<T extends TaggedRecipe>(candidates: T[], preferences: Map<string, number>): T[] {
return [...candidates]
.map((c) => ({ c, score: scoreCandidate(c, preferences) }))
.sort((a, b) => b.score - a.score || b.c.createdAt.getTime() - a.c.createdAt.getTime())
.map(({ c }) => c);
}