'use client'; import React, { useState } from 'react'; import { useRouter } from 'next/navigation'; import { motion, AnimatePresence } from 'framer-motion'; import { Plus, Trash2, PlayCircle, ArrowLeft, Copy } from 'lucide-react'; import Link from 'next/link'; interface ExerciseItem { id: string; name: string; reps: number; weight: number; rest_time_seconds: number; } interface RoundData { id: string; exercises: ExerciseItem[]; } export default function FreeModeBuilder() { const router = useRouter(); // --- Round-Based State --- const [rounds, setRounds] = useState([ { id: 'round-1', exercises: [ { id: 'ex-1', name: 'Squat', reps: 10, weight: 20, rest_time_seconds: 30 } ] } ]); // --- Actions --- const addRound = () => { setRounds([...rounds, { id: Math.random().toString(36).substr(2, 9), exercises: [] }]); }; const duplicateRound = (sourceIndex: number) => { const source = rounds[sourceIndex]; const newExercises = source.exercises.map(ex => ({ ...ex, id: Math.random().toString(36).substr(2, 9) })); // Insert after the source round const newRounds = [...rounds]; newRounds.splice(sourceIndex + 1, 0, { id: Math.random().toString(36).substr(2, 9), exercises: newExercises }); setRounds(newRounds); }; const removeRound = (index: number) => { setRounds(rounds.filter((_, i) => i !== index)); }; const addExerciseToRound = (roundIndex: number) => { const newRounds = [...rounds]; newRounds[roundIndex].exercises.push({ id: Math.random().toString(36).substr(2, 9), name: 'Squat', reps: 10, weight: 10, rest_time_seconds: 30 }); setRounds(newRounds); }; const removeExerciseFromRound = (roundIndex: number, exIndex: number) => { const newRounds = [...rounds]; newRounds[roundIndex].exercises = newRounds[roundIndex].exercises.filter((_, i) => i !== exIndex); setRounds(newRounds); }; const updateExercise = (roundIndex: number, exIndex: number, field: keyof ExerciseItem, value: any) => { const newRounds = [...rounds]; newRounds[roundIndex].exercises[exIndex] = { ...newRounds[roundIndex].exercises[exIndex], [field]: value }; setRounds(newRounds); }; const startTraining = () => { if (rounds.length === 0) return; if (rounds.every(r => r.exercises.length === 0)) return; // Flatten Logic: Expand Rounds into Linear List // Matches Coach App logic exactly const flatList: any[] = []; const counts: Record = {}; const totals: Record = {}; // 1. Calculate Totals (First Pass) rounds.forEach(round => { round.exercises.forEach(ex => { totals[ex.name] = (totals[ex.name] || 0) + 1; }); }); // 2. Flatten and Assign Indices rounds.forEach((round) => { round.exercises.forEach(ex => { counts[ex.name] = (counts[ex.name] || 0) + 1; flatList.push({ name: ex.name, reps: ex.reps, weight: ex.weight, rest_time_seconds: ex.rest_time_seconds, set_index: counts[ex.name], total_sets: totals[ex.name] }); }); }); // Save to LocalStorage const freeMenu = { id: 'free-mode', name: 'Free Session', exercises: flatList }; localStorage.setItem('straps_free_mode_menu', JSON.stringify(freeMenu)); // Redirect router.push('/client/training?mode=free'); }; const EXERCISE_OPTIONS = [ "Bicep Curl", "Hammer Curl", "Squat", "Deadlift", "Lunges", "Overhead Press", "Lateral Raises" ]; return (

Free Style Composer

Design training blocks set-by-set.

{rounds.map((round, roundIndex) => ( {/* Round Header */}

#{ (roundIndex + 1).toString().padStart(2, '0') } SET/GROUP

{/* Exercises List */}
{round.exercises.map((ex, exIndex) => (
{/* Name */}
{/* Kg */}
updateExercise(roundIndex, exIndex, 'weight', parseFloat(e.target.value))} className="w-full bg-zinc-50 border border-zinc-100 rounded-lg px-2 py-2 text-center font-mono text-sm focus:border-primary outline-none" />
{/* Reps */}
updateExercise(roundIndex, exIndex, 'reps', parseInt(e.target.value))} className="w-full bg-zinc-50 border border-zinc-100 rounded-lg px-2 py-2 text-center font-mono text-sm focus:border-primary outline-none" />
{/* Rest */}
updateExercise(roundIndex, exIndex, 'rest_time_seconds', parseFloat(e.target.value))} className="w-full bg-zinc-50 border border-zinc-100 rounded-lg px-2 py-2 text-center font-mono text-sm focus:border-primary outline-none" />
{/* Remove Exercise */}
))}
))}
); }