Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

7.3 KB
Raw Blame
import { useState } from 'react'
import { Plus, Trash2, ChevronUp, ChevronDown, Dumbbell, Moon, ChevronRight, ChevronLeft as ChevronCollapse } from 'lucide-react'
import DayExercisesEditor from './DayExercisesEditor'
import { types, appendDay, removeDayAt, moveDayBy, patchDayAt, useTranslation } from '@lyftr/shared'
import type { DayDraft } from './types'

interface Props {
  days: DayDraft[]
  onChange: (days: DayDraft[]) => void
  pickerExercises: Record<number, types.Exercise>
  onCacheExercise: (ex: types.Exercise) => void
  wUnit: string
  restSecondsDefault: number
}

// The program's day/rest-day cycle, in order. Add/remove/reorder days here; each
// workout day expands to the (unchanged) per-day exercise editor. The array operations
// come from @lyftr/shared — only the `expanded` row bookkeeping below is web's.
export default function ProgramDaysEditor({ days, onChange, pickerExercises, onCacheExercise, wUnit, restSecondsDefault }: Props) {
  const { t } = useTranslation()
  const [expanded, setExpanded] = useState<number | null>(days.findIndex(d => !d.is_rest_day))

  const addDay = (isRest: boolean) => {
    const next = appendDay(days, isRest)
    onChange(next)
    setExpanded(next.length - 1)
  }

  const removeDay = (idx: number) => {
    onChange(removeDayAt(days, idx))
    setExpanded(null)
  }

  const moveDay = (idx: number, dir: -1 | 1) => {
    const next = moveDayBy(days, idx, dir)
    if (next === days) return // move fell off the end — nothing changed
    onChange(next)
    if (expanded === idx) setExpanded(idx + dir)
    else if (expanded === idx + dir) setExpanded(idx)
  }

  const updateDay = (idx: number, patch: Partial<DayDraft>) => onChange(patchDayAt(days, idx, patch))

  const toggleRest = (idx: number) => {
    const day = days[idx]
    // Switching to rest clears its exercises — a rest day never carries any.
    updateDay(idx, { is_rest_day: !day.is_rest_day, exercises: day.is_rest_day ? day.exercises : [] })
  }

  return (
    <div className="space-y-3">
      {days.length === 0 && (
        <p className="text-xs text-tx-muted text-center py-4">{t('programs.form.daysEditor.empty')}</p>
      )}

      {days.map((day, idx) => {
        const isOpen = expanded === idx
        return (
          <div key={idx} className={`border rounded-lg overflow-hidden ${day.is_rest_day ? 'border-surface-border bg-surface-muted/20' : 'border-surface-border bg-surface-muted/30'}`}>
            <div className="flex items-center gap-2 p-3">
              <div className="flex flex-col flex-shrink-0">
                <button type="button" aria-label={t('programs.form.daysEditor.moveUp')} onClick={() => moveDay(idx, -1)} disabled={idx === 0} className="p-0.5 text-tx-muted hover:text-tx-primary disabled:opacity-20 transition-colors">
                  <ChevronUp className="w-3.5 h-3.5" />
                </button>
                <button type="button" aria-label={t('programs.form.daysEditor.moveDown')} onClick={() => moveDay(idx, 1)} disabled={idx === days.length - 1} className="p-0.5 text-tx-muted hover:text-tx-primary disabled:opacity-20 transition-colors">
                  <ChevronDown className="w-3.5 h-3.5" />
                </button>
              </div>

              <div className={`w-8 h-8 rounded-lg flex items-center justify-center flex-shrink-0 ${day.is_rest_day ? 'bg-surface-muted' : 'bg-brand-500/15'}`}>
                {day.is_rest_day ? <Moon className="w-4 h-4 text-tx-muted" /> : <Dumbbell className="w-4 h-4 text-brand-500" />}
              </div>

              <input
                type="text"
                value={day.name}
                onChange={e => updateDay(idx, { name: e.target.value })}
                placeholder={day.is_rest_day ? t('programs.form.daysEditor.restPlaceholder', { number: idx + 1 }) : t('programs.day.numbered', { number: idx + 1 })}
                className="input text-sm flex-1 min-w-0"
              />

              <div className="flex items-center rounded-lg border border-surface-border overflow-hidden flex-shrink-0">
                <button
                  type="button"
                  onClick={() => day.is_rest_day && toggleRest(idx)}
                  className={`px-2.5 py-1.5 text-xs font-semibold transition-colors ${!day.is_rest_day ? 'bg-brand-500 text-white' : 'bg-transparent text-tx-muted hover:text-tx-secondary'}`}
                >
                  {t('programs.form.daysEditor.workout')}
                </button>
                <button
                  type="button"
                  onClick={() => !day.is_rest_day && toggleRest(idx)}
                  className={`px-2.5 py-1.5 text-xs font-semibold transition-colors ${day.is_rest_day ? 'bg-surface-muted text-tx-primary' : 'bg-transparent text-tx-muted hover:text-tx-secondary'}`}
                >
                  {t('programs.form.daysEditor.rest')}
                </button>
              </div>

              {!day.is_rest_day && (
                <button type="button" aria-label={isOpen ? t('programs.form.daysEditor.collapse') : t('programs.form.daysEditor.expand')} aria-expanded={isOpen} onClick={() => setExpanded(isOpen ? null : idx)} className="p-1.5 hover:bg-surface-muted rounded-lg transition-colors flex-shrink-0">
                  {isOpen ? <ChevronCollapse className="w-4 h-4 text-tx-muted rotate-90" /> : <ChevronRight className="w-4 h-4 text-tx-muted" />}
                </button>
              )}

              <button type="button" aria-label={t('programs.form.daysEditor.remove')} onClick={() => removeDay(idx)} className="p-1.5 hover:bg-error-500/20 rounded transition-colors flex-shrink-0">
                <Trash2 className="w-4 h-4 text-error-400" />
              </button>
            </div>

            {!day.is_rest_day && (
              <div className="px-3 pb-3 flex items-center gap-2 -mt-1">
                <span className="text-[11px] text-tx-muted">{t('common.count.exercises', { count: day.exercises.length })}</span>
              </div>
            )}

            {!day.is_rest_day && isOpen && (
              <div className="px-3 pb-4 pt-1 border-t border-surface-border/60">
                <DayExercisesEditor
                  exercises={day.exercises}
                  onChange={exercises => updateDay(idx, { exercises })}
                  pickerExercises={pickerExercises}
                  onCacheExercise={onCacheExercise}
                  wUnit={wUnit}
                  restSecondsDefault={restSecondsDefault}
                />
              </div>
            )}
          </div>
        )
      })}

      <div className="flex gap-2">
        <button
          type="button"
          onClick={() => addDay(false)}
          className="flex-1 flex items-center justify-center gap-1.5 py-2.5 text-xs bg-brand-500/10 hover:bg-brand-500/15 text-brand-400 border border-brand-500/20 rounded-lg transition-colors font-medium"
        >
          <Plus className="w-3.5 h-3.5" />
          {t('programs.form.daysEditor.addWorkoutDay')}
        </button>
        <button
          type="button"
          onClick={() => addDay(true)}
          className="flex-1 flex items-center justify-center gap-1.5 py-2.5 text-xs bg-surface-muted hover:bg-surface-muted/80 text-tx-secondary border border-surface-border rounded-lg transition-colors font-medium"
        >
          <Moon className="w-3.5 h-3.5" />
          {t('programs.form.daysEditor.addRestDay')}
        </button>
      </div>
    </div>
  )
}