feat: Revamp KPI board configuration and validation
Deploy Development / deploy (push) Successful in 53s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 15s

- 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:
Lars committed 2026-04-07 12:46:18 +02:00
1 parent 096d896166
commit c0c512e942
10 files changed
+390 -91

No files matched your search

@@ -0,0 +1,153 @@
import { useEffect, useMemo, useState } from 'react'
import { ChevronDown, ChevronUp } from 'lucide-react'
import { api } from '../utils/api'
import { KPI_KCAL_WINDOW_DEFAULT } from './bodyChartDays'
import { KPI_TILE_AVG_KCAL, KPI_TILE_BODY_FAT, REF_TILE_PREFIX } from './kpiBoardTiles'
/**
* @param {{ tiles: { id: string }[] | undefined, onChange: (next: { id: string }[] | undefined) => void }} props
* undefined tiles = automatisch (kein config.tiles)
*/
export default function KpiBoardConfigEditor({ tiles, onChange }) {
const [catalog, setCatalog] = useState([])
useEffect(() => {
let ok = true
api
.listProfileReferenceValuesSummary()
.then((s) => {
if (!ok) return
/** @type {{ id: string, label: string }[]} */
const opts = [
{ id: KPI_TILE_BODY_FAT, label: 'Körperfett (Caliper)' },
{ id: KPI_TILE_AVG_KCAL, label: `Ø Kalorien (${KPI_KCAL_WINDOW_DEFAULT} Tage)` },
]
const list = Array.isArray(s?.tiles) ? s.tiles : []
for (const t of list) {
if (t?.type_key) {
opts.push({
id: `${REF_TILE_PREFIX}${t.type_key}`,
label: t.type_label || t.type_key,
})
}
}
setCatalog(opts)
})
.catch(() => {
if (ok) setCatalog([])
})
return () => {
ok = false
}
}, [])
const labelById = useMemo(() => Object.fromEntries(catalog.map((c) => [c.id, c.label])), [catalog])
const ordered = Array.isArray(tiles) ? tiles : []
const toggle = (id, checked) => {
if (checked) {
if (ordered.some((t) => t.id === id) || ordered.length >= 9) return
onChange([...ordered, { id }])
} else {
const next = ordered.filter((t) => t.id !== id)
onChange(next.length ? next : [])
}
}
const move = (index, delta) => {
const j = index + delta
if (j < 0 || j >= ordered.length) return
const next = [...ordered]
const tmp = next[index]
next[index] = next[j]
next[j] = tmp
onChange(next)
}
return (
<div style={{ marginTop: 10, marginLeft: 28 }}>
<div style={{ fontSize: 12, color: 'var(--text2)', marginBottom: 8, lineHeight: 1.5 }}>
<strong>KPI-Kacheln:</strong> wählen und sortieren (max. 9). Ohne Auswahl oder „Automatisch“ =
bisherige automatische Belegung.
</div>
<button
type="button"
className="btn btn-secondary"
style={{ marginBottom: 10, fontSize: 12, padding: '6px 12px' }}
onClick={() => onChange(undefined)}
>
Automatisch (wie bisher)
</button>
{ordered.length > 0 && (
<div style={{ marginBottom: 12 }}>
<div style={{ fontSize: 11, fontWeight: 600, color: 'var(--text3)', marginBottom: 6 }}>
Reihenfolge (oben zuerst)
</div>
<ul style={{ listStyle: 'none', padding: 0, margin: 0 }}>
{ordered.map((t, idx) => (
<li
key={`${t.id}-${idx}`}
style={{
display: 'flex',
alignItems: 'center',
gap: 8,
padding: '6px 0',
borderBottom: '1px solid var(--border)',
fontSize: 13,
}}
>
<span style={{ flex: 1 }}>{labelById[t.id] || t.id}</span>
<button
type="button"
className="btn btn-secondary"
style={{ padding: '4px 8px' }}
aria-label="Nach oben"
onClick={() => move(idx, -1)}
>
<ChevronUp size={16} />
</button>
<button
type="button"
className="btn btn-secondary"
style={{ padding: '4px 8px' }}
aria-label="Nach unten"
onClick={() => move(idx, 1)}
>
<ChevronDown size={16} />
</button>
</li>
))}
</ul>
</div>
)}
<div
style={{
display: 'grid',
gridTemplateColumns: 'repeat(auto-fill, minmax(220px, 1fr))',
gap: 8,
maxHeight: 220,
overflowY: 'auto',
padding: 8,
background: 'var(--surface)',
borderRadius: 8,
border: '1px solid var(--border)',
}}
>
{catalog.map((c) => (
<label
key={c.id}
style={{ display: 'flex', alignItems: 'center', gap: 8, fontSize: 12, cursor: 'pointer' }}
>
<input
type="checkbox"
checked={ordered.some((t) => t.id === c.id)}
onChange={(e) => toggle(c.id, e.target.checked)}
/>
<span>{c.label}</span>
</label>
))}
</div>
</div>
)
}
+1 -6
View File
@@ -9,10 +9,5 @@ export function normalizeBodyChartDays(raw) {
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). */
/** KPI-Board Ø-Kalorien: festes Analysefenster (nicht mehr über Layout-Config). */
export const KPI_KCAL_WINDOW_DEFAULT = 7
export function normalizeKpiKcalWindowDays(raw) {
if (raw == null || raw === '') return KPI_KCAL_WINDOW_DEFAULT
return normalizeBodyChartDays(raw)
}
@@ -0,0 +1,22 @@
/** Feste KPI-Kachel-IDs (sync mit backend dashboard_widget_config). */
export const KPI_TILE_BODY_FAT = 'body_fat'
export const KPI_TILE_AVG_KCAL = 'avg_kcal'
export const REF_TILE_PREFIX = 'ref:'
/**
* @param {Record<string, unknown> | undefined} config
* @returns {string[] | undefined} undefined = automatische Kachelwahl (Legacy)
*/
export function kpiTileOrderFromConfig(config) {
if (!config || !Object.prototype.hasOwnProperty.call(config, 'tiles')) return undefined
const raw = config.tiles
if (!Array.isArray(raw)) return undefined
/** @type {string[]} */
const ids = []
for (const item of raw) {
const id = typeof item === 'string' ? item : item && item.id
if (typeof id === 'string' && id.trim()) ids.push(id.trim())
}
return ids.slice(0, 9)
}
@@ -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, normalizeKpiKcalWindowDays } from './bodyChartDays'
import { normalizeBodyChartDays } from './bodyChartDays'
import { registerDashboardWidget } from './dashboardWidgetRegistry'
let _registered = false
@@ -30,7 +30,7 @@ export function ensurePilotLabWidgetsRegistered() {
Component: PilotKpiBoard,
mapProps: (ctx) => ({
refreshTick: ctx.refreshTick,
kcalWindowDays: normalizeKpiKcalWindowDays(ctx.layoutEntry?.config?.chart_days),
kpiConfig: ctx.layoutEntry?.config || {},
}),
})
registerDashboardWidget({