feat: Extend widget configuration for KPI board and enhance validation
Deploy Development / deploy (push) Failing after 44s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 14s

- Added support for the "kpi_board" widget in the dashboard configuration, allowing for chart_days validation.
- Updated the widget catalog description to reflect the new configuration options for KPI tiles.
- Enhanced the DashboardLabPage to manage chart_days input for the KPI board, improving user experience.
- Introduced normalization functions for KPI kcal window days to maintain consistent behavior.
- Bumped app_dashboard version to 1.4.0 to reflect these enhancements.
This commit is contained in:
Lars committed 2026-04-07 12:37:04 +02:00
1 parent 97f9aa696e
commit de99856a28
8 files changed
+65 -24

No files matched your search

@@ -4,6 +4,7 @@ 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'
const MAX_KPI = 9
@@ -16,9 +17,9 @@ function formatRefVal(row) {
}
/**
* KPIs: Referenzwerte (Layer-1-Summary) + Körperfett % + Ø Kalorien 7T — max. 9 Kacheln.
* KPIs: Referenzwerte (Layer-1-Summary) + Körperfett % + Ø Kalorien (Fenster konfigurierbar) — max. 9 Kacheln.
*/
export default function PilotKpiBoard({ refreshTick = 0 }) {
export default function PilotKpiBoard({ refreshTick = 0, kcalWindowDays = KPI_KCAL_WINDOW_DEFAULT }) {
const { activeProfile } = useProfile()
const sex = activeProfile?.sex || 'm'
const [refs, setRefs] = useState([])
@@ -32,15 +33,18 @@ export default function PilotKpiBoard({ refreshTick = 0 }) {
;(async () => {
try {
setLoading(true)
const nutrLimit = Math.min(2000, Math.max(60, kcalWindowDays * 5))
const [summary, calipers, nutrition] = await Promise.all([
api.listProfileReferenceValuesSummary().catch(() => ({ tiles: [] })),
api.listCaliper(3).catch(() => []),
api.listNutrition(30).catch(() => []),
api.listNutrition(nutrLimit).catch(() => []),
])
if (cancelled) return
const tiles = Array.isArray(summary?.tiles) ? summary.tiles.filter((t) => t?.latest) : []
const latestCal = Array.isArray(calipers) && calipers[0]?.body_fat_pct != null ? calipers[0] : null
const recentNutr = (nutrition || []).filter((n) => n.date >= dayjs().subtract(7, 'day').format('YYYY-MM-DD'))
const recentNutr = (nutrition || []).filter(
(n) => n.date >= dayjs().subtract(kcalWindowDays, 'day').format('YYYY-MM-DD'),
})
const kcal =
recentNutr.length > 0
? Math.round(recentNutr.reduce((s, n) => s + (n.kcal || 0), 0) / recentNutr.length)
@@ -74,7 +78,7 @@ export default function PilotKpiBoard({ refreshTick = 0 }) {
return () => {
cancelled = true
}
}, [refreshTick, sex])
}, [refreshTick, sex, kcalWindowDays])
if (loading) {
return (
@@ -124,7 +128,9 @@ export default function PilotKpiBoard({ refreshTick = 0 }) {
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 (7T)</div>
<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>,
+24 -9
View File
@@ -8,12 +8,14 @@ import {
BODY_CHART_DAYS_DEFAULT,
BODY_CHART_DAYS_MAX,
BODY_CHART_DAYS_MIN,
KPI_KCAL_WINDOW_DEFAULT,
normalizeBodyChartDays,
normalizeKpiKcalWindowDays,
} from '../widgetSystem/bodyChartDays'
import { moveWidget, normalizeLayoutForEditor, toggleWidget } from '../widgetSystem/layoutEditor'
/** Widgets mit optionalem config.chart_days (7–90), gleiche UX im Editor */
const CHART_DAYS_WIDGET_IDS = new Set(['body_overview', 'activity_overview'])
const CHART_DAYS_WIDGET_IDS = new Set(['body_overview', 'activity_overview', 'kpi_board'])
function catalogMetaById(catalog) {
if (!catalog?.widgets?.length) return {}
@@ -37,9 +39,12 @@ export default function DashboardLabPage() {
const metaById = catalogMetaById(catalog)
const commitChartDaysDraftToLayout = useCallback((draftStr, baseLayout, widgetId) => {
const clamped = normalizeBodyChartDays(
draftStr === '' || draftStr == null ? BODY_CHART_DAYS_DEFAULT : draftStr
)
const clamped =
widgetId === 'kpi_board'
? normalizeKpiKcalWindowDays(draftStr === '' || draftStr == null ? null : draftStr)
: normalizeBodyChartDays(
draftStr === '' || draftStr == null ? BODY_CHART_DAYS_DEFAULT : draftStr
)
return {
...baseLayout,
widgets: baseLayout.widgets.map((x) =>
@@ -151,7 +156,8 @@ export default function DashboardLabPage() {
</h1>
<p style={{ fontSize: 13, color: 'var(--text2)', lineHeight: 1.6, marginTop: 8 }}>
Widget-System: Katalog, Registry, Renderer; optional pro Widget <code>config</code> (z. B.{' '}
<strong>Körper</strong> und <strong>Aktivität</strong>: Zeitraum 7–90 Tage). Layout pro Profil in der DB —
<strong>Körper</strong>, <strong>Aktivität</strong>, <strong>KPI Ø-Kalorien</strong>: 7–90 Tage). Layout pro
Profil in der DB —
getrennt vom Produktiv-Dashboard.
Vergleich:{' '}
<Link to="/pilot/viz" style={{ color: 'var(--accent)' }}>
@@ -183,8 +189,11 @@ export default function DashboardLabPage() {
<ul style={{ listStyle: 'none', padding: 0, margin: '0 0 12px' }}>
{layout.widgets.map((w, i) => {
const label = metaById[w.id]?.title || w.id
const chartDaysFallback = w.id === 'kpi_board' ? KPI_KCAL_WINDOW_DEFAULT : BODY_CHART_DAYS_DEFAULT
const chartDaysVal =
w.config?.chart_days != null ? normalizeBodyChartDays(w.config.chart_days) : BODY_CHART_DAYS_DEFAULT
w.config?.chart_days != null
? normalizeBodyChartDays(w.config.chart_days)
: chartDaysFallback
return (
<li
key={w.id}
@@ -233,8 +242,12 @@ export default function DashboardLabPage() {
{CHART_DAYS_WIDGET_IDS.has(w.id) && (
<div style={{ marginTop: 10, marginLeft: 28 }}>
<label style={{ fontSize: 12, color: 'var(--text2)', display: 'block', marginBottom: 4 }}>
{w.id === 'body_overview' ? 'Körper-Chart' : 'Aktivität (Verteilung & Konsistenz)'} — Zeitraum
(Tage): {BODY_CHART_DAYS_MIN}–{BODY_CHART_DAYS_MAX}
{w.id === 'body_overview'
? 'Körper-Chart'
: w.id === 'activity_overview'
? 'Aktivität (Verteilung & Konsistenz)'
: 'KPI Ø-Kalorien (Ernährung)'}{' '}
— Zeitraum (Tage): {BODY_CHART_DAYS_MIN}–{BODY_CHART_DAYS_MAX}
</label>
<input
type="text"
@@ -245,7 +258,9 @@ export default function DashboardLabPage() {
aria-label={
w.id === 'body_overview'
? 'Körper-Chart Zeitraum in Tagen'
: 'Aktivität Zeitraum in Tagen'
: w.id === 'activity_overview'
? 'Aktivität Zeitraum in Tagen'
: 'KPI Ø-Kalorien Fenster in Tagen'
}
value={
chartDaysDraftByWidgetId[w.id] !== undefined
+9 -1
View File
@@ -1,4 +1,4 @@
/** Körper-Chart: gültiger Bereich (sync mit backend dashboard_widget_config body_overview). */
/** Körper-/Aktivitäts-Chart: gültiger Bereich (sync mit backend dashboard_widget_config). */
export const BODY_CHART_DAYS_MIN = 7
export const BODY_CHART_DAYS_MAX = 90
export const BODY_CHART_DAYS_DEFAULT = 30
@@ -8,3 +8,11 @@ export function normalizeBodyChartDays(raw) {
if (!Number.isFinite(n)) return BODY_CHART_DAYS_DEFAULT
return Math.min(BODY_CHART_DAYS_MAX, Math.max(BODY_CHART_DAYS_MIN, Math.round(n)))
}
/** KPI-Board Ø-Kalorien: gleiche Grenzen, Default 7 Tage (bisheriges Verhalten ohne config). */
export const KPI_KCAL_WINDOW_DEFAULT = 7
export function normalizeKpiKcalWindowDays(raw) {
if (raw == null || raw === '') return KPI_KCAL_WINDOW_DEFAULT
return normalizeBodyChartDays(raw)
}
@@ -6,7 +6,7 @@ import PilotQuickCapture from '../components/pilot/PilotQuickCapture'
import PilotKpiBoard from '../components/pilot/PilotKpiBoard'
import PilotBodySection from '../components/pilot/PilotBodySection'
import PilotActivitySection from '../components/pilot/PilotActivitySection'
import { normalizeBodyChartDays } from './bodyChartDays'
import { normalizeBodyChartDays, normalizeKpiKcalWindowDays } from './bodyChartDays'
import { registerDashboardWidget } from './dashboardWidgetRegistry'
let _registered = false
@@ -28,7 +28,10 @@ export function ensurePilotLabWidgetsRegistered() {
registerDashboardWidget({
id: 'kpi_board',
Component: PilotKpiBoard,
mapProps: (ctx) => ({ refreshTick: ctx.refreshTick }),
mapProps: (ctx) => ({
refreshTick: ctx.refreshTick,
kcalWindowDays: normalizeKpiKcalWindowDays(ctx.layoutEntry?.config?.chart_days),
}),
})
registerDashboardWidget({
id: 'body_overview',