feat: Zutaten im Listendialog dem BLS zuordnen
Mapping-Status pro Zutat, Suche zuerst Listen und eigene Foods. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
b37005be01
commit
a5adff6196
10 files changed
+237
-23
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import FoodSearchModal from './FoodSearchModal'
|
||||
|
||||
const FALLBACK_UNITS = [
|
||||
{ id: 'g', label: 'g', default_g: 1, needs_unit_map: false },
|
||||
@@ -73,10 +74,11 @@ function fromSavedIngredient(i, mapped) {
|
||||
function mappingFor(name, learned) {
|
||||
const n = (name || '').trim().toLowerCase()
|
||||
if (!n) return null
|
||||
const stripped = n.replace(/^\d+(?:[.,]\d+)?\s*(?:g|kg|ml|l|stück|stk)?\s*/i, '').trim()
|
||||
return (learned || []).find((m) => {
|
||||
const raw = (m.source_name_raw || '').toLowerCase()
|
||||
const norm = (m.source_name_normalized || '').toLowerCase()
|
||||
return raw === n || norm === n
|
||||
return raw === n || norm === n || raw === stripped || norm === stripped
|
||||
}) || null
|
||||
}
|
||||
|
||||
@@ -89,6 +91,15 @@ function filterLearned(term, learned) {
|
||||
}).slice(0, 8)
|
||||
}
|
||||
|
||||
function filterKnownIngredients(term, names) {
|
||||
const t = (term || '').trim().toLowerCase()
|
||||
if (t.length < 2) return []
|
||||
return (names || []).filter((n) => {
|
||||
const hay = `${n.source_name_raw || ''} ${n.source_name_normalized || ''} ${n.food_name_de || ''}`.toLowerCase()
|
||||
return hay.includes(t)
|
||||
}).slice(0, 8)
|
||||
}
|
||||
|
||||
export default function FoodRecipeDialog({
|
||||
title,
|
||||
defaultName,
|
||||
@@ -114,13 +125,48 @@ export default function FoodRecipeDialog({
|
||||
const [error, setError] = useState(null)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [localLearned, setLocalLearned] = useState(learned || [])
|
||||
const [knownNames, setKnownNames] = useState([])
|
||||
const [suggestions, setSuggestions] = useState({})
|
||||
const [mapFor, setMapFor] = useState(null)
|
||||
const nameRef = useRef(null)
|
||||
const qtyRefs = useRef({})
|
||||
const timer = useRef(null)
|
||||
const abortRef = useRef(null)
|
||||
const seqRef = useRef(0)
|
||||
|
||||
useEffect(() => { setLocalLearned(learned || []) }, [learned])
|
||||
useEffect(() => {
|
||||
setLocalLearned(learned || [])
|
||||
setIngs((list) => list.map((ing) => {
|
||||
const m = mappingFor(ing.source_name_raw, learned)
|
||||
if (!m) return ing
|
||||
return {
|
||||
...ing,
|
||||
foodId: ing.foodId || m.food_id,
|
||||
mappedLabel: ing.mappedLabel || m.food_name_de,
|
||||
gramsPerUnit: ing.gramsPerUnit || m.grams_per_unit || '',
|
||||
}
|
||||
}))
|
||||
}, [learned])
|
||||
|
||||
useEffect(() => {
|
||||
api.listNutritionIngredientNames().then((rows) => {
|
||||
setKnownNames(Array.isArray(rows) ? rows : [])
|
||||
}).catch(() => {})
|
||||
}, [])
|
||||
|
||||
const unmappedKey = ings
|
||||
.filter((i) => i.source_name_raw && !mappingFor(i.source_name_raw, localLearned) && !i.foodId)
|
||||
.map((i) => i.source_name_raw)
|
||||
.join('\n')
|
||||
|
||||
useEffect(() => {
|
||||
const names = unmappedKey ? unmappedKey.split('\n') : []
|
||||
if (!names.length) return
|
||||
api.suggestFoodsBatch(names, 3).then((packed) => {
|
||||
if (!packed || typeof packed !== 'object') return
|
||||
setSuggestions((prev) => ({ ...prev, ...packed }))
|
||||
}).catch(() => {})
|
||||
}, [unmappedKey])
|
||||
|
||||
useEffect(() => {
|
||||
api.listFoodUnits().then((list) => {
|
||||
@@ -140,8 +186,13 @@ export default function FoodRecipeDialog({
|
||||
}, [])
|
||||
|
||||
const mappedHits = filterLearned(q, localLearned)
|
||||
const mappedNames = new Set(mappedHits.map((m) => (m.food_name_de || '').toLowerCase()))
|
||||
const catalogOnly = catalogHits.filter((h) => !mappedNames.has((h.name_de || '').toLowerCase()))
|
||||
const listHits = filterKnownIngredients(q, knownNames).filter((n) => {
|
||||
const raw = (n.source_name_raw || '').toLowerCase()
|
||||
return !mappedHits.some((m) => (m.source_name_raw || '').toLowerCase() === raw)
|
||||
})
|
||||
const mappedFoodNames = new Set(mappedHits.map((m) => (m.food_name_de || '').toLowerCase()))
|
||||
const userHits = catalogHits.filter((h) => h.catalog_kind && h.catalog_kind !== 'official_bls')
|
||||
const blsHits = catalogHits.filter((h) => (!h.catalog_kind || h.catalog_kind === 'official_bls') && !mappedFoodNames.has((h.name_de || '').toLowerCase()))
|
||||
|
||||
const runCatalog = async (term) => {
|
||||
const query = (term || '').trim()
|
||||
@@ -216,6 +267,41 @@ export default function FoodRecipeDialog({
|
||||
mappedLabel: h.name_de,
|
||||
})
|
||||
|
||||
const addKnown = (n) => addIngredient({
|
||||
source_name_raw: n.source_name_raw,
|
||||
foodId: n.food_id || null,
|
||||
mappedLabel: n.food_name_de || '',
|
||||
})
|
||||
|
||||
const rememberMapping = (sourceName, food, extras = {}) => {
|
||||
setLocalLearned((list) => [{
|
||||
id: food.mapping_id,
|
||||
source_name_raw: sourceName,
|
||||
source_name_normalized: food.source_name_normalized,
|
||||
food_id: food.id || food.food_id,
|
||||
food_name_de: food.name_de || food.food_name_de,
|
||||
bls_code: food.bls_code,
|
||||
grams_per_unit: extras.grams_per_unit,
|
||||
source_unit: extras.source_unit,
|
||||
}, ...list.filter((m) => (m.source_name_raw || '').toLowerCase() !== sourceName.toLowerCase())])
|
||||
setIngs((list) => list.map((x) => (
|
||||
(x.source_name_raw || '').toLowerCase() === sourceName.toLowerCase()
|
||||
? { ...x, foodId: food.id || food.food_id, mappedLabel: food.name_de || food.food_name_de || '' }
|
||||
: x
|
||||
)))
|
||||
}
|
||||
|
||||
const assignIngredient = async (sourceName, food, extras = {}) => {
|
||||
const res = await api.upsertMyFoodMapping({
|
||||
source_name: sourceName,
|
||||
food_id: food.id || food.food_id,
|
||||
grams_per_unit: extras.grams_per_unit || null,
|
||||
source_unit: extras.source_unit || null,
|
||||
})
|
||||
rememberMapping(sourceName, { ...food, ...res }, extras)
|
||||
setMapFor(null)
|
||||
}
|
||||
|
||||
const addFreeText = () => {
|
||||
const raw = q.trim()
|
||||
if (!raw) return
|
||||
@@ -317,7 +403,7 @@ export default function FoodRecipeDialog({
|
||||
|
||||
<div style={{ overflowY: 'auto', padding: '12px 16px 16px', flex: 1 }}>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', lineHeight: 1.5, margin: '0 0 12px' }}>
|
||||
Roh-Kombination ohne Kochen (z. B. Müsli). Zutaten suchen, Menge und Einheit angeben. Gramm ist die Standardeinheit — EL, TL, Prise usw. werden umgerechnet, sobald der Faktor bekannt ist. Gekochte Gerichte bleiben in Tandoor.
|
||||
Roh-Kombination ohne Kochen (z. B. Müsli). Zuerst Namen aus deinen Listen, dann eigene Lebensmittel, dann BLS. Jede Zutat kannst du hier dem Katalog zuordnen — offene Zutaten bleiben rot markiert.
|
||||
</p>
|
||||
{error && <div style={{ color: 'var(--danger)', fontSize: 13, marginBottom: 10 }}>{error}</div>}
|
||||
|
||||
@@ -352,6 +438,25 @@ export default function FoodRecipeDialog({
|
||||
/>
|
||||
{showResults && (
|
||||
<div style={{ marginTop: 8, border: '1px solid var(--border)', borderRadius: 10, background: 'var(--surface2)' }}>
|
||||
{listHits.length > 0 && (
|
||||
<div style={{ padding: '8px 10px 4px', fontSize: 11, color: 'var(--text3)' }}>Aus deinen Listen</div>
|
||||
)}
|
||||
{listHits.map((n) => (
|
||||
<button
|
||||
key={`list-${n.source_name_normalized || n.source_name_raw}`}
|
||||
type="button"
|
||||
className="btn btn-secondary btn-full"
|
||||
style={{ margin: '0 8px 8px', justifyContent: 'flex-start', textAlign: 'left', height: 'auto', padding: '8px 10px' }}
|
||||
onClick={() => addKnown(n)}
|
||||
>
|
||||
<span>
|
||||
<strong style={{ display: 'block' }}>{n.source_name_raw}</strong>
|
||||
<span style={{ fontSize: 12, color: 'var(--text3)' }}>
|
||||
{n.mapped ? `→ ${n.food_name_de}${n.bls_code ? ` · ${n.bls_code}` : ''}` : 'noch ohne Mapping'}
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
{mappedHits.length > 0 && (
|
||||
<div style={{ padding: '8px 10px 4px', fontSize: 11, color: 'var(--text3)' }}>Bereits zugeordnet</div>
|
||||
)}
|
||||
@@ -370,10 +475,10 @@ export default function FoodRecipeDialog({
|
||||
</button>
|
||||
))}
|
||||
{searching && <p style={{ fontSize: 12, color: 'var(--text2)', padding: '6px 10px' }}>Katalog…</p>}
|
||||
{catalogOnly.length > 0 && (
|
||||
<div style={{ padding: '8px 10px 4px', fontSize: 11, color: 'var(--text3)' }}>Katalog</div>
|
||||
{userHits.length > 0 && (
|
||||
<div style={{ padding: '8px 10px 4px', fontSize: 11, color: 'var(--text3)' }}>Eigene Lebensmittel</div>
|
||||
)}
|
||||
{catalogOnly.map((h) => (
|
||||
{userHits.map((h) => (
|
||||
<button
|
||||
key={h.id}
|
||||
type="button"
|
||||
@@ -383,10 +488,24 @@ export default function FoodRecipeDialog({
|
||||
>
|
||||
<span>
|
||||
<strong style={{ display: 'block' }}>{h.name_de}</strong>
|
||||
<span style={{ fontSize: 12, color: 'var(--text3)' }}>
|
||||
{h.bls_code ? `BLS ${h.bls_code}` : 'ohne Code'}
|
||||
{h.catalog_kind !== 'official_bls' ? ' · manuell' : ''}
|
||||
</span>
|
||||
<span style={{ fontSize: 12, color: 'var(--text3)' }}>{h.bls_code ? `BLS ${h.bls_code}` : 'manuell'}</span>
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
{blsHits.length > 0 && (
|
||||
<div style={{ padding: '8px 10px 4px', fontSize: 11, color: 'var(--text3)' }}>BLS-Katalog</div>
|
||||
)}
|
||||
{blsHits.map((h) => (
|
||||
<button
|
||||
key={h.id}
|
||||
type="button"
|
||||
className="btn btn-secondary btn-full"
|
||||
style={{ margin: '0 8px 8px', justifyContent: 'flex-start', textAlign: 'left', height: 'auto', padding: '8px 10px' }}
|
||||
onClick={() => addCatalog(h)}
|
||||
>
|
||||
<span>
|
||||
<strong style={{ display: 'block' }}>{h.name_de}</strong>
|
||||
<span style={{ fontSize: 12, color: 'var(--text3)' }}>{h.bls_code ? `BLS ${h.bls_code}` : 'ohne Code'}</span>
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
@@ -396,12 +515,19 @@ export default function FoodRecipeDialog({
|
||||
</div>
|
||||
)}
|
||||
|
||||
<p style={{ fontSize: 13, fontWeight: 600, margin: '16px 0 8px' }}>Zutaten ({ings.length})</p>
|
||||
<p style={{ fontSize: 13, fontWeight: 600, margin: '16px 0 8px' }}>
|
||||
Zutaten ({ings.length}
|
||||
{ings.filter((i) => !mappingFor(i.source_name_raw, localLearned) && !i.foodId).length
|
||||
? ` · ${ings.filter((i) => !mappingFor(i.source_name_raw, localLearned) && !i.foodId).length} ohne Mapping`
|
||||
: ings.length ? ' · alle zugeordnet' : ''})
|
||||
</p>
|
||||
{ings.length === 0 && (
|
||||
<p style={{ fontSize: 13, color: 'var(--text3)', margin: 0 }}>Noch keine Zutaten — oben suchen und übernehmen.</p>
|
||||
)}
|
||||
{ings.map((ing, i) => {
|
||||
const mapped = mappingFor(ing.source_name_raw, localLearned) || (ing.foodId ? { food_name_de: ing.mappedLabel } : null)
|
||||
const mapped = mappingFor(ing.source_name_raw, localLearned) || (ing.foodId ? { food_name_de: ing.mappedLabel, food_id: ing.foodId } : null)
|
||||
const packed = suggestions[ing.source_name_raw] || {}
|
||||
const hints = packed.suggestions || []
|
||||
const meta = unitMeta(units, ing.unit)
|
||||
const approx = gramsHint(ing, units)
|
||||
const needsFactor = Boolean(meta?.needs_unit_map) && !approx
|
||||
@@ -456,9 +582,24 @@ export default function FoodRecipeDialog({
|
||||
{' '}g
|
||||
</label>
|
||||
)}
|
||||
<div style={{ fontSize: 11, color: mapped ? 'var(--accent-dark)' : 'var(--text3)', marginTop: 4 }}>
|
||||
{approx != null ? `≈ ${String(approx).replace('.', ',')} g` : (needsFactor ? 'Gramm pro Einheit fehlt — später umrechenbar' : '')}
|
||||
{mapped ? ` · zugeordnet: ${mapped.food_name_de || ing.mappedLabel}` : ' · noch offen — nach dem Speichern in der Liste zuordnen'}
|
||||
<div style={{ fontSize: 12, marginTop: 6, color: mapped ? 'var(--accent-dark)' : 'var(--danger)' }}>
|
||||
{mapped
|
||||
? `Zugeordnet: ${mapped.food_name_de || ing.mappedLabel}${mapped.bls_code ? ` · ${mapped.bls_code}` : ''}`
|
||||
: 'Noch kein Mapping'}
|
||||
{approx != null ? ` · ≈ ${String(approx).replace('.', ',')} g` : (needsFactor ? ' · Gramm pro Einheit fehlt' : '')}
|
||||
</div>
|
||||
{!mapped && hints[0] && (
|
||||
<div style={{ marginTop: 6, display: 'flex', flexWrap: 'wrap', gap: 6, alignItems: 'center' }}>
|
||||
<span style={{ fontSize: 12 }}>→ {hints[0].name_de}{hints[0].bls_code ? ` · ${hints[0].bls_code}` : ''}</span>
|
||||
<button type="button" className="btn btn-primary" style={{ fontSize: 12 }} disabled={saving || disabled} onClick={() => assignIngredient(ing.source_name_raw, hints[0]).catch((e) => setError(e.message))}>
|
||||
Bestätigen
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, marginTop: 6 }}>
|
||||
<button type="button" className="btn btn-secondary" style={{ fontSize: 12 }} disabled={saving || disabled} onClick={() => setMapFor(ing)}>
|
||||
{mapped ? 'Mapping ändern' : 'BLS zuordnen'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
@@ -472,6 +613,16 @@ export default function FoodRecipeDialog({
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{mapFor && (
|
||||
<FoodSearchModal
|
||||
zIndex={22000}
|
||||
title={`Zuordnen: ${mapFor.source_name_raw}`}
|
||||
initialQuery={mapFor.source_name_raw}
|
||||
quantityHint={`${mapFor.amount || ''} ${mapFor.unit || ''}`.trim()}
|
||||
onClose={() => setMapFor(null)}
|
||||
onSelect={(food, extras) => assignIngredient(mapFor.source_name_raw, food, extras).catch((e) => setError(e.message))}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -25,7 +25,7 @@ function detectUnit(...texts) {
|
||||
return null
|
||||
}
|
||||
|
||||
export default function FoodSearchModal({ title, initialQuery, quantityHint, allowRecipe, onSelect, onWantRecipe, onClose }) {
|
||||
export default function FoodSearchModal({ title, initialQuery, quantityHint, allowRecipe, onSelect, onWantRecipe, onClose, zIndex }) {
|
||||
const [q, setQ] = useState(initialQuery || '')
|
||||
const [hits, setHits] = useState([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
@@ -130,7 +130,7 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, all
|
||||
<div
|
||||
style={{
|
||||
position: 'fixed', inset: 0, background: 'rgba(0,0,0,0.45)',
|
||||
display: 'flex', alignItems: 'center', justifyContent: 'center', zIndex: 20000,
|
||||
display: 'flex', alignItems: 'center', justifyContent: 'center', zIndex: zIndex || 20000,
|
||||
padding: 16,
|
||||
}}
|
||||
onClick={onClose}
|
||||
|
||||
Reference in new issue
Block a user