Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.
import { memo } from 'react'
import { Pressable, Text, TextInput, View, type LayoutChangeEvent } from 'react-native'
import { router, type Href } from 'expo-router'
import { CheckCircle2, ChevronLeft, ChevronRight, Flag, Plus, X } from 'lucide-react-native'
import { displayToLbs, displayWeight, sanitizeNumericInput, useNumericText, useTranslation, type ActiveSessionExercise } from '@lyftr/shared'
import { AppText, NUMERIC_ACCESSORY_ID } from '../ui'
import { ExerciseImage } from './ExerciseImage'
import { useTheme } from '../../theme/useTheme'
import { muscleColor, muscleLabel } from '../../utils/exerciseUtils'
const exerciseHref = (id: number) => `/workouts/exercise/${id}` as unknown as Href
const CELL = 'h-11 flex-1 rounded-lg border border-surface-border/60 bg-surface-overlay px-2 text-center font-sans text-base text-tx-primary'
// Both cells of a set row. A component rather than two inline TextInputs because each
// row needs its own text buffer, and these render inside a .map over the sets - a hook
// cannot be called in a loop.
//
// The buffer is half of #141. The parent re-derives `value` from a *number* every
// keystroke, so typing "12." stores Number("12.") === 12, re-renders as "12", and RN
// pushes that back into the native field - deleting the separator before the fractional
// digit can be typed. ExerciseFormCard's WeightCell already had a buffer and says why;
// this one, which backs the *default* List layout, never got the same treatment, so a
// decimal weight could not be entered here by any route.
//
// No selectTextOnFocus, unlike the log/edit form: these are live sets being corrected
// mid-workout, so a tap should land where it was aimed rather than replacing the value.
// That is the case sanitizeNumericInput's first-separator-wins rule exists for - the
// cursor can sit inside a prefilled "82,5".
function SetCell({ value, editable, mode, placeholder, placeholderColor, accessibilityLabel, onChange }: {
value: string
editable: boolean
/** Reps are whole; weight takes one decimal separator. */
mode: 'numeric' | 'decimal'
placeholder: string
placeholderColor: string
accessibilityLabel: string
onChange: (next: string) => void
}) {
const [text, setText] = useNumericText(value)
return (
<TextInput
value={text}
editable={editable}
onChangeText={(t) => {
const v = sanitizeNumericInput(t, mode)
setText(v)
onChange(v)
}}
keyboardType={mode === 'numeric' ? 'number-pad' : 'decimal-pad'}
inputAccessoryViewID={NUMERIC_ACCESSORY_ID}
placeholder={placeholder}
placeholderTextColor={placeholderColor}
className={`${CELL} ${editable ? '' : 'opacity-40'}`}
style={{ fontVariant: ['tabular-nums'] }}
accessibilityLabel={accessibilityLabel}
/>
)
}
interface Props {
/** Position in the session — rendered as-is in callbacks so the owning screen
doesn't bind a fresh closure per card (that would defeat the memo below and
re-render every card on the per-second elapsed-time tick). */
index: number
ex: ActiveSessionExercise
isActive: boolean
isLast: boolean
wUnit: string
weightUnit: 'lbs' | 'kg'
onCardLayout: (index: number, y: number) => void
onRemoveExercise: (index: number) => void
onNotesChange: (index: number, text: string) => void
onAddSet: (index: number) => void
onRemoveSet: (index: number, setIdx: number) => void
onUpdateSet: (index: number, setIdx: number, field: 'actual_reps' | 'actual_weight', value: number) => void
onCompleteSet: (index: number, setIdx: number) => void
onJumpToExercise: (index: number) => void
onRequestFinish: () => void
}
// One exercise inside the active-workout screen: header (thumbnail · name ·
// muscle · remove) + notes + the live set table (reps/weight inputs + complete
// toggle) + prev/add-set/next-or-finish row. Extracted out of active.tsx and
// memoized so the screen's per-second elapsed-time re-render doesn't cascade
// into every exercise's ~2 TextInputs-per-set (same fix as ExerciseFormCard).
function ActiveExerciseCardBase({
index, ex, isActive, isLast, wUnit, weightUnit,
onCardLayout, onRemoveExercise, onNotesChange, onAddSet, onRemoveSet, onUpdateSet, onCompleteSet,
onJumpToExercise, onRequestFinish,
}: Props) {
const { t } = useTranslation()
const { colors, accent } = useTheme()
const allSetsComplete = ex.sets.length > 0 && ex.sets.every((s) => s.completed)
const completedHere = ex.sets.filter((s) => s.completed).length
const tint = muscleColor(ex.exercise.muscle_group)
return (
<View
onLayout={(e: LayoutChangeEvent) => onCardLayout(index, e.nativeEvent.layout.y)}
className={`overflow-hidden rounded-2xl border ${allSetsComplete ? 'border-brand-500/30 bg-brand-500/5' : isActive ? 'border-brand-500/40 bg-surface-base' : 'border-surface-border bg-surface-base'}`}
>
{/* Header */}
<View className="flex-row items-center gap-3 px-4 pb-3 pt-4">
<ExerciseImage url={ex.exercise.image_url} />
<Pressable className="min-w-0 flex-1" onPress={() => router.push(exerciseHref(ex.exercise_id))}>
<AppText variant="bodySemibold" numberOfLines={1}>{ex.exercise.name}</AppText>
<View className="mt-1 flex-row items-center gap-2">
<View className={`rounded px-1.5 py-0.5 ${tint?.chip ?? 'bg-surface-muted'}`}>
<AppText variant="caption" style={{ color: tint?.text ?? colors.txMuted }}>{muscleLabel(ex.exercise.muscle_group)}</AppText>
</View>
<AppText variant="caption" color="muted" style={{ fontVariant: ['tabular-nums'] }}>{t('workouts.active.setProgress', { done: completedHere, total: ex.sets.length })}</AppText>
</View>
</Pressable>
{allSetsComplete ? (
<View className="h-8 w-8 items-center justify-center rounded-full bg-brand-500">
<CheckCircle2 size={18} color="#ffffff" />
</View>
) : (
<Pressable onPress={() => onRemoveExercise(index)} hitSlop={6} accessibilityLabel={t('workouts.active.removeExercise')} className="h-9 w-9 items-center justify-center rounded-xl active:bg-error-500/10">
<X size={16} color={colors.txMuted} />
</Pressable>
)}
</View>
{/* Notes */}
<View className="px-4 pb-2">
<TextInput
defaultValue={ex.notes}
onEndEditing={(e) => onNotesChange(index, e.nativeEvent.text)}
placeholder={t('workouts.active.notes.placeholderMobile')}
placeholderTextColor={colors.txMuted}
className="font-sans text-xs text-tx-secondary"
accessibilityLabel={t('workouts.active.notes.label', { name: ex.exercise.name })}
/>
</View>
{/* Sets */}
<View className="gap-2 px-3 pb-3">
<View className="flex-row items-center gap-2 px-1">
<View className="w-8 items-center"><AppText variant="caption" color="muted">{t('workouts.active.columns.set')}</AppText></View>
<View className="flex-1 items-center"><AppText variant="caption" color="muted">{t('workouts.active.columns.reps')}</AppText></View>
<View className="flex-1 items-center"><AppText variant="caption" color="muted">{t('common.weightWithUnit', { unit: wUnit })}</AppText></View>
<View className="w-12 items-center"><AppText variant="caption" color="muted">{t('workouts.active.columns.done')}</AppText></View>
<View className="w-7" />
</View>
{ex.sets.map((set, setIdx) => {
const isNextSet = isActive && !set.completed && ex.sets.slice(0, setIdx).every((s) => s.completed)
return (
<View
key={setIdx}
className={`flex-row items-center gap-2 rounded-xl border ${set.completed ? 'border-brand-500/20 bg-brand-500/10' : isNextSet ? 'border-brand-500/35 bg-surface-muted/50' : 'border-surface-border/60 bg-surface-muted/30'}`}
>
<View className="w-8 items-center py-3">
<Text className="font-sans-bold text-sm" style={{ color: set.completed ? accent : colors.txMuted, fontVariant: ['tabular-nums'] }}>{set.set_number}</Text>
</View>
<View className="flex-1">
<SetCell
mode="numeric"
editable={!set.completed}
value={set.actual_reps ? String(set.actual_reps) : ''}
onChange={(v) => onUpdateSet(index, setIdx, 'actual_reps', Number(v) || 0)}
placeholder={set.target_reps > 0 ? String(set.target_reps) : '—'}
placeholderColor={colors.txMuted}
accessibilityLabel={t('workouts.active.repsLabel', { number: set.set_number, name: ex.exercise.name })}
/>
</View>
<View className="flex-1">
<SetCell
mode="decimal"
editable={!set.completed}
value={set.actual_weight ? String(displayWeight(set.actual_weight, wUnit)) : ''}
onChange={(v) => onUpdateSet(index, setIdx, 'actual_weight', displayToLbs(Number(v) || 0, weightUnit))}
placeholder={set.target_weight > 0 ? String(displayWeight(set.target_weight, wUnit)) : '—'}
placeholderColor={colors.txMuted}
accessibilityLabel={t('workouts.active.weightLabel', { number: set.set_number, name: ex.exercise.name })}
/>
</View>
<Pressable
onPress={() => onCompleteSet(index, setIdx)}
className={`h-11 w-12 items-center justify-center ${set.completed ? 'bg-brand-500' : isNextSet ? 'bg-brand-500/20' : ''}`}
accessibilityRole="button"
accessibilityLabel={t(set.completed ? 'workouts.active.completedSet' : 'workouts.active.completeSet', { number: set.set_number, name: ex.exercise.name })}
>
<CheckCircle2 size={24} color={set.completed ? '#ffffff' : isNextSet ? accent : colors.txMuted} />
</Pressable>
<Pressable onPress={() => onRemoveSet(index, setIdx)} hitSlop={6} accessibilityLabel={t('workouts.active.removeSet')} className="w-7 items-center justify-center">
<X size={14} color={colors.txMuted} />
</Pressable>
</View>
)
})}
{/* Add Set / Prev / Next / Finish */}
<View className="mt-1 flex-row gap-2">
{isActive && index > 0 ? (
<Pressable onPress={() => onJumpToExercise(index - 1)} accessibilityLabel={t('workouts.active.previous')} className="items-center justify-center rounded-xl border border-surface-border bg-surface-muted px-3 py-2.5 active:scale-95">
<ChevronLeft size={16} color={colors.txSecondary} />
</Pressable>
) : null}
<Pressable onPress={() => onAddSet(index)} className="flex-1 flex-row items-center justify-center gap-1.5 rounded-xl border border-dashed border-surface-border py-2.5 active:opacity-60">
<Plus size={14} color={colors.txMuted} />
<Text className="font-sans-semibold text-xs text-tx-muted">{t('workouts.active.addSet')}</Text>
</Pressable>
{isActive && !isLast ? (
<Pressable onPress={() => onJumpToExercise(index + 1)} className={`flex-row items-center justify-center gap-1 rounded-xl px-3 py-2.5 active:scale-95 ${allSetsComplete ? 'bg-brand-500' : 'border border-surface-border bg-surface-muted'}`}>
<Text className={`font-sans-semibold text-xs ${allSetsComplete ? 'text-white' : 'text-tx-secondary'}`}>{t('workouts.active.next')}</Text>
<ChevronRight size={14} color={allSetsComplete ? '#ffffff' : colors.txSecondary} />
</Pressable>
) : null}
{isActive && isLast ? (
<Pressable onPress={onRequestFinish} className={`flex-row items-center justify-center gap-1 rounded-xl px-3 py-2.5 active:scale-95 ${allSetsComplete ? 'bg-brand-500' : 'border border-surface-border bg-surface-muted'}`}>
<Flag size={14} color={allSetsComplete ? '#ffffff' : colors.txSecondary} />
<Text className={`font-sans-semibold text-xs ${allSetsComplete ? 'text-white' : 'text-tx-secondary'}`}>{t('workouts.active.finish')}</Text>
</Pressable>
) : null}
</View>
</View>
</View>
)
}
export const ActiveExerciseCard = memo(ActiveExerciseCardBase)