281 lines
13 KiB
TypeScript
281 lines
13 KiB
TypeScript
import { useState } from 'react';
|
||
import { useTranslation } from 'react-i18next';
|
||
import { ChevronDown, ChevronUp, MessageSquare, MessageSquarePlus, Trash2 } from 'lucide-react';
|
||
import type { HistorySession } from '../api/types';
|
||
import { fmtDate, fmtDuration, fmtVolume } from '../lib/format';
|
||
import { localizedName } from '../lib/localizedName';
|
||
import { muscleName, SESSION_STATUS } from '../lib/labels';
|
||
import { Badge, Button, Textarea } from './ui';
|
||
|
||
interface HistoryTableProps {
|
||
sessions: HistorySession[];
|
||
/** Pokaż formularz „Dodaj komentarz” pod ćwiczeniami. */
|
||
canComment?: boolean;
|
||
/** Id zalogowanego usera (przycisk usuwania własnych komentarzy). */
|
||
myUserId?: string;
|
||
/** Sesja otwarta od razu (np. po kliknięciu karty załącznika). */
|
||
initialOpenSessionId?: string;
|
||
onAddComment?: (args: {
|
||
sessionId: string;
|
||
sessionExerciseId: string;
|
||
body: string;
|
||
}) => Promise<void>;
|
||
onDeleteComment?: (commentId: string) => Promise<void>;
|
||
}
|
||
|
||
/**
|
||
* Read-only session history with plan vs. actual per set + comments under
|
||
* executed exercises. Shared by the user's Profile and the trainer's trainee detail.
|
||
*/
|
||
export default function HistoryTable({
|
||
sessions,
|
||
canComment = false,
|
||
myUserId,
|
||
initialOpenSessionId,
|
||
onAddComment,
|
||
onDeleteComment,
|
||
}: HistoryTableProps) {
|
||
const { t, i18n } = useTranslation();
|
||
const [openSession, setOpenSession] = useState<string | null>(initialOpenSessionId ?? null);
|
||
const [drafts, setDrafts] = useState<Record<string, string>>({});
|
||
const [busyKey, setBusyKey] = useState<string | null>(null);
|
||
|
||
const submitComment = async (sessionId: string, sessionExerciseId: string) => {
|
||
const body = (drafts[sessionExerciseId] ?? '').trim();
|
||
if (!body || !onAddComment) return;
|
||
setBusyKey(sessionExerciseId);
|
||
try {
|
||
await onAddComment({ sessionId, sessionExerciseId, body });
|
||
setDrafts((d) => ({ ...d, [sessionExerciseId]: '' }));
|
||
} finally {
|
||
setBusyKey(null);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="divide-y divide-line">
|
||
{sessions.map((s) => {
|
||
const st = SESSION_STATUS[s.status];
|
||
const open = openSession === s.id;
|
||
const plannedTotal = s.exercises.reduce(
|
||
(n, ex) => n + ex.sets.filter((set) => set.planned).length,
|
||
0,
|
||
);
|
||
const plannedDone = s.exercises.reduce(
|
||
(n, ex) => n + ex.sets.filter((set) => set.planned && set.reps != null).length,
|
||
0,
|
||
);
|
||
// Sets performed beyond the plan: sets without a planned counterpart with recorded reps.
|
||
const extraSets = s.exercises.reduce(
|
||
(n, ex) => n + ex.sets.filter((set) => set.planned == null && set.reps != null).length,
|
||
0,
|
||
);
|
||
// Deficyt = niewyrobione serie normowe (bonus nie wchodzi w normę).
|
||
const deficit = plannedTotal - plannedDone;
|
||
// Objętość = Σ planowane obciążenie × powtórzenia (faktyczny load nie istnieje w danych).
|
||
const volume = s.exercises
|
||
.flatMap((ex) => ex.sets)
|
||
.reduce(
|
||
(sum, set) =>
|
||
set.reps != null && set.planned?.loadKg != null
|
||
? sum + set.planned.loadKg * set.reps
|
||
: sum,
|
||
0,
|
||
);
|
||
const duration = s.endedAt ? fmtDuration(s.startedAt, s.endedAt) : null;
|
||
return (
|
||
<div key={s.id} className="px-1">
|
||
<button
|
||
type="button"
|
||
onClick={() => setOpenSession(open ? null : s.id)}
|
||
aria-expanded={open}
|
||
className="w-full cursor-pointer rounded-[10px] px-2 py-3 text-left transition-colors hover:bg-line/20"
|
||
>
|
||
<div className="flex items-center justify-between gap-3">
|
||
<div className="min-w-0">
|
||
<div className="truncate font-display text-[0.95rem] font-semibold tabular-nums">
|
||
{s.date ? fmtDate(s.date) : '—'} · {s.plan}
|
||
</div>
|
||
<div className="mt-0.5 flex flex-wrap items-center gap-x-3 gap-y-0.5 text-dim text-[0.8125rem]">
|
||
{volume > 0 && <span className="tabular-nums">{fmtVolume(volume)}</span>}
|
||
{duration && <span className="tabular-nums">{duration}</span>}
|
||
{plannedTotal > 0 && (
|
||
<span className="tabular-nums">
|
||
{t('history.quota', { done: plannedDone, total: plannedTotal })}
|
||
</span>
|
||
)}
|
||
{extraSets > 0 && (
|
||
<span className="tabular-nums">
|
||
{t('history.bonusValue', { n: extraSets })}
|
||
</span>
|
||
)}
|
||
{deficit > 0 && (
|
||
<span className="tabular-nums">
|
||
{t('history.deficitValue', { n: deficit })}
|
||
</span>
|
||
)}
|
||
</div>
|
||
</div>
|
||
<div className="flex shrink-0 items-center gap-2">
|
||
<Badge color={st.color}>{t(st.labelKey)}</Badge>
|
||
{open ? (
|
||
<ChevronUp className="h-4 w-4 text-dim" />
|
||
) : (
|
||
<ChevronDown className="h-4 w-4 text-dim" />
|
||
)}
|
||
</div>
|
||
</div>
|
||
{plannedTotal > 0 && (
|
||
<div className="mt-2 h-[4px] overflow-hidden rounded-full bg-input">
|
||
<div
|
||
className="h-full rounded-full bg-(image:--progress-grad)"
|
||
style={{ width: `${Math.round((plannedDone / plannedTotal) * 100)}%` }}
|
||
/>
|
||
</div>
|
||
)}
|
||
</button>
|
||
{open && (
|
||
<div className="pb-1">
|
||
{s.exercises.length === 0 && (
|
||
<p className="py-3 text-dim text-[0.875rem]">{t('history.noExercises')}</p>
|
||
)}
|
||
{s.exercises.length > 0 && (
|
||
<div className="divide-y divide-line">
|
||
{s.exercises.map((ex) => (
|
||
<div key={ex.id} className="py-3">
|
||
<div className="mb-2 flex items-center justify-between gap-3">
|
||
<div className="flex min-w-0 items-center gap-2">
|
||
<strong className="text-[0.95rem]">
|
||
{localizedName(ex, i18n.language)}
|
||
</strong>
|
||
{ex.isExtra && <Badge color="yellow">{t('history.added')}</Badge>}
|
||
</div>
|
||
<span className="text-dim text-[0.875rem]">{muscleName(ex.muscle)}</span>
|
||
</div>
|
||
{/* Kompaktowe wiersze serii: linia 1 = S{n} + powtórzenia;
|
||
linia 2 = plan/RIR/trudność (tylko istniejące). Bez „× load” —
|
||
faktyczny obciążenie nie istnieje w danych. */}
|
||
<div className="divide-y divide-line">
|
||
{ex.sets.map((set) => (
|
||
<div key={set.index} className="py-1.5">
|
||
<div className="flex items-center gap-2">
|
||
<span className="w-8 shrink-0 text-[0.8125rem] font-semibold text-dim tabular-nums">
|
||
S{set.index + 1}
|
||
</span>
|
||
{set.planned && set.reps == null ? (
|
||
<span className="rounded-full border border-danger/30 bg-danger/10 px-2 py-0.5 text-[0.75rem] font-semibold text-danger">
|
||
{t('history.notDone')}
|
||
</span>
|
||
) : (
|
||
<span className="font-display text-[0.95rem] font-semibold tabular-nums">
|
||
{set.reps ?? '—'}
|
||
<span className="ml-0.5 text-[0.75em] font-semibold text-dim">
|
||
{t('plans:exec.repsUnit')}
|
||
</span>
|
||
</span>
|
||
)}
|
||
</div>
|
||
{(set.planned || set.rir != null || set.difficulty != null) && (
|
||
<div className="mt-0.5 flex flex-wrap items-center gap-x-3 gap-y-0.5 pl-10 text-[0.75rem] text-dim tabular-nums">
|
||
{set.planned && (
|
||
<span>
|
||
{t('history.planPrefix')}
|
||
{set.planned.reps ?? '—'}
|
||
{set.planned.loadKg != null
|
||
? `/${set.planned.loadKg} kg`
|
||
: ''}
|
||
{set.planned.restSec != null
|
||
? `/${set.planned.restSec} s`
|
||
: ''}
|
||
</span>
|
||
)}
|
||
{set.rir != null && <span>RIR {set.rir}</span>}
|
||
{set.difficulty != null && (
|
||
<span>
|
||
{t('history.difficultyWord', { n: set.difficulty })}
|
||
</span>
|
||
)}
|
||
</div>
|
||
)}
|
||
{/* Komentarz trenującego przy serii (wpisany w oknie uzupełniania). */}
|
||
{set.note && (
|
||
<div className="mt-0.5 pl-10 text-[0.75rem] text-dim">
|
||
<MessageSquare className="mr-1 inline h-3 w-3 align-[-2px]" />
|
||
{set.note}
|
||
</div>
|
||
)}
|
||
</div>
|
||
))}
|
||
</div>
|
||
{ex.comments.length > 0 && (
|
||
<div className="mt-2.5 space-y-1.5">
|
||
{ex.comments.map((c) => (
|
||
<div
|
||
key={c.id}
|
||
className="flex items-start gap-2 py-1 text-[0.875rem]"
|
||
>
|
||
<div className="min-w-0 flex-1">
|
||
<span className="font-semibold">{c.authorName}</span>
|
||
<span className="text-dim"> · {fmtDate(c.createdAt)}</span>
|
||
<div>{c.body}</div>
|
||
</div>
|
||
{myUserId && c.authorId === myUserId && onDeleteComment && (
|
||
<button
|
||
type="button"
|
||
aria-label={t('history.deleteCommentAria', {
|
||
author: c.authorName,
|
||
})}
|
||
onClick={() => void onDeleteComment(c.id)}
|
||
className="-m-1 shrink-0 cursor-pointer p-1 text-dim hover:text-danger"
|
||
>
|
||
<Trash2 className="h-4 w-4" />
|
||
</button>
|
||
)}
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
{canComment && onAddComment && (
|
||
<form
|
||
className="mt-2.5 flex flex-col gap-1.5"
|
||
onSubmit={(e) => {
|
||
e.preventDefault();
|
||
void submitComment(s.id, ex.id);
|
||
}}
|
||
>
|
||
<Textarea
|
||
rows={2}
|
||
placeholder={t('history.addComment')}
|
||
value={drafts[ex.id] ?? ''}
|
||
onChange={(e) =>
|
||
setDrafts((d) => ({ ...d, [ex.id]: e.target.value }))
|
||
}
|
||
aria-label={t('history.commentForAria', {
|
||
name: localizedName(ex, i18n.language),
|
||
})}
|
||
/>
|
||
<div className="flex justify-end">
|
||
<Button
|
||
size="sm"
|
||
type="submit"
|
||
disabled={busyKey === ex.id || !(drafts[ex.id] ?? '').trim()}
|
||
>
|
||
<MessageSquarePlus className="h-4 w-4" />
|
||
{t('history.addComment')}
|
||
</Button>
|
||
</div>
|
||
</form>
|
||
)}
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
);
|
||
}
|