Redesign pantry horizon controls

This commit is contained in:
2026-04-14 23:14:28 +02:00
parent 71b91b50b4
commit d3a68a80eb
3 changed files with 495 additions and 100 deletions

View File

@@ -1,7 +1,7 @@
import { INGREDIENTS, RECIPES, PRODUCTS, getProductsForIngredient } from '../data/catalog.js?v=8';
import { MEAL_SLOTS } from '../planner/mealSlots.js';
import { addDays } from './dateUtils.js';
import { getDayPlan } from './planStore.js?v=2';
import { dateKey, getDayPlan } from './planStore.js?v=2';
import { getPantryTotal } from './pantryShopping.js?v=2';
export function dayHasAnyMeal(plans, d) {
@@ -182,6 +182,42 @@ export function aggregateWeekIngredientNeed(plans, weekStart) {
return mergeIngredientLines(all);
}
/**
* Zapotrzebowanie składników od startDate przez numDays dni.
* Jak aggregateWeekIngredientNeed, ale z dowolnym zakresem i informacją
* w które dni dany składnik jest potrzebny.
* @param {Record<string, unknown>} plans
* @param {Date} startDate
* @param {number} numDays
* @returns {Array<{ingredientId: string, name: string, category: string, amount: number, unit: string, days: string[]}>}
*/
export function aggregateRangeIngredientNeed(plans, startDate, numDays) {
/** @type {Map<string, {ingredientId: string, name: string, category: string, amount: number, unit: string, days: Set<string>}>} */
const map = new Map();
for (let i = 0; i < numDays; i++) {
const day = addDays(startDate, i);
const dk = dateKey(day);
const dayPlan = getDayPlan(plans, day);
const lines = flattenDayIngredientLines(dayPlan);
for (const line of lines) {
const key = `${line.ingredientId}\t${line.unit}`;
const cur = map.get(key);
if (!cur) {
map.set(key, { ...line, days: new Set([dk]) });
} else {
cur.amount = Math.round((cur.amount + line.amount) * 10) / 10;
cur.days.add(dk);
}
}
}
return [...map.values()]
.map((item) => ({ ...item, days: [...item.days].sort() }))
.sort((a, b) => {
const c = a.category.localeCompare(b.category);
return c !== 0 ? c : a.name.localeCompare(b.name, 'pl');
});
}
/**
* Jedna grupa na porę dnia: nagłówek pory raz, potem bloki przepisów ze składnikami.
*/