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, useEffect, useRef, useCallback } from 'react'
import { createPortal } from 'react-dom'
import { ArrowLeft, Search, Dumbbell } from 'lucide-react'
import { useVirtualizer } from '@tanstack/react-virtual'
import { useServerList, types, useTranslation, hasEquipmentToShow, EXERCISE_PAGE_SIZE, EXERCISE_SEARCH_DEBOUNCE_MS } from '@lyftr/shared'
import ExerciseListEmpty from './ExerciseListEmpty'
import { exerciseAPI } from '../services/api'
import { muscleColorBordered, muscleLabel, equipmentLabel } from '../utils/exerciseUtils'

// How close to the end of the loaded rows to get before fetching the next page.
const PREFETCH_ROWS = 10

interface Props {
  selectedIds: number[]
  onSelect: (exercise: types.Exercise) => void
  onClose: () => void
}

export default function ExercisePicker({ selectedIds, onSelect, onClose }: Props) {
  const { t } = useTranslation()
  const [query, setQuery] = useState('')
  const [debouncedQuery, setDebouncedQuery] = useState('')
  const scrollRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    const timer = setTimeout(() => setDebouncedQuery(query), EXERCISE_SEARCH_DEBOUNCE_MS)
    return () => clearTimeout(timer)
  }, [query])

  // Results come a page at a time from the server, which queries open-exercise-db.
  // Nothing downloads the catalog to filter it here: with ~873 exercises upstream
  // and growing, the browser is the wrong place to do that, and it put the whole
  // library behind one slow request before the picker could render anything.
  const fetcher = useCallback(
    (offset: number, limit: number) =>
      exerciseAPI
        .list({ ...(debouncedQuery ? { q: debouncedQuery } : {}), page: offset / limit + 1 })
        .then(data => data ?? []),
    [debouncedQuery]
  )

  const list = useServerList<types.Exercise>({
    fetcher,
    pageSize: EXERCISE_PAGE_SIZE,
    deps: [debouncedQuery],
  })
  const { items: exercises, loadMore, hasMore, loading, initialLoading, error } = list

  const available = exercises.filter(e => !selectedIds.includes(e.id))

  const virtualizer = useVirtualizer({
    count: available.length,
    getScrollElement: () => scrollRef.current,
    estimateSize: useCallback(() => 64, []),
    overscan: 5,
  })

  const virtualItems = virtualizer.getVirtualItems()

  // Fetch the next page as the last rendered row approaches the end of what we
  // hold. Keyed on the last visible index rather than a scroll handler so it works
  // with the virtualizer's own windowing.
  const lastVisible = virtualItems.length ? virtualItems[virtualItems.length - 1].index : 0
  useEffect(() => {
    if (!hasMore || loading || available.length === 0) return
    if (lastVisible >= available.length - PREFETCH_ROWS) loadMore()
  }, [lastVisible, available.length, hasMore, loading, loadMore])

  return createPortal(
    <div className="fixed inset-0 z-[60] bg-surface-base flex flex-col animate-slide-up">
      {/* Header */}
      <div className="flex items-center gap-3 px-4 pt-4 pb-3 border-b border-surface-border flex-shrink-0 bg-surface-base/95 backdrop-blur">
        <button
          onClick={onClose}
          className="p-2 hover:bg-surface-muted rounded-lg transition-colors flex-shrink-0"
        >
          <ArrowLeft className="w-5 h-5 text-tx-muted" />
        </button>
        <div>
          <h2 className="font-display font-bold text-xl text-tx-primary">{t('exercises.picker.title')}</h2>
          {/* Derived from the rows we actually hold, so it is only true if the fetch
              landed. On a failure `available` is empty and this read "0 loaded" directly
              above a message saying we could not load anything — the same claim the
              error state exists to stop. */}
          {!error && (
            <p className="text-xs text-tx-muted">
              {t(hasMore ? 'exercises.picker.loadedMore' : 'exercises.picker.loaded', { count: available.length })}
            </p>
          )}
        </div>
      </div>

      {/* Search */}
      <div className="px-4 py-3 border-b border-surface-border flex-shrink-0">
        <div className="relative">
          <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-tx-muted pointer-events-none" />
          <input
            type="text"
            value={query}
            onChange={e => setQuery(e.target.value)}
            placeholder={t('exercises.picker.search')}
            className="input pl-10 w-full"
            autoFocus
          />
        </div>
      </div>

      {/* List */}
      <div ref={scrollRef} className="flex-1 overflow-y-auto">
        {initialLoading ? (
          <div className="flex items-center justify-center py-16 text-tx-muted text-sm">
            <Dumbbell className="w-5 h-5 mr-2 animate-pulse text-brand-500" />
            {t('exercises.picker.loading')}
          </div>
        ) : available.length === 0 ? (
          <ExerciseListEmpty query={debouncedQuery} {...list} />
        ) : (
          <div
            className="px-4 py-2 relative"
            style={{ height: virtualizer.getTotalSize() }}
          >
            {virtualItems.map(row => {
              const ex = available[row.index]
              return (
                <div
                  key={ex.id}
                  style={{
                    position: 'absolute',
                    top: row.start,
                    left: 0,
                    right: 0,
                    height: row.size,
                    padding: '0 1rem',
                  }}
                >
                  <button
                    type="button"
                    onClick={() => onSelect(ex)}
                    className="w-full flex items-center gap-3 px-3 py-3 rounded-xl hover:bg-surface-muted transition-colors text-left"
                  >
                    {ex.image_url ? (
                      <img
                        src={ex.image_url}
                        alt=""
                        loading="lazy"
                        className="w-10 h-10 rounded-lg object-cover flex-shrink-0 bg-surface-muted"
                        onError={e => { (e.target as HTMLImageElement).style.display = 'none' }}
                      />
                    ) : (
                      <div className="w-10 h-10 rounded-lg bg-brand-500/10 border border-brand-500/20 flex items-center justify-center flex-shrink-0">
                        <Dumbbell className="w-4 h-4 text-brand-500" />
                      </div>
                    )}
                    <div className="flex-1 min-w-0">
                      <p className="text-sm font-medium text-tx-primary truncate">{ex.name}</p>
                      <div className="flex items-center gap-1.5 mt-0.5 flex-wrap">
                        <span className={`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium border ${muscleColorBordered(ex.muscle_group)}`}>
                          {muscleLabel(ex.muscle_group)}
                        </span>
                        {hasEquipmentToShow(ex.equipment) && (
                          <span className="text-xs text-tx-muted">
                            {equipmentLabel(ex.equipment)}
                          </span>
                        )}
                      </div>
                    </div>
                  </button>
                </div>
              )
            })}
          </div>
        )}
      </div>
    </div>,
    document.body
  )
}