"use client"; import { useState } from "react"; import Link from "next/link"; import { ChevronDown, ChevronUp } from "lucide-react"; import { WorkoutWithSets } from "@/types"; import { formatSetsSummary } from "@/lib/formatSets"; interface WorkoutCardProps { workout: WorkoutWithSets; } export default function WorkoutCard({ workout }: WorkoutCardProps) { const [expanded, setExpanded] = useState(false); // Calculate total volume const totalVolume = workout.setLogs.reduce((sum, set) => { if (set.weight && set.reps) return sum + set.weight * set.reps; return sum; }, 0); const uniqueExercises = new Set(workout.setLogs.map((s) => s.exerciseId)).size; const date = new Date(workout.date); const formattedDate = date.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric", }); const caloriesBurned = (workout as any).caloriesBurned as number | null | undefined; // Build compact stats chips const stats: string[] = []; stats.push(`${uniqueExercises} exercise${uniqueExercises !== 1 ? "s" : ""}`); const totalSets = workout.setLogs.length; stats.push(`${totalSets} set${totalSets !== 1 ? "s" : ""}`); if (totalVolume > 0) stats.push(`${Math.round(totalVolume)} lbs`); if (workout.durationMinutes) stats.push(`${workout.durationMinutes} min`); if (caloriesBurned) stats.push(`${caloriesBurned} cal`); if (workout.difficulty) stats.push(`${workout.difficulty}/10`); // Group sets by exercise const exerciseGroups = (() => { const groups: Array<{ exerciseId: string; exerciseName: string; sets: Array<{ weight?: number | null; reps?: number | null; weightUnit?: string | null }>; setCount: number; }> = []; const indexMap = new Map(); for (const set of workout.setLogs) { const existing = indexMap.get(set.exerciseId); if (existing !== undefined) { groups[existing].sets.push({ weight: set.weight, reps: set.reps, weightUnit: set.weightUnit }); groups[existing].setCount++; } else { indexMap.set(set.exerciseId, groups.length); groups.push({ exerciseId: set.exerciseId, exerciseName: set.exercise.name, sets: [{ weight: set.weight, reps: set.reps, weightUnit: set.weightUnit }], setCount: 1, }); } } return groups; })(); return (
{/* Single-line card */}
{/* Clickable area — navigates to detail page */} {formattedDate} {workout.name || "Unnamed Workout"} · {stats.join(" · ")} {/* Expand/collapse chevron */}
{/* Mobile stats row (visible only on small screens) */}
{stats.join(" · ")}
{/* Expanded exercise detail */} {expanded && (
{exerciseGroups.map((group) => { const summary = formatSetsSummary(group.sets); return (
{group.exerciseName} {group.setCount}s {summary && ( — {summary} )}
); })}
)}
); }