"use client";
import {
ArrowTrendingDownIcon,
CalendarDaysIcon,
PlusIcon,
ScaleIcon,
TrashIcon,
} from "@heroicons/react/24/outline";
import { useMemo, useState } from "react";
import { todayKey } from "@/lib/hellth-data";
import { trpc } from "@/lib/trpc";
function formatDate(dateKey: string) {
return new Date(`${dateKey}T12:00:00`).toLocaleDateString("de-DE", {
day: "2-digit",
month: "2-digit",
year: "numeric",
});
}
function WeightChart({ entries }: { entries: Array<{ id?: string; date: string; weightKg: number }> }) {
if (entries.length < 2) {
return (
Noch zu wenig Messpunkte für einen Verlauf.
);
}
const min = Math.min(...entries.map((entry) => entry.weightKg));
const max = Math.max(...entries.map((entry) => entry.weightKg));
const range = Math.max(0.5, max - min);
return (
{entries.map((entry) => {
const height = 18 + ((entry.weightKg - min) / range) * 76;
return (
);
})}
);
}
export default function GewichtPage() {
const utils = trpc.useUtils();
const [date, setDate] = useState(todayKey());
const [weightKg, setWeightKg] = useState("");
const [note, setNote] = useState("");
const [status, setStatus] = useState(null);
const historyQuery = trpc.health.weightHistory.useQuery({ limit: 120 });
const saveMutation = trpc.health.upsertWeightLog.useMutation({
onSuccess: async () => {
await Promise.all([
utils.health.weightHistory.invalidate(),
utils.health.statsSummary.invalidate(),
utils.health.overview.invalidate({ date }),
]);
setWeightKg("");
setNote("");
setStatus("Gewicht gespeichert.");
},
});
const deleteMutation = trpc.health.deleteWeightLog.useMutation({
onSuccess: async () => {
await Promise.all([
utils.health.weightHistory.invalidate(),
utils.health.statsSummary.invalidate(),
]);
setStatus("Eintrag entfernt.");
},
});
const history = historyQuery.data;
const entries = history?.entries ?? [];
const latest = history?.latest;
const sortedRecent = useMemo(() => [...entries].reverse(), [entries]);
function saveWeight() {
const value = Number(weightKg.replace(",", "."));
if (!value || value <= 0) return;
saveMutation.mutate({
date,
weightKg: value,
note: note.trim() || null,
});
}
return (
Gewicht
Verlauf statt Tageslaune
Einzelne Werte schwanken. Hellth Hub schaut auf den Trend und macht Prognosen erst, wenn genug echte Daten da sind.
Gewichtshistorie
{entries.length} Messpunkt(e) in der Datenbank
Ziel: {history?.targetWeightKg ? `${history.targetWeightKg} kg` : "nicht gesetzt"}
Aktuell
{latest ? `${latest.weightKg} kg` : "-"}
Seit Start
{history ? `${history.deltaFromFirst} kg` : "-"}
Letzte Änderung
{history ? `${history.deltaFromPrevious} kg` : "-"}
Messwerte
{sortedRecent.map((entry) => (
{entry.weightKg} kg
{formatDate(entry.date)}
{entry.note ? ` · ${entry.note}` : ""}
))}
{!entries.length ?
Noch keine Messwerte gespeichert.
: null}
);
}