Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.
import { useState, useRef, useEffect } from 'react'
import { Outlet, Link, useLocation, useNavigate } from 'react-router-dom'
import {
Home, Dumbbell, Apple, Scale, BookOpen,
LogOut, Moon, Sun, User,
Timer, ChevronRight,
} from 'lucide-react'
import { useAuthStore } from '../stores/auth'
import { useTheme } from '../hooks/useTheme'
import { useWorkoutSession } from '../stores/workoutSession'
import { useRestTimer } from '../hooks/useRestTimer'
import { fmtClock, formatElapsed, useElapsedSeconds, nameInitial, useTranslation } from '@lyftr/shared'
import { useDisplayName } from '../lib/lyftr'
import { useSettingsStore, weightShort } from '../stores/settings'
import GymModeWorkout from '../pages/GymModeWorkout'
import Logo from './Logo'
import ErrorBoundary from './ErrorBoundary'
const NAV = [
{ path: '/', label: 'nav.home', icon: Home },
{ path: '/workouts', label: 'nav.workouts', icon: Dumbbell },
{ path: '/programs', label: 'nav.programs', icon: BookOpen },
{ path: '/food', label: 'nav.food', icon: Apple },
{ path: '/weight', label: 'nav.weight', icon: Scale },
] as const
function ActiveSessionBar() {
const { t } = useTranslation()
const { session, gymOpen, openGym } = useWorkoutSession()
const { settings } = useSettingsStore()
const navigate = useNavigate()
const { pathname } = useLocation()
const elapsed = useElapsedSeconds(session?.started_at)
// Rest countdown for the minimized workout — the full panel doesn't follow you out
// of the workout; this compact chip does. (useRestTimer also owns the auto-dismiss,
// and this bar is always mounted, so "rest over" clears even while minimized.)
const { active: resting, paused, done, left } = useRestTimer()
if (!session) return null
// Hide pill when gym mode overlay is open or when on active workout page in list mode
if (gymOpen && settings.workout_layout === 'gym') return null
if (pathname === '/workout/active' && settings.workout_layout !== 'gym') return null
const completedSets = session.exercises.reduce((s, ex) => s + ex.sets.filter(set => set.completed).length, 0)
const totalSets = session.exercises.reduce((s, ex) => s + ex.sets.length, 0)
const handleClick = () => {
if (settings.workout_layout === 'gym') {
openGym()
} else {
navigate('/workout/active')
}
}
return (
<div className="absolute bottom-full left-0 right-0 flex justify-center pb-3 pointer-events-none">
<button
onClick={handleClick}
className="pointer-events-auto flex items-center gap-2.5 px-4 py-2.5 bg-brand-500 hover:bg-brand-600 active:scale-95 shadow-lg shadow-brand-500/40 rounded-full transition-all"
>
<div className="w-6 h-6 rounded-full bg-white/20 flex items-center justify-center flex-shrink-0">
<Timer className="w-3 h-3 text-white" />
</div>
<div className="text-left">
<p className="text-xs font-bold text-white leading-tight truncate max-w-[140px]">{session.name}</p>
<p className="text-[11px] text-white/70 leading-tight">{t('workouts.active.setProgress', { done: completedSets, total: totalSets })} · {formatElapsed(elapsed)}</p>
</div>
{resting && (
<div className="flex items-center gap-1.5 pl-2.5 py-1 pr-1 rounded-full bg-white/15 flex-shrink-0">
<span className="text-[10px] font-semibold uppercase tracking-wide text-white/70 leading-none">
{done ? t('workouts.rest.label.over') : paused ? t('workouts.rest.label.paused') : t('nav.session.rest')}
</span>
{!done && <span className="text-xs font-bold text-white tabular-nums leading-none">{fmtClock(left)}</span>}
</div>
)}
<ChevronRight className="w-4 h-4 text-white/80 flex-shrink-0" />
</button>
</div>
)
}
function UserMenu() {
const { t } = useTranslation()
const { user, logout } = useAuthStore()
const { theme, toggleTheme } = useTheme()
const [open, setOpen] = useState(false)
const ref = useRef<HTMLDivElement>(null)
useEffect(() => {
const handler = (e: MouseEvent) => {
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false)
}
document.addEventListener('mousedown', handler)
return () => document.removeEventListener('mousedown', handler)
}, [])
const username = useDisplayName()
const initial = nameInitial(username)
return (
<div ref={ref} className="relative">
<button
onClick={() => setOpen(o => !o)}
className={`w-9 h-9 rounded-full flex items-center justify-center transition-all ${
open
? 'ring-2 ring-brand-500 ring-offset-2 ring-offset-surface-base'
: 'hover:ring-2 hover:ring-surface-border hover:ring-offset-2 hover:ring-offset-surface-base'
}`}
aria-label={t('nav.userMenu')}
>
<div className="w-9 h-9 rounded-full flex items-center justify-center" style={{ background: 'linear-gradient(135deg, #0891b2, #00b8d9)' }}>
<span className="text-sm font-bold text-white leading-none">{initial}</span>
</div>
</button>
{open && (
<div className="absolute right-0 top-full mt-2 w-56 bg-surface-overlay border border-surface-border/60 rounded-2xl shadow-dropdown z-50 animate-slide-up overflow-hidden">
{/* User info */}
<div className="px-4 py-3 border-b border-surface-border/40">
<p className="text-sm font-semibold text-tx-primary truncate">{username}</p>
<p className="text-xs text-tx-muted truncate">{user?.email}</p>
</div>
{/* Actions */}
<div className="py-1.5">
<Link
to="/settings"
onClick={() => setOpen(false)}
className="flex items-center gap-3 px-4 py-2.5 text-sm text-tx-secondary hover:text-tx-primary hover:bg-surface-muted/50 transition-colors"
>
<User className="w-4 h-4 text-tx-muted flex-shrink-0" />
{t('nav.settings')}
</Link>
<button
onClick={toggleTheme}
className="w-full flex items-center gap-3 px-4 py-2.5 text-sm text-tx-secondary hover:text-tx-primary hover:bg-surface-muted/50 transition-colors"
>
{theme === 'dark'
? <><Sun className="w-4 h-4 text-tx-muted flex-shrink-0" />{t('nav.lightMode')}</>
: <><Moon className="w-4 h-4 text-tx-muted flex-shrink-0" />{t('nav.darkMode')}</>
}
</button>
</div>
{/* Sign out */}
<div className="border-t border-surface-border/40 py-1.5">
<button
onClick={() => { logout(); setOpen(false) }}
className="w-full flex items-center gap-3 px-4 py-2.5 text-sm text-error-400 hover:bg-error-500/10 transition-colors"
>
<LogOut className="w-4 h-4 flex-shrink-0" />
{t('nav.signOut')}
</button>
</div>
</div>
)}
</div>
)
}
export default function Layout() {
const { t } = useTranslation()
const { pathname } = useLocation()
const { session, gymOpen } = useWorkoutSession()
const { settings } = useSettingsStore()
const wUnit = weightShort(settings.weight_unit)
// The one condition that decides whether the gym overlay is covering the app.
const gymOverlayUp = !!session && settings.workout_layout === 'gym' && gymOpen
return (
<div className="min-h-screen flex flex-col bg-surface-base">
{/* Opaque, not bg-surface-base/95. That class generated nothing: Tailwind cannot apply
an opacity modifier to a colour held in a CSS variable as a hex, so it was silently
dropped and this bar had no background at all — content scrolled under it behind a
4px blur. Opaque also gives iOS 26 Safari something to read: it tints its toolbar
from the background-color of sticky elements at the viewport edges, and a
transparent one leaves the tint stuck on whatever it sampled at load. */}
<header className="sticky top-0 z-50 border-b border-surface-border bg-surface-base">
<div className="max-w-6xl mx-auto px-5 h-14 flex justify-between items-center">
<Link to="/"><Logo size="md" /></Link>
<UserMenu />
</div>
</header>
{/* inert while the gym overlay is up: aria-modal hides this from assistive tech,
but only inert stops Tab walking into the controls underneath — Finish and
Remove set among them, invisible but clickable. */}
<main
inert={gymOverlayUp}
className="flex-1 max-w-6xl mx-auto w-full min-w-0 overflow-x-hidden px-5 py-7 animate-fade-in"
>
{/* Keyed on the path so the boundary RESETS on navigation: without the key a
crash on one route would follow the user to every other one, turning a bad
row on /workouts into an app that looks broken everywhere. */}
<ErrorBoundary key={pathname}>
<Outlet />
</ErrorBoundary>
</main>
{/* Gym mode overlay — rendered at root so it persists across routes */}
{gymOverlayUp && <GymModeWorkout wUnit={wUnit} />}
{/* Active session pill floats above bottom nav */}
<div className="sticky bottom-0 z-50 relative">
<ActiveSessionBar />
{/* Opaque for the same two reasons as the header above — the /95 variant never
rendered, and Safari tints the bottom toolbar from this element. */}
<nav className="border-t border-surface-border bg-surface-base">
<div className="max-w-6xl mx-auto flex">
{NAV.map(({ path, label, icon: Icon }) => {
const active = pathname === path
return (
<Link key={path} to={path} className={`nav-item flex-1 ${active ? 'active' : ''}`}>
<Icon className="w-5 h-5" strokeWidth={active ? 2.5 : 1.75} />
<span>{t(label)}</span>
</Link>
)
})}
</div>
</nav>
</div>
</div>
)
}