feat: Revamp KPI board configuration and validation
- Enhanced the KPI board widget to support tile configuration, allowing users to select and order tiles. - Updated validation logic to ensure proper handling of tile IDs and configuration fields. - Removed legacy chart_days support, transitioning to a fixed analysis window for KPI metrics. - Improved the DashboardLabPage to integrate the new KpiBoardConfigEditor for managing tile selections. - Bumped app_dashboard version to 1.5.0 to reflect these significant changes.
This commit is contained in:
1 parent
096d896166
commit
c0c512e942
10 files changed
+390
-91
No files matched your search
@@ -1,10 +1,11 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useState, useEffect, useMemo, useCallback } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import dayjs from 'dayjs'
|
||||
import { api } from '../../utils/api'
|
||||
import { getBfCategory } from '../../utils/calc'
|
||||
import { useProfile } from '../../context/ProfileContext'
|
||||
import { KPI_KCAL_WINDOW_DEFAULT } from '../../widgetSystem/bodyChartDays'
|
||||
import { kpiTileOrderFromConfig } from '../../widgetSystem/kpiBoardTiles'
|
||||
|
||||
const MAX_KPI = 9
|
||||
|
||||
@@ -16,13 +17,33 @@ function formatRefVal(row) {
|
||||
return row.value_text != null ? String(row.value_text) : '–'
|
||||
}
|
||||
|
||||
function parseRefTypeKey(tileId) {
|
||||
if (!tileId.startsWith('ref:')) return null
|
||||
return tileId.slice(4) || null
|
||||
}
|
||||
|
||||
function buildAutoTileIds(refTiles, hasBf, hasKcal) {
|
||||
const ids = []
|
||||
for (const t of refTiles) {
|
||||
if (t?.type_key) ids.push(`ref:${t.type_key}`)
|
||||
}
|
||||
if (hasBf) ids.push('body_fat')
|
||||
if (hasKcal) ids.push('avg_kcal')
|
||||
return ids.slice(0, MAX_KPI)
|
||||
}
|
||||
|
||||
/**
|
||||
* KPIs: Referenzwerte (Layer-1-Summary) + Körperfett % + Ø Kalorien (Fenster konfigurierbar) — max. 9 Kacheln.
|
||||
* KPIs: Referenzwerte, Körperfett, Ø Kalorien — max. 9 Kacheln.
|
||||
* @param {{ refreshTick?: number, kpiConfig?: Record<string, unknown> }} props
|
||||
* kpiConfig.tiles: geordnete Kachel-ids; fehlend = automatische Belegung (wie bisher).
|
||||
*/
|
||||
export default function PilotKpiBoard({ refreshTick = 0, kcalWindowDays = KPI_KCAL_WINDOW_DEFAULT }) {
|
||||
export default function PilotKpiBoard({ refreshTick = 0, kpiConfig }) {
|
||||
const manualOrder = useMemo(() => kpiTileOrderFromConfig(kpiConfig), [kpiConfig])
|
||||
|
||||
const { activeProfile } = useProfile()
|
||||
const sex = activeProfile?.sex || 'm'
|
||||
const [refs, setRefs] = useState([])
|
||||
const [refTiles, setRefTiles] = useState([])
|
||||
const [refByKey, setRefByKey] = useState(() => new Map())
|
||||
const [bf, setBf] = useState(null)
|
||||
const [avgKcal, setAvgKcal] = useState(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
@@ -33,7 +54,8 @@ export default function PilotKpiBoard({ refreshTick = 0, kcalWindowDays = KPI_KC
|
||||
;(async () => {
|
||||
try {
|
||||
setLoading(true)
|
||||
const nutrLimit = Math.min(2000, Math.max(60, kcalWindowDays * 5))
|
||||
const kcalDays = KPI_KCAL_WINDOW_DEFAULT
|
||||
const nutrLimit = Math.min(2000, Math.max(60, kcalDays * 5))
|
||||
const [summary, calipers, nutrition] = await Promise.all([
|
||||
api.listProfileReferenceValuesSummary().catch(() => ({ tiles: [] })),
|
||||
api.listCaliper(3).catch(() => []),
|
||||
@@ -41,9 +63,10 @@ export default function PilotKpiBoard({ refreshTick = 0, kcalWindowDays = KPI_KC
|
||||
])
|
||||
if (cancelled) return
|
||||
const tiles = Array.isArray(summary?.tiles) ? summary.tiles.filter((t) => t?.latest) : []
|
||||
const map = new Map(tiles.map((t) => [t.type_key, t]))
|
||||
const latestCal = Array.isArray(calipers) && calipers[0]?.body_fat_pct != null ? calipers[0] : null
|
||||
const recentNutr = (nutrition || []).filter(
|
||||
(n) => n.date >= dayjs().subtract(kcalWindowDays, 'day').format('YYYY-MM-DD'),
|
||||
(n) => n.date >= dayjs().subtract(kcalDays, 'day').format('YYYY-MM-DD'),
|
||||
)
|
||||
const kcal =
|
||||
recentNutr.length > 0
|
||||
@@ -52,9 +75,9 @@ export default function PilotKpiBoard({ refreshTick = 0, kcalWindowDays = KPI_KC
|
||||
|
||||
const wantBf = !!latestCal?.body_fat_pct
|
||||
const wantKcal = kcal != null && kcal > 0
|
||||
const extra = (wantBf ? 1 : 0) + (wantKcal ? 1 : 0)
|
||||
const refCap = Math.max(0, MAX_KPI - extra)
|
||||
setRefs(tiles.slice(0, refCap))
|
||||
|
||||
setRefTiles(tiles)
|
||||
setRefByKey(map)
|
||||
setBf(
|
||||
wantBf
|
||||
? {
|
||||
@@ -69,7 +92,8 @@ export default function PilotKpiBoard({ refreshTick = 0, kcalWindowDays = KPI_KC
|
||||
} catch (e) {
|
||||
if (!cancelled) {
|
||||
setErr(e.message || 'KPIs konnten nicht geladen werden')
|
||||
setRefs([])
|
||||
setRefTiles([])
|
||||
setRefByKey(new Map())
|
||||
}
|
||||
} finally {
|
||||
if (!cancelled) setLoading(false)
|
||||
@@ -78,7 +102,73 @@ export default function PilotKpiBoard({ refreshTick = 0, kcalWindowDays = KPI_KC
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [refreshTick, sex, kcalWindowDays])
|
||||
}, [refreshTick, sex])
|
||||
|
||||
const orderIds = useMemo(() => {
|
||||
if (manualOrder !== undefined) {
|
||||
return manualOrder
|
||||
}
|
||||
const hasBf = !!bf
|
||||
const hasKcal = avgKcal != null && avgKcal > 0
|
||||
return buildAutoTileIds(refTiles, hasBf, hasKcal)
|
||||
}, [manualOrder, refTiles, bf, avgKcal])
|
||||
|
||||
const pushTileForId = useCallback(
|
||||
(id, out) => {
|
||||
if (id === 'body_fat') {
|
||||
if (!bf) return
|
||||
out.push(
|
||||
<div key="kpi-bf" className="card" style={{ margin: 0, padding: 12, boxSizing: 'border-box' }}>
|
||||
<div style={{ fontSize: 11, fontWeight: 600, color: 'var(--text3)' }}>Körperfett</div>
|
||||
<div style={{ fontSize: 17, fontWeight: 700, marginTop: 4, color: bf.cat?.color || 'var(--text1)' }}>
|
||||
{bf.pct}%
|
||||
</div>
|
||||
<div style={{ fontSize: 10, color: 'var(--text3)', marginTop: 4 }}>{bf.cat?.label || 'Caliper'}</div>
|
||||
</div>,
|
||||
)
|
||||
return
|
||||
}
|
||||
if (id === 'avg_kcal') {
|
||||
if (avgKcal == null) return
|
||||
out.push(
|
||||
<div key="kpi-kcal" className="card" style={{ margin: 0, padding: 12, boxSizing: 'border-box' }}>
|
||||
<div style={{ fontSize: 11, fontWeight: 600, color: 'var(--text3)' }}>
|
||||
Ø Kalorien ({KPI_KCAL_WINDOW_DEFAULT}T)
|
||||
</div>
|
||||
<div style={{ fontSize: 17, fontWeight: 700, marginTop: 4, color: '#EF9F27' }}>{avgKcal} kcal</div>
|
||||
<div style={{ fontSize: 10, color: 'var(--text3)', marginTop: 4 }}>Ernährung</div>
|
||||
</div>,
|
||||
)
|
||||
return
|
||||
}
|
||||
const tk = parseRefTypeKey(id)
|
||||
if (!tk) return
|
||||
const tile = refByKey.get(tk)
|
||||
if (!tile?.latest) return
|
||||
const l = tile.latest
|
||||
out.push(
|
||||
<div key={`ref-${tk}`} className="card" style={{ margin: 0, padding: 12, boxSizing: 'border-box' }}>
|
||||
<div style={{ fontSize: 11, fontWeight: 600, color: 'var(--text3)' }}>{tile.type_label}</div>
|
||||
<div style={{ fontSize: 17, fontWeight: 700, marginTop: 4 }}>
|
||||
{formatRefVal(l)}
|
||||
{l.unit ? (
|
||||
<span style={{ fontSize: 13, fontWeight: 600, color: 'var(--text2)', marginLeft: 4 }}>{l.unit}</span>
|
||||
) : null}
|
||||
</div>
|
||||
<div style={{ fontSize: 10, color: 'var(--text3)', marginTop: 4 }}>Ref.wert</div>
|
||||
</div>,
|
||||
)
|
||||
},
|
||||
[bf, avgKcal, refByKey],
|
||||
)
|
||||
|
||||
const visibleTiles = useMemo(() => {
|
||||
const out = []
|
||||
for (const id of orderIds) {
|
||||
pushTileForId(id, out)
|
||||
}
|
||||
return out
|
||||
}, [orderIds, pushTileForId])
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
@@ -95,54 +185,12 @@ export default function PilotKpiBoard({ refreshTick = 0, kcalWindowDays = KPI_KC
|
||||
)
|
||||
}
|
||||
|
||||
const tiles = []
|
||||
|
||||
refs.forEach((tile) => {
|
||||
const l = tile.latest
|
||||
tiles.push(
|
||||
<div key={`ref-${tile.type_key}`} className="card" style={{ margin: 0, padding: 12, boxSizing: 'border-box' }}>
|
||||
<div style={{ fontSize: 11, fontWeight: 600, color: 'var(--text3)' }}>{tile.type_label}</div>
|
||||
<div style={{ fontSize: 17, fontWeight: 700, marginTop: 4 }}>
|
||||
{formatRefVal(l)}
|
||||
{l.unit ? (
|
||||
<span style={{ fontSize: 13, fontWeight: 600, color: 'var(--text2)', marginLeft: 4 }}>{l.unit}</span>
|
||||
) : null}
|
||||
</div>
|
||||
<div style={{ fontSize: 10, color: 'var(--text3)', marginTop: 4 }}>Ref.wert</div>
|
||||
</div>,
|
||||
)
|
||||
})
|
||||
|
||||
if (bf) {
|
||||
tiles.push(
|
||||
<div key="kpi-bf" className="card" style={{ margin: 0, padding: 12, boxSizing: 'border-box' }}>
|
||||
<div style={{ fontSize: 11, fontWeight: 600, color: 'var(--text3)' }}>Körperfett</div>
|
||||
<div style={{ fontSize: 17, fontWeight: 700, marginTop: 4, color: bf.cat?.color || 'var(--text1)' }}>
|
||||
{bf.pct}%
|
||||
</div>
|
||||
<div style={{ fontSize: 10, color: 'var(--text3)', marginTop: 4 }}>{bf.cat?.label || 'Caliper'}</div>
|
||||
</div>,
|
||||
)
|
||||
}
|
||||
|
||||
if (avgKcal != null) {
|
||||
tiles.push(
|
||||
<div key="kpi-kcal" className="card" style={{ margin: 0, padding: 12, boxSizing: 'border-box' }}>
|
||||
<div style={{ fontSize: 11, fontWeight: 600, color: 'var(--text3)' }}>
|
||||
Ø Kalorien ({kcalWindowDays}T)
|
||||
</div>
|
||||
<div style={{ fontSize: 17, fontWeight: 700, marginTop: 4, color: '#EF9F27' }}>{avgKcal} kcal</div>
|
||||
<div style={{ fontSize: 10, color: 'var(--text3)', marginTop: 4 }}>Ernährung</div>
|
||||
</div>,
|
||||
)
|
||||
}
|
||||
|
||||
if (tiles.length === 0) {
|
||||
if (visibleTiles.length === 0) {
|
||||
return (
|
||||
<div className="card section-gap">
|
||||
<div className="card-title">Kennzahlen</div>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>
|
||||
Noch keine Daten.{' '}
|
||||
Noch keine Daten oder keine passenden Kacheln.{' '}
|
||||
<Link to="/settings/reference-values" style={{ color: 'var(--accent)' }}>
|
||||
Referenzwerte
|
||||
</Link>
|
||||
@@ -164,9 +212,11 @@ export default function PilotKpiBoard({ refreshTick = 0, kcalWindowDays = KPI_KC
|
||||
<div className="card section-gap">
|
||||
<div className="card-title">Kennzahlen</div>
|
||||
<p style={{ fontSize: 12, color: 'var(--text2)', marginBottom: 12, lineHeight: 1.5 }}>
|
||||
Referenzwerte (bis {MAX_KPI} gesamt inkl. KF% und Ø-Kalorien). Fehlende Typen werden automatisch weggelassen.
|
||||
{manualOrder !== undefined
|
||||
? 'Ausgewählte Kacheln in festgelegter Reihenfolge (ohne Daten werden Kacheln ausgelassen).'
|
||||
: `Bis ${MAX_KPI} Kacheln: Referenzwerte, Körperfett, Ø Kalorien (${KPI_KCAL_WINDOW_DEFAULT}T).`}
|
||||
</p>
|
||||
<div className="ref-value-tiles-grid">{tiles}</div>
|
||||
<div className="ref-value-tiles-grid">{visibleTiles}</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user