93936eae10
Pantry: notes + category fields (collapsible grouping like the shopping list), a "Merge duplicates" cleanup action, and fixed quantity display precision (was showing raw decimal(10,4) strings like "0.3333 kg" everywhere — pantry, shopping list, print views, Markdown exports). Ingredient-alias matching: the ingredients table (canonical name + aliases) existed but was never populated or used. Seeded ~10 bilingual EN/FR staples and wired resolution into pantry add/edit, can-cook scoring, auto-deduct-on-cook, and shopping-list pantry-awareness, so "sel"/"sel fin"/"table salt" are recognized as the same ingredient. Cook log: entries from "Mark cooked" can now be edited and deleted (previously log-only, no fix-a-mistake path). The "Cooked N times" text is a hover tooltip listing every date and opens a full manage sheet on click. Also: the "Forked by N others" backlink is now a click-to-open popover instead of an always-inline list. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { resolveIngredientKey, type IngredientAliasIndex } from "./ingredient-match";
|
|
|
|
export const EXPIRING_WITHIN_DAYS = 3;
|
|
|
|
export function isExpiringSoon(expiresAt: Date | null): boolean {
|
|
if (!expiresAt) return false;
|
|
const days = Math.ceil((expiresAt.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
|
|
return days >= 0 && days <= EXPIRING_WITHIN_DAYS;
|
|
}
|
|
|
|
type ScorableRecipe<T> = T & { ingredients: { rawName: string }[] };
|
|
|
|
export function scoreRecipesAgainstPantry<T>(
|
|
recipesList: ScorableRecipe<T>[],
|
|
pantry: { rawName: string; expiresAt: Date | null }[],
|
|
aliasIndex?: IngredientAliasIndex
|
|
) {
|
|
// With no alias index, this resolves to a plain lowercase compare —
|
|
// same behavior as before aliases existed.
|
|
const keyOf = (name: string) => (aliasIndex ? resolveIngredientKey(name, aliasIndex) : name.trim().toLowerCase());
|
|
|
|
const pantryKeys = new Set(pantry.map((p) => keyOf(p.rawName)));
|
|
const expiringSoonKeys = new Set(
|
|
pantry.filter((p) => isExpiringSoon(p.expiresAt)).map((p) => keyOf(p.rawName))
|
|
);
|
|
|
|
return recipesList
|
|
.filter((r) => r.ingredients.length > 0)
|
|
.map((recipe) => {
|
|
const matched = recipe.ingredients.filter((ing) => pantryKeys.has(keyOf(ing.rawName))).length;
|
|
const missing = recipe.ingredients
|
|
.filter((ing) => !pantryKeys.has(keyOf(ing.rawName)))
|
|
.map((ing) => ing.rawName)
|
|
.slice(0, 5);
|
|
const usesExpiring = recipe.ingredients
|
|
.filter((ing) => expiringSoonKeys.has(keyOf(ing.rawName)))
|
|
.map((ing) => ing.rawName);
|
|
const total = recipe.ingredients.length;
|
|
return { recipe, matched, total, pct: Math.round((matched / total) * 100), missing, usesExpiring };
|
|
})
|
|
.sort((a, b) => {
|
|
if (a.usesExpiring.length > 0 !== b.usesExpiring.length > 0) {
|
|
return a.usesExpiring.length > 0 ? -1 : 1;
|
|
}
|
|
return b.pct - a.pct;
|
|
});
|
|
}
|