added fitness

This commit is contained in:
2026-06-10 09:12:50 +03:00
parent 0b39692300
commit d0bdd1e95c
25 changed files with 2082 additions and 7 deletions
+1
View File
@@ -16,6 +16,7 @@ function noticeLabel(content: string): string {
if (content.startsWith("📋")) return "задачи";
if (content.startsWith("🔀")) return "git";
if (content.startsWith("🧠")) return "память";
if (content.startsWith("💪")) return "фитнес";
return "система";
}
+179
View File
@@ -0,0 +1,179 @@
.fitness-page {
max-width: 900px;
margin: 0 auto;
padding: 1.5rem;
display: flex;
flex-direction: column;
gap: 1rem;
}
.fitness-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
flex-wrap: wrap;
gap: 1rem;
}
.fitness-header h2 {
margin: 0 0 0.25rem;
}
.fitness-header p {
margin: 0;
color: #8b95a8;
font-size: 0.9rem;
}
.fitness-header-actions {
display: flex;
gap: 0.5rem;
}
.fitness-message {
padding: 0.6rem 0.9rem;
background: #1a2433;
border: 1px solid #2a3f5a;
border-radius: 8px;
}
.fitness-section {
background: #151922;
border: 1px solid #2a2f3a;
border-radius: 10px;
padding: 1rem 1.25rem;
}
.fitness-section h3 {
margin: 0 0 0.75rem;
}
.fitness-section h4 {
margin: 0.75rem 0 0.35rem;
font-size: 0.85rem;
color: #8b95a8;
}
.fitness-progress-grid {
display: flex;
flex-direction: column;
gap: 0.6rem;
}
.fitness-progress-header {
display: flex;
justify-content: space-between;
font-size: 0.85rem;
margin-bottom: 0.2rem;
}
.fitness-progress-track {
height: 8px;
background: #0f1218;
border-radius: 4px;
overflow: hidden;
}
.fitness-progress-fill {
height: 100%;
background: #3d7a5a;
border-radius: 4px;
}
.fitness-profile-form {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 0.75rem;
}
.fitness-profile-form label {
display: flex;
flex-direction: column;
gap: 0.25rem;
font-size: 0.85rem;
color: #8b95a8;
}
.fitness-profile-form input,
.fitness-profile-form select {
padding: 0.45rem 0.6rem;
border-radius: 6px;
border: 1px solid #2a2f3a;
background: #0f1218;
color: #e8ecf1;
}
.fitness-profile-form button {
grid-column: 1 / -1;
justify-self: start;
}
.fitness-computed {
margin: 0.75rem 0 0;
font-size: 0.9rem;
color: #8b95a8;
}
.fitness-log-list {
list-style: none;
margin: 0;
padding: 0;
}
.fitness-log-list li {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.35rem 0;
border-bottom: 1px solid #1e2430;
font-size: 0.9rem;
}
.fitness-log-list button {
padding: 0 0.4rem;
font-size: 1rem;
line-height: 1;
}
.fitness-table {
width: 100%;
border-collapse: collapse;
font-size: 0.9rem;
}
.fitness-table th,
.fitness-table td {
text-align: left;
padding: 0.35rem 0.5rem;
border-bottom: 1px solid #1e2430;
}
.fitness-reminders {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.fitness-reminders li {
display: flex;
gap: 1rem;
align-items: center;
}
.fitness-empty {
color: #8b95a8;
font-style: italic;
}
.fitness-raw {
background: #0f1218;
border: 1px solid #2a2f3a;
border-radius: 10px;
padding: 1rem;
overflow: auto;
font-size: 0.82rem;
max-height: 70vh;
}
+307
View File
@@ -0,0 +1,307 @@
import { FormEvent, useCallback, useEffect, useState } from "react";
import {
api,
FitnessProfile,
FitnessReminder,
FitnessSnapshot,
} from "../api/client";
import "./Fitness.css";
function ProgressBar({ label, current, target, unit }: {
label: string;
current: number;
target: number;
unit: string;
}) {
const pct = target > 0 ? Math.min(100, (current / target) * 100) : 0;
return (
<div className="fitness-progress">
<div className="fitness-progress-header">
<span>{label}</span>
<span>
{current.toFixed(0)}/{target.toFixed(0)} {unit}
</span>
</div>
<div className="fitness-progress-track">
<div className="fitness-progress-fill" style={{ width: `${pct}%` }} />
</div>
</div>
);
}
export default function Fitness() {
const [snapshot, setSnapshot] = useState<FitnessSnapshot | null>(null);
const [profile, setProfile] = useState<Partial<FitnessProfile>>({});
const [message, setMessage] = useState("");
const [showRaw, setShowRaw] = useState(false);
const [loading, setLoading] = useState(false);
const load = useCallback(async () => {
setLoading(true);
try {
const data = await api.getFitnessSnapshot();
setSnapshot(data);
if (data.profile) setProfile(data.profile);
} catch (err) {
setMessage(err instanceof Error ? err.message : "Ошибка загрузки");
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
load().catch(console.error);
}, [load]);
const handleProfileSave = async (e: FormEvent) => {
e.preventDefault();
try {
await api.updateFitnessProfile(profile);
setMessage("Профиль сохранён");
await load();
} catch (err) {
setMessage(err instanceof Error ? err.message : "Ошибка");
}
};
const handleToggleReminder = async (rem: FitnessReminder) => {
try {
await api.updateFitnessReminder(rem.kind, { enabled: !rem.enabled });
await load();
} catch (err) {
setMessage(err instanceof Error ? err.message : "Ошибка");
}
};
const handleDeleteMeal = async (id: number) => {
await api.deleteFitnessMeal(id);
await load();
};
const handleDeleteWater = async (id: number) => {
await api.deleteFitnessWater(id);
await load();
};
const today = snapshot?.today;
const totals = today?.totals;
const targets = today?.targets;
return (
<div className="fitness-page">
<header className="fitness-header">
<div>
<h2>Фитнес</h2>
<p>Дневник, цели, напоминания</p>
</div>
<div className="fitness-header-actions">
<button type="button" onClick={() => load()} disabled={loading}>
{loading ? "…" : "Обновить"}
</button>
<button type="button" onClick={() => setShowRaw((v) => !v)}>
{showRaw ? "UI" : "JSON"}
</button>
</div>
</header>
{message && <div className="fitness-message">{message}</div>}
{showRaw ? (
<pre className="fitness-raw">{JSON.stringify(snapshot, null, 2)}</pre>
) : (
<>
<section className="fitness-section">
<h3>Сегодня</h3>
{totals && targets ? (
<div className="fitness-progress-grid">
<ProgressBar
label="Калории"
current={totals.calories}
target={targets.calories}
unit="ккал"
/>
<ProgressBar
label="Белок"
current={totals.protein_g}
target={targets.protein_g}
unit="г"
/>
<ProgressBar
label="Жиры"
current={totals.fat_g}
target={targets.fat_g}
unit="г"
/>
<ProgressBar
label="Углеводы"
current={totals.carbs_g}
target={targets.carbs_g}
unit="г"
/>
<ProgressBar
label="Вода"
current={totals.water_ml / 1000}
target={targets.water_ml / 1000}
unit="л"
/>
</div>
) : (
<p className="fitness-empty">Нет данных за сегодня</p>
)}
</section>
<section className="fitness-section">
<h3>Профиль и цели</h3>
<form className="fitness-profile-form" onSubmit={handleProfileSave}>
<label>
<span>пол</span>
<select
value={profile.sex ?? "male"}
onChange={(e) => setProfile((p) => ({ ...p, sex: e.target.value }))}
>
<option value="male">male</option>
<option value="female">female</option>
</select>
</label>
<label>
<span>возраст</span>
<input
type="number"
value={profile.age ?? ""}
onChange={(e) =>
setProfile((p) => ({ ...p, age: Number(e.target.value) }))
}
/>
</label>
<label>
<span>рост см</span>
<input
type="number"
value={profile.height_cm ?? ""}
onChange={(e) =>
setProfile((p) => ({ ...p, height_cm: Number(e.target.value) }))
}
/>
</label>
<label>
<span>вес кг</span>
<input
type="number"
value={profile.weight_kg ?? ""}
onChange={(e) =>
setProfile((p) => ({ ...p, weight_kg: Number(e.target.value) }))
}
/>
</label>
<label>
<span>активность</span>
<select
value={profile.activity_level ?? "moderate"}
onChange={(e) =>
setProfile((p) => ({ ...p, activity_level: e.target.value }))
}
>
<option value="sedentary">sedentary</option>
<option value="light">light</option>
<option value="moderate">moderate</option>
<option value="active">active</option>
<option value="very_active">very_active</option>
</select>
</label>
<label>
<span>цель</span>
<select
value={profile.goal ?? "maintain"}
onChange={(e) => setProfile((p) => ({ ...p, goal: e.target.value }))}
>
<option value="lose">lose</option>
<option value="maintain">maintain</option>
<option value="gain">gain</option>
</select>
</label>
<button type="submit">Сохранить и пересчитать TDEE</button>
</form>
{profile.computed && (
<p className="fitness-computed">
BMR {profile.computed.bmr} · TDEE {profile.computed.tdee} · BMI{" "}
{profile.computed.bmi}
</p>
)}
</section>
<section className="fitness-section">
<h3>Логи за сегодня</h3>
<h4>Еда</h4>
<ul className="fitness-log-list">
{(today?.meals ?? []).map((m) => (
<li key={m.id}>
{m.estimated ? "≈" : ""}
{m.description} {m.calories} ккал
<button type="button" onClick={() => handleDeleteMeal(m.id)}>
×
</button>
</li>
))}
</ul>
<h4>Вода</h4>
<ul className="fitness-log-list">
{(today?.water ?? []).map((w) => (
<li key={w.id}>
+{w.amount_ml} мл
<button type="button" onClick={() => handleDeleteWater(w.id)}>
×
</button>
</li>
))}
</ul>
<h4>Тренировки</h4>
<ul className="fitness-log-list">
{(today?.workouts ?? []).map((w) => (
<li key={w.id}>{w.title}</li>
))}
</ul>
</section>
<section className="fitness-section">
<h3>История веса</h3>
<table className="fitness-table">
<thead>
<tr>
<th>Дата</th>
<th>кг</th>
</tr>
</thead>
<tbody>
{(snapshot?.body_metrics ?? []).map((m) => (
<tr key={m.id}>
<td>{m.recorded_at?.slice(0, 10)}</td>
<td>{m.weight_kg}</td>
</tr>
))}
</tbody>
</table>
</section>
<section className="fitness-section">
<h3>Напоминания</h3>
<ul className="fitness-reminders">
{(snapshot?.reminders ?? []).map((r) => (
<li key={r.id}>
<span>{r.kind}</span>
<span>
{r.interval_hours
? `каждые ${r.interval_hours}ч`
: `${String(r.hour).padStart(2, "0")}:${String(r.minute).padStart(2, "0")}`}
</span>
<button type="button" onClick={() => handleToggleReminder(r)}>
{r.enabled ? "вкл" : "выкл"}
</button>
</li>
))}
</ul>
</section>
</>
)}
</div>
);
}