Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.
import { useCallback } from 'react'
import { Pause, Play, SkipForward, Check } from 'lucide-react'
import { useWorkoutSession } from '../stores/workoutSession'
import { useRestTimer } from '../hooks/useRestTimer'
import { fmtClock, nextIncompleteSet, useTranslation } from '@lyftr/shared'
import { IconButton } from './ui'
// Hevy/Strong-style rest panel: a thin draining progress line, a big centred
// countdown with a pause/resume toggle, and full-width −15/+15/Skip actions.
// Shown only INSIDE the workout — docked in-flow as the last child of every gym phase,
// below that phase's bottom actions, so it pushes content up and never covers a control.
// When the workout is minimized the countdown moves to a compact chip in the session
// pill (see Layout's ActiveSessionBar) instead of following you around the app.
// Haptics/sound are left to a future native app.
export default function RestTimerBanner() {
const { t } = useTranslation()
const {
restEndsAt, restDurationSec, restPausedRemainingMs, restExIdx, restSetIdx,
session, adjustRest, clearRest, pauseRest, resumeRest,
} = useWorkoutSession()
const { active, paused, done, left } = useRestTimer()
// Drain the progress line over the *actual* remaining time (one linear CSS
// transition), not stepping per second. A callback ref (not an effect) so it
// fires on the real mount — the bar only renders after restEndsAt is set, which
// an effect keyed on restEndsAt would miss. Re-armed when the ref identity
// changes (start, ±15s, pause/resume — restPausedRemainingMs is in the deps).
const setupLine = useCallback((el: HTMLDivElement | null) => {
if (!el) return
const total = Math.max(1, (restDurationSec ?? 0) * 1000)
// Paused: freeze the line at the parked fraction, no transition.
if (restPausedRemainingMs != null) {
el.style.transition = 'none'
el.style.width = `${Math.min(100, (restPausedRemainingMs / total) * 100)}%`
return
}
if (restEndsAt == null) return
const remaining = Math.max(0, restEndsAt - Date.now())
el.style.transition = 'none'
el.style.width = `${Math.min(100, (remaining / total) * 100)}%`
void el.offsetWidth // reflow so the next change animates from here
el.style.transition = `width ${remaining}ms linear`
el.style.width = '0%'
}, [restEndsAt, restDurationSec, restPausedRemainingMs])
// Hidden when no rest is running or paused (auto-dismiss lives in useRestTimer).
if (!active) return null
// Set ↔ timer linkage (Hevy-style): the store records which set started this rest
// (restExIdx/restSetIdx = the just-completed set); label the panel with that set
// + the next incomplete one (mirrors gym-mode's auto-advance) so it's clear which
// set is resting and which is up next.
const restEx = restExIdx != null ? session?.exercises[restExIdx] : undefined
const nextIdx = restEx != null && restSetIdx != null ? nextIncompleteSet(restEx.sets, restSetIdx) : -1
const doneSetNum = restEx != null && restSetIdx != null ? restSetIdx + 1 : null
// Once rest is over the headline says "Rest over", so the caption carries the
// thing you actually need next instead of repeating it.
const label = doneSetNum == null
? (done ? t('workouts.rest.label.ready') : paused ? t('workouts.rest.label.paused') : t('workouts.rest.label.resting'))
: done
? (nextIdx !== -1 ? t('workouts.rest.label.upNext', { next: nextIdx + 1 }) : t('workouts.rest.label.setDone', { done: doneSetNum }))
: paused
? t('workouts.rest.label.setDonePaused', { done: doneSetNum })
: nextIdx !== -1
? t('workouts.rest.label.setDoneNext', { done: doneSetNum, next: nextIdx + 1 })
: t('workouts.rest.label.setDoneResting', { done: doneSetNum })
const secBtn = 'flex-1 flex items-center justify-center py-2.5 rounded-xl text-sm font-semibold tabular-nums bg-surface-muted border border-surface-border text-tx-secondary active:scale-95'
const brandBtn = 'flex-1 flex items-center justify-center gap-1.5 py-2.5 rounded-xl text-sm font-semibold bg-brand-500 text-white active:scale-95'
// Done is the only control in its row, so it has to size itself: `flex-1` does
// nothing outside a flex parent, and a lone <button> shrink-to-fits even at
// display:flex. Same height and radius as the resting row so the panel doesn't
// change height the moment the countdown ends.
const doneBtn = 'w-full flex items-center justify-center gap-1.5 py-2.5 rounded-xl text-sm font-semibold bg-brand-500 text-white active:scale-95'
const body = (
<>
{/* draining count-down line */}
{done ? (
<div className="h-1 bg-success-500" />
) : (
<div className="h-1 bg-surface-muted">
<div ref={setupLine} className="h-full rounded-r-full bg-brand-500" />
</div>
)}
<div className="px-4 pt-2 pb-3">
<p className="text-[11px] font-medium text-tx-muted text-center leading-none">{label}</p>
{/* Big countdown + pause/resume toggle (the panel's signature element). Once
rest is over the clock reads 0:00, which is dead information, so the slot
states the outcome instead. min-h pins both states to the same height so
the docked panel doesn't shift the set content when the timer flips. */}
<div className="flex items-center justify-center gap-3 my-2 min-h-[3rem]">
{done ? (
<>
<Check className="w-7 h-7 text-success-500" />
<span className="font-display text-3xl font-black text-tx-primary leading-none">{t('workouts.rest.label.over')}</span>
</>
) : (
<>
<IconButton
icon={paused ? Play : Pause}
label={paused ? t('workouts.rest.resume') : t('workouts.rest.pause')}
onClick={paused ? resumeRest : pauseRest}
variant="ghost" size="lg"
className="!text-brand-500 hover:!text-brand-500"
/>
<span className="font-display text-4xl font-black tabular-nums text-tx-primary leading-none">{fmtClock(left)}</span>
</>
)}
</div>
{/* full-width actions */}
{done ? (
<button onClick={() => clearRest()} className={doneBtn}><Check className="w-4 h-4" />{t('common.done')}</button>
) : (
<div className="flex gap-2">
<button onClick={() => adjustRest(-15)} aria-label={t('workouts.rest.shorten')} className={secBtn}>−15</button>
<button onClick={() => adjustRest(15)} aria-label={t('workouts.rest.extend')} className={secBtn}>+15</button>
<button onClick={() => clearRest()} className={brandBtn}><SkipForward className="w-4 h-4" />{t('workouts.rest.skip')}</button>
</div>
)}
</div>
</>
)
// In-flow block at the bottom of the gym phase column, so the flex layout pushes
// the content up above it (nothing is covered).
return (
<div className="flex-shrink-0 bg-surface-raised border-t border-surface-border animate-slide-up">
<div className="max-w-md mx-auto">{body}</div>
</div>
)
}