feat: add nutrition/food logging to fitness section

Daily food log with calorie and macro tracking against configurable diet
goals (presets: WHO balanced, cut, bulk, keto, etc.). Includes USDA/BLS
food search with portion-based units, favorite ingredients, custom
reusable meals, per-food micronutrient detail pages, and recipe-to-log
integration via AddToFoodLogButton. Extends FitnessGoal with nutrition
targets and adds birth year to user profile for BMR calculation.
This commit is contained in:
2026-04-04 14:34:45 +02:00
parent 5d7a959355
commit 23b45abc5a
27 changed files with 4904 additions and 20 deletions
@@ -0,0 +1,482 @@
<script>
import { page } from '$app/stores';
import { Heart, ExternalLink, Search, X } from 'lucide-svelte';
import { detectFitnessLang, fitnessSlugs, t } from '$lib/js/fitnessI18n';
/**
* @type {{
* onselect: (food: { name: string, source: string, sourceId: string, amountGrams: number, per100g: any, portions?: any[], selectedPortion?: { description: string, grams: number } }) => void,
* oncancel?: () => void,
* showFavorites?: boolean,
* showDetailLinks?: boolean,
* autofocus?: boolean,
* confirmLabel?: string,
* }}
*/
let {
onselect,
oncancel = undefined,
showFavorites = true,
showDetailLinks = true,
autofocus = false,
confirmLabel = undefined,
} = $props();
const lang = $derived(detectFitnessLang($page.url.pathname));
const s = $derived(fitnessSlugs(lang));
const isEn = $derived(lang === 'en');
const btnLabel = $derived(confirmLabel ?? t('log_food', lang));
// --- Search state ---
let query = $state('');
let results = $state([]);
let loading = $state(false);
let timeout = $state(null);
// --- Selection state ---
let selected = $state(null);
let amountInput = $state('100');
let portionIdx = $state(-1); // -1 = grams
function doSearch() {
if (timeout) clearTimeout(timeout);
if (query.length < 2) {
results = [];
return;
}
loading = true;
timeout = setTimeout(async () => {
try {
const favParam = showFavorites ? '&favorites=true' : '';
const res = await fetch(`/api/nutrition/search?q=${encodeURIComponent(query)}&full=true${favParam}`);
if (res.ok) results = await res.json();
} catch {} finally {
loading = false;
}
}, 300);
}
function selectItem(item) {
selected = item;
if (item.portions?.length > 0) {
portionIdx = 0;
amountInput = '1';
} else {
portionIdx = -1;
amountInput = '100';
}
}
function resolveGrams() {
const qty = Number(amountInput) || 0;
if (portionIdx >= 0 && selected?.portions?.[portionIdx]) {
return Math.round(qty * selected.portions[portionIdx].grams);
}
return qty;
}
const previewGrams = $derived.by(() => {
const qty = Number(amountInput) || 0;
if (portionIdx >= 0 && selected?.portions?.[portionIdx]) {
return Math.round(qty * selected.portions[portionIdx].grams);
}
return qty;
});
function confirm() {
if (!selected) return;
const grams = resolveGrams();
if (!grams || grams <= 0) return;
const food = {
name: selected.name,
source: selected.source,
sourceId: selected.id,
amountGrams: grams,
per100g: selected.per100g,
};
if (selected.portions?.length > 0) {
food.portions = selected.portions;
}
if (portionIdx >= 0 && selected.portions?.[portionIdx]) {
food.selectedPortion = selected.portions[portionIdx];
}
onselect(food);
reset();
}
function reset() {
selected = null;
query = '';
results = [];
amountInput = '100';
portionIdx = -1;
}
async function toggleFavorite(item) {
const wasFav = item.favorited;
item.favorited = !wasFav;
results = [...results];
try {
if (wasFav) {
await fetch('/api/fitness/favorite-ingredients', {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ source: item.source, sourceId: item.id })
});
} else {
await fetch('/api/fitness/favorite-ingredients', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ source: item.source, sourceId: item.id, name: item.name })
});
}
} catch {
item.favorited = wasFav;
results = [...results];
}
}
function fmt(v) {
if (v >= 100) return Math.round(v).toString();
if (v >= 10) return v.toFixed(1);
return v.toFixed(1);
}
</script>
{#if !selected}
<!-- svelte-ignore a11y_autofocus -->
<input
type="search"
class="fs-search-input"
placeholder={t('search_food', lang)}
bind:value={query}
oninput={doSearch}
autofocus={autofocus}
/>
{#if loading}
<p class="fs-status">{t('loading', lang)}</p>
{/if}
{#if results.length > 0}
<div class="fs-results">
{#each results as item}
<div class="fs-result-row">
{#if showFavorites}
<button class="fs-fav" class:is-fav={item.favorited} onclick={() => toggleFavorite(item)} aria-label="Toggle favorite">
<Heart size={14} fill={item.favorited ? 'var(--nord11)' : 'none'} />
</button>
{/if}
<button class="fs-result" onclick={() => selectItem(item)}>
<div class="fs-result-info">
<span class="fs-result-name">{item.name}</span>
<span class="fs-result-meta">
<span class="fs-source-badge" class:usda={item.source === 'usda'}>{item.source === 'bls' ? 'BLS' : 'USDA'}</span>
{item.category}
</span>
</div>
<span class="fs-result-cal">{item.calories}<small> kcal</small></span>
</button>
{#if showDetailLinks}
<a class="fs-detail-link" href="/fitness/{s.nutrition}/food/{item.source}/{item.id}" aria-label="View details">
<ExternalLink size={13} />
</a>
{/if}
</div>
{/each}
</div>
{/if}
{#if oncancel}
<button class="fs-btn-cancel" onclick={oncancel}>{t('cancel', lang)}</button>
{/if}
{:else}
<!-- Selected food — amount & portion -->
<div class="fs-selected">
<div class="fs-selected-header">
<span class="fs-selected-name">
<span class="fs-source-badge" class:usda={selected.source === 'usda'}>{selected.source === 'bls' ? 'BLS' : 'USDA'}</span>
{selected.name}
</span>
</div>
<div class="fs-amount-row">
<input
type="number"
class="fs-amount-input"
bind:value={amountInput}
min="0.1"
step={portionIdx >= 0 ? '0.5' : '1'}
/>
{#if selected.portions?.length > 0}
<select class="fs-unit-select" bind:value={portionIdx} onchange={() => {
const grams = resolveGrams();
if (portionIdx >= 0 && selected.portions[portionIdx]) {
amountInput = String(Math.round((grams / selected.portions[portionIdx].grams) * 10) / 10 || 1);
} else {
amountInput = String(grams || 100);
}
}}>
<option value={-1}>g</option>
{#each selected.portions as p, pi}
<option value={pi}>{p.description} ({Math.round(p.grams)}g)</option>
{/each}
</select>
{:else}
<span class="fs-unit-label">g</span>
{/if}
</div>
{#if previewGrams > 0}
<div class="fs-preview">
{#if portionIdx >= 0}
<span class="fs-preview-grams">{previewGrams}g</span>
{/if}
<span class="fs-preview-cal">{Math.round((selected.per100g?.calories ?? 0) * previewGrams / 100)} <small>kcal</small></span>
<span class="fs-preview-p">{fmt((selected.per100g?.protein ?? 0) * previewGrams / 100)}g P</span>
<span class="fs-preview-f">{fmt((selected.per100g?.fat ?? 0) * previewGrams / 100)}g F</span>
<span class="fs-preview-c">{fmt((selected.per100g?.carbs ?? 0) * previewGrams / 100)}g C</span>
</div>
{/if}
<div class="fs-actions">
<button class="fs-btn-cancel" onclick={() => { selected = null; }}>{t('cancel', lang)}</button>
<button class="fs-btn-confirm" onclick={confirm}>{btnLabel}</button>
</div>
</div>
{/if}
<style>
/* ── Search input ── */
.fs-search-input {
width: 100%;
padding: 0.55rem 0.65rem;
background: var(--color-bg-tertiary);
border: 1px solid var(--color-border);
border-radius: 8px;
color: var(--color-text-primary);
font-size: 0.9rem;
box-sizing: border-box;
transition: border-color 0.15s;
}
.fs-search-input:focus {
outline: none;
border-color: var(--nord8);
}
.fs-status {
font-size: 0.78rem;
color: var(--color-text-tertiary);
margin: 0.4rem 0;
}
/* ── Results ── */
.fs-results {
max-height: 260px;
overflow-y: auto;
border-radius: 8px;
border: 1px solid var(--color-border);
}
.fs-result-row {
display: flex;
align-items: center;
border-bottom: 1px solid var(--color-border);
}
.fs-result-row:last-child {
border-bottom: none;
}
.fs-fav {
background: none;
border: none;
cursor: pointer;
padding: 0.4rem;
color: var(--color-text-tertiary);
flex-shrink: 0;
display: flex;
transition: color 0.15s;
}
.fs-fav.is-fav { color: var(--nord11); }
.fs-fav:hover { color: var(--nord11); }
.fs-result {
display: flex;
justify-content: space-between;
align-items: center;
flex: 1;
min-width: 0;
padding: 0.55rem 0.65rem;
background: none;
border: none;
cursor: pointer;
text-align: left;
color: var(--color-text-primary);
gap: 0.75rem;
transition: background 0.12s;
}
.fs-result:hover {
background: var(--color-bg-elevated);
}
.fs-result-info {
min-width: 0;
display: flex;
flex-direction: column;
gap: 0.1rem;
}
.fs-result-name {
font-size: 0.83rem;
font-weight: 500;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.fs-result-meta {
font-size: 0.68rem;
color: var(--color-text-tertiary);
display: flex;
align-items: center;
gap: 0.25rem;
}
.fs-source-badge {
display: inline-block;
font-size: 0.55rem;
font-weight: 700;
letter-spacing: 0.04em;
padding: 0.05rem 0.25rem;
border-radius: 3px;
background: var(--color-bg-elevated);
color: var(--color-text-tertiary);
vertical-align: middle;
}
.fs-source-badge.usda {
background: color-mix(in srgb, var(--nord10) 15%, transparent);
color: var(--nord10);
}
.fs-result-cal {
font-size: 0.85rem;
font-weight: 700;
color: var(--color-text-primary);
white-space: nowrap;
flex-shrink: 0;
font-variant-numeric: tabular-nums;
}
.fs-result-cal small {
font-size: 0.65rem;
font-weight: 500;
color: var(--color-text-tertiary);
}
.fs-detail-link {
display: flex;
padding: 0.4rem;
color: var(--color-text-tertiary);
flex-shrink: 0;
transition: color 0.15s;
}
.fs-detail-link:hover {
color: var(--nord10);
}
/* ── Selected food ── */
.fs-selected {
display: flex;
flex-direction: column;
gap: 0.6rem;
}
.fs-selected-name {
font-weight: 700;
font-size: 0.9rem;
letter-spacing: -0.01em;
display: flex;
align-items: center;
gap: 0.35rem;
}
.fs-amount-row {
display: flex;
align-items: center;
gap: 0.5rem;
}
.fs-amount-input {
width: 5rem;
padding: 0.45rem 0.55rem;
background: var(--color-bg-tertiary);
border: 1px solid var(--color-border);
border-radius: 8px;
color: var(--color-text-primary);
font-size: 0.9rem;
text-align: right;
transition: border-color 0.15s;
box-sizing: border-box;
}
.fs-amount-input:focus {
outline: none;
border-color: var(--nord8);
}
.fs-unit-select {
padding: 0.45rem 0.4rem;
background: var(--color-bg-tertiary);
border: 1px solid var(--color-border);
border-radius: 8px;
color: var(--color-text-primary);
font-size: 0.78rem;
min-width: 0;
flex: 1;
}
.fs-unit-select:focus {
outline: none;
border-color: var(--nord8);
}
.fs-unit-label {
font-size: 0.85rem;
color: var(--color-text-secondary);
}
/* ── Preview ── */
.fs-preview {
display: flex;
gap: 0.5rem;
font-size: 0.78rem;
padding: 0.5rem 0.6rem;
background: var(--color-bg-tertiary);
border-radius: 8px;
font-variant-numeric: tabular-nums;
}
.fs-preview-grams {
color: var(--color-text-tertiary);
}
.fs-preview-cal { font-weight: 700; color: var(--color-text-primary); }
.fs-preview-cal small { font-weight: 500; color: var(--color-text-secondary); }
.fs-preview-p { color: var(--nord14); font-weight: 600; }
.fs-preview-f { color: var(--nord12); font-weight: 600; }
.fs-preview-c { color: var(--nord9); font-weight: 600; }
/* ── Buttons ── */
.fs-actions {
display: flex;
gap: 0.5rem;
justify-content: flex-end;
}
.fs-btn-cancel {
padding: 0.5rem 1.1rem;
background: var(--color-bg-tertiary);
color: var(--color-text-primary);
border: 1px solid var(--color-border);
border-radius: 8px;
cursor: pointer;
font-size: 0.82rem;
font-weight: 500;
transition: background 0.15s;
}
.fs-btn-cancel:hover {
background: var(--color-bg-elevated);
}
.fs-btn-confirm {
padding: 0.5rem 1.1rem;
background: var(--nord8);
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 0.82rem;
font-weight: 700;
transition: background 0.15s, transform 0.1s;
}
.fs-btn-confirm:hover {
background: var(--nord10);
}
.fs-btn-confirm:active {
transform: scale(0.97);
}
</style>
@@ -0,0 +1,383 @@
<script>
import { UtensilsCrossed, X } from 'lucide-svelte';
import { toast } from '$lib/js/toast.svelte';
let {
recipeName,
recipeId = '',
nutritionMappings = [],
referencedNutrition = [],
ingredients = [],
portions = '',
isEnglish = true,
} = $props();
// Flatten ingredient sections into a flat array with indices
const flatIngredients = $derived.by(() => {
const flat = [];
for (let si = 0; si < ingredients.length; si++) {
const section = ingredients[si];
const items = section?.list ?? section?.ingredients ?? section?.items ?? [];
for (let ii = 0; ii < items.length; ii++) {
const ing = items[ii];
flat.push({ name: ing.name, unit: ing.unit, amount: ing.amount, sectionIndex: si, ingredientIndex: ii });
}
}
return flat;
});
let showDialog = $state(false);
let portionAmount = $state('1');
let mealType = $state('lunch');
let saving = $state(false);
let customGrams = $state('');
let useGrams = $state(false);
const labels = $derived({
addToLog: isEnglish ? 'Add to food log' : 'Zum Ernährungstagebuch',
portions: isEnglish ? 'Portions' : 'Portionen',
grams: isEnglish ? 'Grams' : 'Gramm',
meal: isEnglish ? 'Meal' : 'Mahlzeit',
breakfast: isEnglish ? 'Breakfast' : 'Frühstück',
lunch: isEnglish ? 'Lunch' : 'Mittagessen',
dinner: isEnglish ? 'Dinner' : 'Abendessen',
snack: isEnglish ? 'Snack' : 'Snack',
log: isEnglish ? 'Log' : 'Eintragen',
cancel: isEnglish ? 'Cancel' : 'Abbrechen',
});
// Parse portion count from recipe's portions string (e.g. "4 Portionen")
const basePortionCount = $derived.by(() => {
if (!portions) return 0;
const match = portions.match(/^(\d+(?:[.,]\d+)?)/);
return match ? parseFloat(match[1].replace(',', '.')) : 0;
});
// Parse amount string to number (simplified from nutrition.svelte.ts)
function parseAmount(amount) {
if (!amount?.trim()) return 0;
let s = amount.trim().replace(',', '.');
const rangeMatch = s.match(/^(\d+(?:\.\d+)?)\s*[-]\s*(\d+(?:\.\d+)?)$/);
if (rangeMatch) return (parseFloat(rangeMatch[1]) + parseFloat(rangeMatch[2])) / 2;
const fractionMatch = s.match(/^(\d+)\s*\/\s*(\d+)$/);
if (fractionMatch) return parseInt(fractionMatch[1]) / parseInt(fractionMatch[2]);
const mixedMatch = s.match(/^(\d+)\s+(\d+)\s*\/\s*(\d+)$/);
if (mixedMatch) return parseInt(mixedMatch[1]) + parseInt(mixedMatch[2]) / parseInt(mixedMatch[3]);
const parsed = parseFloat(s);
return isNaN(parsed) ? 0 : parsed;
}
// Compute total recipe nutrition (all ingredients at multiplier=1)
const recipeTotals = $derived.by(() => {
const result = {};
const nutrientKeys = [
'calories', 'protein', 'fat', 'saturatedFat', 'carbs', 'fiber', 'sugars',
'calcium', 'iron', 'magnesium', 'phosphorus', 'potassium', 'sodium', 'zinc',
'vitaminA', 'vitaminC', 'vitaminD', 'vitaminE', 'vitaminK',
'thiamin', 'riboflavin', 'niacin', 'vitaminB6', 'vitaminB12', 'folate', 'cholesterol',
'isoleucine', 'leucine', 'lysine', 'methionine', 'phenylalanine',
'threonine', 'tryptophan', 'valine', 'histidine', 'alanine',
'arginine', 'asparticAcid', 'cysteine', 'glutamicAcid', 'glycine',
'proline', 'serine', 'tyrosine',
];
for (const k of nutrientKeys) result[k] = 0;
let totalWeightGrams = 0;
// Build mapping index
const mappingIndex = new Map();
for (const m of nutritionMappings) {
mappingIndex.set(`${m.sectionIndex}-${m.ingredientIndex}`, m);
}
for (const ing of flatIngredients) {
const mapping = mappingIndex.get(`${ing.sectionIndex}-${ing.ingredientIndex}`);
if (!mapping || mapping.matchMethod === 'none' || mapping.excluded || !mapping.per100g) continue;
if (!mapping.gramsPerUnit) continue;
const parsedAmount = parseAmount(ing.amount) || (mapping.defaultAmountUsed ? 1 : 0);
const grams = parsedAmount * mapping.gramsPerUnit;
totalWeightGrams += grams;
const factor = grams / 100;
for (const k of nutrientKeys) {
result[k] += factor * (mapping.per100g[k] ?? 0);
}
}
// Add referenced recipe nutrition
for (const ref of referencedNutrition) {
const refMult = ref.baseMultiplier ?? 1;
for (const k of nutrientKeys) {
result[k] += (ref.nutrition?.[k] ?? 0) * refMult;
}
}
return { totals: result, totalWeightGrams };
});
// Per-100g for the entire recipe
const per100g = $derived.by(() => {
const w = recipeTotals.totalWeightGrams;
if (w <= 0) return recipeTotals.totals;
const result = {};
for (const [k, v] of Object.entries(recipeTotals.totals)) {
result[k] = v / w * 100;
}
return result;
});
// Grams for the selected portion
const portionGrams = $derived.by(() => {
if (useGrams) return Number(customGrams) || 0;
const numPortions = Number(portionAmount) || 0;
if (basePortionCount <= 0 || numPortions <= 0) return 0;
return (recipeTotals.totalWeightGrams / basePortionCount) * numPortions;
});
// Preview calories
const previewCal = $derived(portionGrams > 0 ? (per100g.calories ?? 0) * portionGrams / 100 : 0);
function openDialog() {
portionAmount = '1';
mealType = 'lunch';
customGrams = '';
useGrams = false;
showDialog = true;
}
async function submit() {
const grams = portionGrams;
if (grams <= 0) return;
saving = true;
try {
const today = new Date().toISOString().slice(0, 10);
const res = await fetch('/api/fitness/food-log', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
date: today,
mealType,
name: recipeName,
source: 'recipe',
sourceId: recipeId,
amountGrams: Math.round(grams),
per100g,
})
});
if (res.ok) {
toast.success(isEnglish ? 'Added to food log' : 'Zum Ernährungstagebuch hinzugefügt');
showDialog = false;
} else {
toast.error(isEnglish ? 'Failed to add' : 'Fehler beim Hinzufügen');
}
} catch {
toast.error(isEnglish ? 'Failed to add' : 'Fehler beim Hinzufügen');
} finally {
saving = false;
}
}
</script>
<button class="add-to-log-btn" onclick={openDialog} title={labels.addToLog}>
<UtensilsCrossed size={14} />
<span>{labels.addToLog}</span>
</button>
{#if showDialog}
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div class="dialog-overlay" onclick={() => showDialog = false} onkeydown={(e) => e.key === 'Escape' && (showDialog = false)}>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div class="dialog" onclick={(e) => e.stopPropagation()}>
<div class="dialog-header">
<h3>{labels.addToLog}</h3>
<button class="close-btn" onclick={() => showDialog = false}><X size={18} /></button>
</div>
<div class="dialog-body">
<div class="field">
<label for="log-meal">{labels.meal}</label>
<select id="log-meal" bind:value={mealType}>
<option value="breakfast">{labels.breakfast}</option>
<option value="lunch">{labels.lunch}</option>
<option value="dinner">{labels.dinner}</option>
<option value="snack">{labels.snack}</option>
</select>
</div>
<div class="amount-toggle">
<button class:active={!useGrams} onclick={() => useGrams = false}>{labels.portions}</button>
<button class:active={useGrams} onclick={() => useGrams = true}>{labels.grams}</button>
</div>
{#if useGrams}
<div class="field">
<label for="log-grams">{labels.grams}</label>
<input id="log-grams" type="number" bind:value={customGrams} min="1" />
</div>
{:else}
<div class="field">
<label for="log-portions">{labels.portions}</label>
<input id="log-portions" type="number" bind:value={portionAmount} min="0.25" step="0.25" />
{#if basePortionCount > 0}
<span class="portion-hint">{Math.round(portionGrams)}g</span>
{/if}
</div>
{/if}
{#if previewCal > 0}
<div class="preview">
{Math.round(previewCal)} kcal
</div>
{/if}
</div>
<div class="dialog-actions">
<button class="btn-cancel" onclick={() => showDialog = false}>{labels.cancel}</button>
<button class="btn-primary" onclick={submit} disabled={saving || portionGrams <= 0}>
{saving ? '…' : labels.log}
</button>
</div>
</div>
</div>
{/if}
<style>
.add-to-log-btn {
background: none;
border: none;
cursor: pointer;
color: var(--color-primary, var(--nord10));
padding: 0;
display: inline-flex;
align-items: center;
gap: 0.3rem;
font-size: 0.85rem;
text-decoration: underline;
text-decoration-style: dotted;
}
.add-to-log-btn:hover {
color: var(--nord15);
}
.dialog-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
.dialog {
background: var(--color-surface);
border-radius: 0.75rem;
width: 90%;
max-width: 360px;
overflow: hidden;
}
.dialog-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.75rem 1rem;
border-bottom: 1px solid var(--color-border);
}
.dialog-header h3 {
margin: 0;
font-size: 1rem;
}
.close-btn {
background: none;
border: none;
color: var(--color-text-secondary);
cursor: pointer;
padding: 0.25rem;
}
.dialog-body {
padding: 1rem;
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.field {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.field label {
font-size: 0.8rem;
color: var(--color-text-secondary);
}
.field input,
.field select {
padding: 0.5rem;
background: var(--color-bg-tertiary);
border: 1px solid var(--color-border);
border-radius: 0.4rem;
color: var(--color-text-primary);
font-size: 0.9rem;
}
.amount-toggle {
display: flex;
border: 1px solid var(--color-border);
border-radius: 0.4rem;
overflow: hidden;
}
.amount-toggle button {
flex: 1;
padding: 0.4rem;
background: var(--color-bg-tertiary);
border: none;
color: var(--color-text-secondary);
cursor: pointer;
font-size: 0.85rem;
}
.amount-toggle button.active {
background: var(--nord8);
color: white;
}
.portion-hint {
font-size: 0.8rem;
color: var(--color-text-secondary);
}
.preview {
text-align: center;
font-size: 1.1rem;
font-weight: 600;
color: var(--color-text-primary);
}
.dialog-actions {
display: flex;
gap: 0.5rem;
justify-content: flex-end;
padding: 0.75rem 1rem;
border-top: 1px solid var(--color-border);
}
.btn-primary {
padding: 0.45rem 1rem;
background: var(--nord8);
color: white;
border: none;
border-radius: 0.4rem;
cursor: pointer;
font-weight: 600;
}
.btn-primary:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn-cancel {
padding: 0.45rem 1rem;
background: var(--color-bg-tertiary);
border: 1px solid var(--color-border);
border-radius: 0.4rem;
cursor: pointer;
color: var(--color-text-primary);
}
</style>
@@ -5,7 +5,10 @@ import { browser } from '$app/environment';
import { page } from '$app/stores';
import HefeSwapper from './HefeSwapper.svelte';
import NutritionSummary from './NutritionSummary.svelte';
import AddToFoodLogButton from './AddToFoodLogButton.svelte';
let { data } = $props();
const isLoggedIn = $derived(!!data.session?.user);
const hasNutrition = $derived(!!data.nutritionMappings?.length);
// Helper function to multiply numbers in ingredient amounts
/** @param {string} amount @param {number} multiplier */
@@ -635,6 +638,20 @@ const nutritionFlatIngredients = $derived.by(() => {
{multiplier}
portions={data.portions}
isEnglish={isEnglish}
/>
>
{#snippet actions()}
{#if isLoggedIn && hasNutrition}
<AddToFoodLogButton
recipeName={data.strippedName || data.name}
recipeId={data._id}
nutritionMappings={data.nutritionMappings}
referencedNutrition={data.referencedNutrition || []}
ingredients={data.ingredients || []}
portions={data.portions || ''}
{isEnglish}
/>
{/if}
{/snippet}
</NutritionSummary>
</div>
{/if}
@@ -1,7 +1,7 @@
<script>
import { createNutritionCalculator } from '$lib/js/nutrition.svelte';
let { flatIngredients, nutritionMappings, sectionNames, referencedNutrition, multiplier, portions, isEnglish } = $props();
let { flatIngredients, nutritionMappings, sectionNames, referencedNutrition, multiplier, portions, isEnglish, actions } = $props();
const nutrition = createNutritionCalculator(
() => flatIngredients,
@@ -138,7 +138,10 @@
}
.details-toggle-row {
text-align: center;
display: flex;
align-items: center;
justify-content: center;
gap: 0.75rem;
margin-top: 0.5rem;
}
.details-toggle {
@@ -290,6 +293,9 @@
<button class="details-toggle" onclick={() => showDetails = !showDetails}>
{showDetails ? '' : '+'} {labels.details}
</button>
{#if actions}
{@render actions()}
{/if}
</div>
</div>
{/if}
+125
View File
@@ -0,0 +1,125 @@
/**
* Daily Reference Intake (DRI) values for micronutrients.
* Sources: WHO, EFSA, US DRI (IOM).
* Values are daily recommended amounts for adults.
* Units match NutritionPer100g: vitamins in mcg/mg as stored, minerals in mg.
*/
export type DRI = {
// Macros (g)
protein: number;
fat: number;
carbs: number;
fiber: number;
sugars: number; // max recommended
// Minerals (mg)
calcium: number;
iron: number;
magnesium: number;
phosphorus: number;
potassium: number;
sodium: number; // max recommended
zinc: number;
// Vitamins
vitaminA: number; // mcg RAE
vitaminC: number; // mg
vitaminD: number; // mcg
vitaminE: number; // mg
vitaminK: number; // mcg
thiamin: number; // mg
riboflavin: number; // mg
niacin: number; // mg
vitaminB6: number; // mg
vitaminB12: number; // mcg
folate: number; // mcg
cholesterol: number; // mg max
};
/** DRI for adult males (19-50) */
export const DRI_MALE: DRI = {
protein: 56,
fat: 78, // ~35% of 2000 kcal
carbs: 300,
fiber: 38,
sugars: 50, // WHO max free sugars
calcium: 1000,
iron: 8,
magnesium: 420,
phosphorus: 700,
potassium: 3400,
sodium: 2300,
zinc: 11,
vitaminA: 900,
vitaminC: 90,
vitaminD: 15,
vitaminE: 15,
vitaminK: 120,
thiamin: 1.2,
riboflavin: 1.3,
niacin: 16,
vitaminB6: 1.3,
vitaminB12: 2.4,
folate: 400,
cholesterol: 300,
};
/** DRI for adult females (19-50) */
export const DRI_FEMALE: DRI = {
protein: 46,
fat: 62, // ~35% of 1600 kcal
carbs: 250,
fiber: 25,
sugars: 50,
calcium: 1000,
iron: 18,
magnesium: 320,
phosphorus: 700,
potassium: 2600,
sodium: 2300,
zinc: 8,
vitaminA: 700,
vitaminC: 75,
vitaminD: 15,
vitaminE: 15,
vitaminK: 90,
thiamin: 1.1,
riboflavin: 1.1,
niacin: 14,
vitaminB6: 1.3,
vitaminB12: 2.4,
folate: 400,
cholesterol: 300,
};
/** Get DRI values by sex */
export function getDRI(sex: 'male' | 'female'): DRI {
return sex === 'female' ? DRI_FEMALE : DRI_MALE;
}
/** Nutrient display metadata: label, unit, and whether it's a max (upper limit) */
export const NUTRIENT_META: Record<keyof DRI, { label: string; labelDe: string; unit: string; isMax?: boolean }> = {
protein: { label: 'Protein', labelDe: 'Eiweiß', unit: 'g' },
fat: { label: 'Fat', labelDe: 'Fett', unit: 'g' },
carbs: { label: 'Carbs', labelDe: 'Kohlenhydrate', unit: 'g' },
fiber: { label: 'Fiber', labelDe: 'Ballaststoffe', unit: 'g' },
sugars: { label: 'Sugars', labelDe: 'Zucker', unit: 'g', isMax: true },
calcium: { label: 'Calcium', labelDe: 'Kalzium', unit: 'mg' },
iron: { label: 'Iron', labelDe: 'Eisen', unit: 'mg' },
magnesium: { label: 'Magnesium', labelDe: 'Magnesium', unit: 'mg' },
phosphorus: { label: 'Phosphorus', labelDe: 'Phosphor', unit: 'mg' },
potassium: { label: 'Potassium', labelDe: 'Kalium', unit: 'mg' },
sodium: { label: 'Sodium', labelDe: 'Natrium', unit: 'mg', isMax: true },
zinc: { label: 'Zinc', labelDe: 'Zink', unit: 'mg' },
vitaminA: { label: 'Vitamin A', labelDe: 'Vitamin A', unit: 'mcg' },
vitaminC: { label: 'Vitamin C', labelDe: 'Vitamin C', unit: 'mg' },
vitaminD: { label: 'Vitamin D', labelDe: 'Vitamin D', unit: 'mcg' },
vitaminE: { label: 'Vitamin E', labelDe: 'Vitamin E', unit: 'mg' },
vitaminK: { label: 'Vitamin K', labelDe: 'Vitamin K', unit: 'mcg' },
thiamin: { label: 'Thiamin (B1)', labelDe: 'Thiamin (B1)', unit: 'mg' },
riboflavin: { label: 'Riboflavin (B2)', labelDe: 'Riboflavin (B2)', unit: 'mg' },
niacin: { label: 'Niacin (B3)', labelDe: 'Niacin (B3)', unit: 'mg' },
vitaminB6: { label: 'Vitamin B6', labelDe: 'Vitamin B6', unit: 'mg' },
vitaminB12: { label: 'Vitamin B12', labelDe: 'Vitamin B12', unit: 'mcg' },
folate: { label: 'Folate', labelDe: 'Folsäure', unit: 'mcg' },
cholesterol: { label: 'Cholesterol', labelDe: 'Cholesterin', unit: 'mg', isMax: true },
};
+68 -4
View File
@@ -1,8 +1,8 @@
/** Fitness route i18n — slug mappings and UI translations */
const slugMap: Record<string, Record<string, string>> = {
en: { statistik: 'stats', verlauf: 'history', training: 'workout', aktiv: 'active', uebungen: 'exercises', messen: 'measure' },
de: { stats: 'statistik', history: 'verlauf', workout: 'training', active: 'aktiv', exercises: 'uebungen', measure: 'messen' }
en: { statistik: 'stats', verlauf: 'history', training: 'workout', aktiv: 'active', uebungen: 'exercises', messen: 'measure', ernaehrung: 'nutrition' },
de: { stats: 'statistik', history: 'verlauf', workout: 'training', active: 'aktiv', exercises: 'uebungen', measure: 'messen', nutrition: 'ernaehrung' }
};
const germanSlugs = new Set(Object.keys(slugMap.en));
@@ -31,7 +31,8 @@ export function fitnessSlugs(lang: 'en' | 'de') {
workout: lang === 'en' ? 'workout' : 'training',
active: lang === 'en' ? 'active' : 'aktiv',
exercises: lang === 'en' ? 'exercises' : 'uebungen',
measure: lang === 'en' ? 'measure' : 'messen'
measure: lang === 'en' ? 'measure' : 'messen',
nutrition: lang === 'en' ? 'nutrition' : 'ernaehrung'
};
}
@@ -42,7 +43,8 @@ export function fitnessLabels(lang: 'en' | 'de') {
history: lang === 'en' ? 'History' : 'Verlauf',
workout: lang === 'en' ? 'Workout' : 'Training',
exercises: lang === 'en' ? 'Exercises' : 'Übungen',
measure: lang === 'en' ? 'Measure' : 'Messen'
measure: lang === 'en' ? 'Measure' : 'Messen',
nutrition: lang === 'en' ? 'Nutrition' : 'Ernährung'
};
}
@@ -80,6 +82,7 @@ const translations: Translations = {
male: { en: 'Male', de: 'Männlich' },
female: { en: 'Female', de: 'Weiblich' },
height: { en: 'Height (cm)', de: 'Grösse (cm)' },
birth_year: { en: 'Birth Year', de: 'Geburtsjahr' },
no_workout_data: { en: 'No workout data to display yet.', de: 'Noch keine Trainingsdaten vorhanden.' },
weight: { en: 'Weight', de: 'Gewicht' },
@@ -263,6 +266,67 @@ const translations: Translations = {
label_tempo: { en: 'Tempo', de: 'Tempo' },
label_warm_up: { en: 'Warm Up', de: 'Aufwärmen' },
label_cool_down: { en: 'Cool Down', de: 'Abkühlen' },
// Nutrition / Food log
nutrition_title: { en: 'Nutrition', de: 'Ernährung' },
breakfast: { en: 'Breakfast', de: 'Frühstück' },
lunch: { en: 'Lunch', de: 'Mittagessen' },
dinner: { en: 'Dinner', de: 'Abendessen' },
snack: { en: 'Snack', de: 'Snack' },
add_food: { en: 'Add food', de: 'Essen hinzufügen' },
search_food: { en: 'Search food…', de: 'Essen suchen…' },
amount_grams: { en: 'Amount (g)', de: 'Menge (g)' },
meal_type: { en: 'Meal', de: 'Mahlzeit' },
daily_goal: { en: 'Daily Goal', de: 'Tagesziel' },
calorie_target: { en: 'Calorie target (kcal)', de: 'Kalorienziel (kcal)' },
protein_goal: { en: 'Protein goal', de: 'Proteinziel' },
protein_fixed: { en: 'Fixed (g/day)', de: 'Fest (g/Tag)' },
protein_per_kg: { en: 'Per kg bodyweight', de: 'Pro kg Körpergewicht' },
fat_percent: { en: 'Fat (%)', de: 'Fett (%)' },
carb_percent: { en: 'Carbs (%)', de: 'Kohlenhydrate (%)' },
kcal: { en: 'kcal', de: 'kcal' },
protein: { en: 'Protein', de: 'Protein' },
fat: { en: 'Fat', de: 'Fett' },
carbs: { en: 'Carbs', de: 'Kohlenhydrate' },
remaining: { en: 'remaining', de: 'übrig' },
over: { en: 'over', de: 'über' },
no_entries_yet: { en: 'No entries yet. Add food to start tracking.', de: 'Noch keine Einträge. Füge Essen hinzu, um zu tracken.' },
set_goal_prompt: { en: 'Set a daily calorie goal to start tracking.', de: 'Setze ein Kalorienziel, um mit dem Tracking zu beginnen.' },
micro_details: { en: 'Micronutrients', de: 'Mikronährstoffe' },
of_daily: { en: 'of daily goal', de: 'vom Tagesziel' },
per_serving: { en: 'per serving', de: 'pro Portion' },
log_food: { en: 'Log', de: 'Eintragen' },
delete_entry_confirm: { en: 'Delete this food entry?', de: 'Diesen Eintrag löschen?' },
// Custom meals
custom_meals: { en: 'Custom Meals', de: 'Eigene Mahlzeiten' },
custom_meal: { en: 'Custom Meal', de: 'Eigene Mahlzeit' },
new_meal: { en: 'New Meal', de: 'Neue Mahlzeit' },
meal_name: { en: 'Meal name', de: 'Name der Mahlzeit' },
add_ingredient: { en: 'Add ingredient', de: 'Zutat hinzufügen' },
no_custom_meals: { en: 'No custom meals yet.', de: 'Noch keine eigenen Mahlzeiten.' },
create_meal_hint: { en: 'Create reusable meals for quick logging.', de: 'Erstelle wiederverwendbare Mahlzeiten zum schnellen Eintragen.' },
ingredients: { en: 'Ingredients', de: 'Zutaten' },
total: { en: 'Total', de: 'Gesamt' },
log_meal: { en: 'Log Meal', de: 'Mahlzeit eintragen' },
delete_meal_confirm: { en: 'Delete this custom meal?', de: 'Diese Mahlzeit löschen?' },
save_meal: { en: 'Save Meal', de: 'Mahlzeit speichern' },
// Favorites
favorites: { en: 'Favorites', de: 'Favoriten' },
// Ingredient detail
per_100g: { en: 'per 100 g', de: 'pro 100 g' },
macros: { en: 'Macronutrients', de: 'Makronährstoffe' },
minerals: { en: 'Minerals', de: 'Mineralstoffe' },
vitamins: { en: 'Vitamins', de: 'Vitamine' },
amino_acids: { en: 'Amino Acids', de: 'Aminosäuren' },
essential: { en: 'Essential', de: 'Essenziell' },
non_essential: { en: 'Non-Essential', de: 'Nicht-essenziell' },
saturated_fat: { en: 'Saturated Fat', de: 'Gesättigte Fettsäuren' },
fiber: { en: 'Fiber', de: 'Ballaststoffe' },
sugars: { en: 'Sugars', de: 'Zucker' },
source_db: { en: 'Source', de: 'Quelle' },
};
/** Get a translated string */