Spaces:
Running
Running
File size: 10,689 Bytes
49699c7 3d4aa49 6bddf9c 359f0ff 49699c7 3d4aa49 49699c7 6bddf9c 49699c7 6bddf9c 49699c7 6bddf9c 49699c7 359f0ff 49699c7 6bddf9c 359f0ff 6bddf9c 359f0ff 49699c7 3d4aa49 49699c7 3d4aa49 359f0ff 49699c7 3d4aa49 49699c7 359f0ff 6bddf9c 359f0ff 49699c7 6bddf9c 49699c7 3d4aa49 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | import { useState } from 'react';
import { formatPace } from '../utils/weekUtils';
import './RunLog.css';
const INJURY_LOCS = [
{ key: 'left_knee', label: 'L Knee' },
{ key: 'right_knee', label: 'R Knee' },
];
function RunLog({ runs, onEditRun, onDeleteRun }) {
const [editingId, setEditingId] = useState(null);
const [editForm, setEditForm] = useState({});
if (!runs || runs.length === 0) {
return (
<div className="run-log card">
<h2>Run History</h2>
<p className="empty-message">No runs logged yet. Add your first run above.</p>
</div>
);
}
const sorted = [...runs].sort((a, b) => b.date.localeCompare(a.date));
function formatDate(dateStr) {
const d = new Date(dateStr + 'T00:00:00');
return d.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' });
}
function handleEdit(run) {
setEditingId(run.id);
const injuries = {};
for (const loc of INJURY_LOCS) {
const d = run[`${loc.key}_during`];
const a = run[`${loc.key}_after`];
// Also support legacy single-location format
const legacyMatch = run.injury_location === loc.key;
if (d != null || a != null || legacyMatch) {
injuries[loc.key] = {
enabled: true,
during: legacyMatch && d == null ? (run.pain_during ?? '') : (d ?? ''),
after: legacyMatch && a == null ? (run.pain_after ?? '') : (a ?? ''),
};
}
}
setEditForm({
date: run.date,
distance_km: run.distance_km,
time_minutes: run.time_minutes,
rpe: run.rpe,
notes: run.notes || '',
injuries,
});
}
function toggleEditInjury(locKey, checked) {
setEditForm((prev) => {
const injuries = { ...prev.injuries };
if (checked) {
injuries[locKey] = { enabled: true, during: '', after: '' };
} else {
delete injuries[locKey];
}
return { ...prev, injuries };
});
}
function updateEditInjury(locKey, field, value) {
setEditForm((prev) => ({
...prev,
injuries: {
...prev.injuries,
[locKey]: { ...prev.injuries[locKey], [field]: value },
},
}));
}
function handleSave() {
const dist = parseFloat(editForm.distance_km);
const mins = parseFloat(editForm.time_minutes);
const rpe = parseInt(editForm.rpe, 10);
if (!editForm.date || isNaN(dist) || dist <= 0 || isNaN(mins) || mins <= 0 || isNaN(rpe) || rpe < 1 || rpe > 10) return;
const updated = {
date: editForm.date,
distance_km: dist,
time_minutes: mins,
rpe,
notes: (editForm.notes || '').trim(),
// Clear legacy fields
injury_location: null,
pain_during: null,
pain_after: null,
};
for (const loc of INJURY_LOCS) {
const injury = editForm.injuries?.[loc.key];
if (injury?.enabled) {
updated[`${loc.key}_during`] = injury.during !== '' ? Number(injury.during) : null;
updated[`${loc.key}_after`] = injury.after !== '' ? Number(injury.after) : null;
} else {
updated[`${loc.key}_during`] = null;
updated[`${loc.key}_after`] = null;
}
}
onEditRun(editingId, updated);
setEditingId(null);
}
function handleCancel() {
setEditingId(null);
}
function handleDelete(id) {
if (window.confirm('Delete this run?')) {
onDeleteRun(id);
}
}
function handleKeyDown(e) {
if (e.key === 'Enter') handleSave();
if (e.key === 'Escape') handleCancel();
}
return (
<div className="run-log card">
<h2>Run History</h2>
<div className="table-wrapper">
<table>
<thead>
<tr>
<th>Date</th>
<th>Distance</th>
<th>Time</th>
<th>Pace</th>
<th>RPE</th>
<th>Load</th>
<th>Pain (D/A)</th>
<th>Notes</th>
<th></th>
</tr>
</thead>
<tbody>
{sorted.map((run) =>
editingId === run.id ? (
<tr key={run.id} className="editing-row">
<td>
<input
type="date"
value={editForm.date}
onChange={(e) => setEditForm({ ...editForm, date: e.target.value })}
onKeyDown={handleKeyDown}
/>
</td>
<td>
<input
type="number"
step="0.01"
min="0.01"
value={editForm.distance_km}
onChange={(e) => setEditForm({ ...editForm, distance_km: e.target.value })}
onKeyDown={handleKeyDown}
/>
</td>
<td>
<input
type="number"
step="0.01"
min="0.01"
value={editForm.time_minutes}
onChange={(e) => setEditForm({ ...editForm, time_minutes: e.target.value })}
onKeyDown={handleKeyDown}
/>
</td>
<td className="computed-cell">
{formatPace(parseFloat(editForm.time_minutes), parseFloat(editForm.distance_km))}/km
</td>
<td>
<input
type="number"
min="1"
max="10"
value={editForm.rpe}
onChange={(e) => setEditForm({ ...editForm, rpe: e.target.value })}
onKeyDown={handleKeyDown}
/>
</td>
<td className="computed-cell">
{(parseFloat(editForm.distance_km || 0) * parseInt(editForm.rpe || 0, 10)).toFixed(0)}
</td>
<td>
<div className="injury-edit-stack">
{INJURY_LOCS.map((loc) => {
const injury = editForm.injuries?.[loc.key];
const enabled = !!injury?.enabled;
return (
<div key={loc.key} className="injury-edit-row">
<label className="injury-edit-toggle">
<input
type="checkbox"
checked={enabled}
onChange={(e) => toggleEditInjury(loc.key, e.target.checked)}
/>
<span>{loc.label}</span>
</label>
{enabled && (
<div className="pain-edit-cell">
<input
type="number" min="1" max="10" step="1"
value={injury.during}
onChange={(e) => updateEditInjury(loc.key, 'during', e.target.value)}
onKeyDown={handleKeyDown}
placeholder="D"
className="pain-input"
/>
<span>/</span>
<input
type="number" min="1" max="10" step="1"
value={injury.after}
onChange={(e) => updateEditInjury(loc.key, 'after', e.target.value)}
onKeyDown={handleKeyDown}
placeholder="A"
className="pain-input"
/>
</div>
)}
</div>
);
})}
</div>
</td>
<td>
<input
type="text"
value={editForm.notes}
onChange={(e) => setEditForm({ ...editForm, notes: e.target.value })}
onKeyDown={handleKeyDown}
placeholder="Notes..."
/>
</td>
<td className="action-buttons">
<button className="btn-save" onClick={handleSave} aria-label="Save">β</button>
<button className="btn-cancel" onClick={handleCancel} aria-label="Cancel">β</button>
</td>
</tr>
) : (
<tr key={run.id}>
<td>{formatDate(run.date)}</td>
<td>{run.distance_km.toFixed(1)} km</td>
<td>{run.time_minutes} min</td>
<td>{formatPace(run.time_minutes, run.distance_km)}/km</td>
<td>{run.rpe}/10</td>
<td>{(run.distance_km * run.rpe).toFixed(0)}</td>
<td className="pain-display-cell">
{(() => {
const entries = INJURY_LOCS.filter((loc) => {
// Support new per-location fields and legacy single-location format
const hasNew = run[`${loc.key}_during`] != null || run[`${loc.key}_after`] != null;
const hasLegacy = run.injury_location === loc.key;
return hasNew || hasLegacy;
});
if (entries.length === 0) return 'β';
return entries.map((loc) => {
const d = run[`${loc.key}_during`] ?? (run.injury_location === loc.key ? run.pain_during : null);
const a = run[`${loc.key}_after`] ?? (run.injury_location === loc.key ? run.pain_after : null);
return <div key={loc.key}>{loc.label}: {d ?? 'β'}/{a ?? 'β'}</div>;
});
})()}
</td>
<td className="notes-cell">{run.notes || ''}</td>
<td className="action-buttons">
<button className="btn-edit" onClick={() => handleEdit(run)} aria-label="Edit run">β</button>
<button className="btn-delete" onClick={() => handleDelete(run.id)} aria-label="Delete run">β</button>
</td>
</tr>
)
)}
</tbody>
</table>
</div>
</div>
);
}
export default RunLog;
|