Timer implemented. No working tests.

This commit is contained in:
AG
2025-12-10 23:07:31 +02:00
parent 3df4abba47
commit b86664816d
24 changed files with 806 additions and 116 deletions

View File

@@ -7,6 +7,9 @@ import ExerciseModal from '../ExerciseModal';
import { useTracker } from './useTracker';
import SetLogger from './SetLogger';
import { formatSetMetrics } from '../../utils/setFormatting';
import { useAuth } from '../../context/AuthContext';
import { api } from '../../services/api';
import RestTimerFAB from '../ui/RestTimerFAB';
interface ActiveSessionViewProps {
tracker: ReturnType<typeof useTracker>;
@@ -71,6 +74,61 @@ const ActiveSessionView: React.FC<ActiveSessionViewProps> = ({ tracker, activeSe
exercises
} = tracker;
const { currentUser, updateUser } = useAuth();
// Timer Logic is now managed in useTracker to persist across re-renders/step changes
const { timer } = tracker;
const handleLogSet = async () => {
await handleAddSet();
// Determine next rest time
let nextTime = currentUser?.profile?.restTimerDefault || 120;
if (activePlan) {
// Logic: activePlan set just added. We are moving to next step?
// Tracker's handleAddSet calls addSet -> which calls ActiveWorkoutContext's addSet -> which increments currentStepIndex (logic inside context)
// But state update might be async or we might need to look at current index before update?
// Usually we want the rest time AFTER the set we just did.
// The user just configured the set for the *current* step index.
// So we look at activePlan.steps[currentStepIndex].restTime.
// BUT, if the user just finished step 0, and step 0 says "Rest 60s", then we rest 60s.
// If fallback, use default.
// Note: currentStepIndex might update immediately or after render.
// In a real app, we might get the next set's target time? No, rest is usually associated with the fatigue of the set just done.
// Requirement: "rest time after this set".
// So we use currentStepIndex (which likely points to the set we just logged, assuming UI hasn't advanced yet?
// Actually, handleAddSet likely appends set. Context might auto-advance.
// Let's assume we use the restTime of the step that matches the set just logged.
const currentStep = activePlan.steps[currentStepIndex];
if (currentStep && currentStep.restTimeSeconds) {
nextTime = currentStep.restTimeSeconds;
}
}
if (timer.status !== 'RUNNING') {
timer.reset(nextTime);
timer.start();
}
};
const handleDurationChange = async (newVal: number) => {
// Update user profile
try {
await api.patch('/auth/profile', { restTimerDefault: newVal });
if (currentUser) {
updateUser({
...currentUser,
profile: { ...currentUser.profile, restTimerDefault: newVal }
});
}
} catch (e) {
console.error("Failed to update default timer", e);
}
};
const isPlanFinished = activePlan && currentStepIndex >= activePlan.steps.length;
@@ -177,7 +235,7 @@ const ActiveSessionView: React.FC<ActiveSessionViewProps> = ({ tracker, activeSe
<SetLogger
tracker={tracker}
lang={lang}
onLogSet={handleAddSet}
onLogSet={handleLogSet}
/>
{activeSession.sets.length > 0 && (
@@ -397,6 +455,8 @@ const ActiveSessionView: React.FC<ActiveSessionViewProps> = ({ tracker, activeSe
</div>
</div>
)}
<RestTimerFAB timer={timer} onDurationChange={handleDurationChange} />
</div>
);
};