import { useCallback, useEffect, useState } from 'react' import { ChevronDown, ChevronUp, LayoutGrid } from 'lucide-react' import { Link } from 'react-router-dom' import { api, formatFastApiDetail } from '../utils/api' import { WidgetRenderer } from '../widgetSystem/dashboardWidgetRegistry' import { ensurePilotLabWidgetsRegistered } from '../widgetSystem/registerPilotLabWidgets' import { BODY_CHART_DAYS_DEFAULT, BODY_CHART_DAYS_MAX, BODY_CHART_DAYS_MIN, normalizeBodyChartDays, } 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']) function catalogMetaById(catalog) { if (!catalog?.widgets?.length) return {} return Object.fromEntries(catalog.widgets.map((w) => [w.id, w])) } export default function DashboardLabPage() { ensurePilotLabWidgetsRegistered() const [refreshTick, setRefreshTick] = useState(0) const requestRefresh = () => setRefreshTick((t) => t + 1) const [catalog, setCatalog] = useState(null) const [bundle, setBundle] = useState(null) const [layout, setLayout] = useState(null) const [err, setErr] = useState(null) const [busy, setBusy] = useState(false) const [msg, setMsg] = useState(null) /** Pro Widget-ID: Rohstring während der Eingabe (Tippen ohne sofortiges Clampen) */ const [chartDaysDraftByWidgetId, setChartDaysDraftByWidgetId] = useState({}) const metaById = catalogMetaById(catalog) const commitChartDaysDraftToLayout = useCallback((draftStr, baseLayout, widgetId) => { const clamped = normalizeBodyChartDays( draftStr === '' || draftStr == null ? BODY_CHART_DAYS_DEFAULT : draftStr ) return { ...baseLayout, widgets: baseLayout.widgets.map((x) => x.id !== widgetId ? x : { ...x, config: { ...x.config, chart_days: clamped } } ), } }, []) const load = useCallback(async () => { setErr(null) try { const [cat, b] = await Promise.all([api.getAppWidgetsCatalog(), api.getAppDashboardLayout()]) setCatalog(cat) setBundle(b) setChartDaysDraftByWidgetId({}) setLayout(normalizeLayoutForEditor(b.layout)) } catch (e) { setErr(formatFastApiDetail(null, e.message)) } }, []) useEffect(() => { load() }, [load]) const save = async () => { if (!layout) return let toSave = layout const draftEntries = Object.entries(chartDaysDraftByWidgetId) if (draftEntries.length) { for (const [wid, val] of draftEntries) { toSave = normalizeLayoutForEditor(commitChartDaysDraftToLayout(val, toSave, wid)) } setLayout(toSave) setChartDaysDraftByWidgetId({}) } setBusy(true) setMsg(null) setErr(null) try { await api.putAppDashboardLayout(toSave) setMsg('Layout gespeichert.') await load() } catch (e) { setErr(formatFastApiDetail(null, e.message)) } finally { setBusy(false) } } const reset = async () => { if (!confirm('Persönliches Layout löschen und Standard wiederherstellen?')) return setBusy(true) setMsg(null) setErr(null) try { const r = await api.resetAppDashboardLayout() setChartDaysDraftByWidgetId({}) setLayout(normalizeLayoutForEditor(r.layout)) setMsg('Auf Standard zurückgesetzt.') await load() } catch (e) { setErr(formatFastApiDetail(null, e.message)) } finally { setBusy(false) } } const applyDefaultLocal = () => { if (bundle?.default_layout) { setChartDaysDraftByWidgetId({}) setLayout(normalizeLayoutForEditor(structuredClone(bundle.default_layout))) setMsg('Standard geladen (noch nicht gespeichert).') } } if (err && !layout) { return (

{err}

) } if (!layout) { return (
) } return (
← Einstellungen

App-Bereich: Dashboard-Lab

Widget-System: Katalog, Registry, Renderer; optional pro Widget config (z. B.{' '} Körper und Aktivität: Zeitraum 7–90 Tage). Layout pro Profil in der DB — getrennt vom Produktiv-Dashboard. Vergleich:{' '} Pilot-Übersicht (festes Standard-Layout) .

Layout (v1)
{bundle && (

Status: {bundle.custom ? 'individuell gespeichert' : 'Standard (nicht in DB)'}

)} {err &&

{err}

} {msg &&

{msg}

}
    {layout.widgets.map((w, i) => { const label = metaById[w.id]?.title || w.id const chartDaysVal = w.config?.chart_days != null ? normalizeBodyChartDays(w.config.chart_days) : BODY_CHART_DAYS_DEFAULT return (
  • {CHART_DAYS_WIDGET_IDS.has(w.id) && (
    setChartDaysDraftByWidgetId((prev) => ({ ...prev, [w.id]: String(chartDaysVal), })) } onChange={(e) => setChartDaysDraftByWidgetId((prev) => ({ ...prev, [w.id]: e.target.value, })) } onBlur={(e) => { const raw = e.target.value setLayout((L) => normalizeLayoutForEditor(commitChartDaysDraftToLayout(raw, L, w.id)) ) setChartDaysDraftByWidgetId((prev) => { const next = { ...prev } delete next[w.id] return next }) }} onKeyDown={(e) => { if (e.key === 'Enter') e.currentTarget.blur() }} />
    )}
  • ) })}
) }