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.

6.1 KB
Raw Blame
import { useState } from 'react'
import { Modal, Platform, Pressable, View } from 'react-native'
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context'
import DateTimePicker, { type DateTimePickerEvent } from '@react-native-community/datetimepicker'
import { CalendarDays } from 'lucide-react-native'
import { formatDate, useTranslation } from '@lyftr/shared'
import { useTheme } from '../../theme/useTheme'
import { AppText, Label } from './Typography'

interface Props {
  label?: string
  /** ISO calendar date, YYYY-MM-DD — same value shape as web's <input type=date>. */
  value: string
  onChange: (next: string) => void
  /** Upper bound (e.g. new Date() = today) — mirrors the web input's `max`. */
  maximumDate?: Date
}

// new Date('YYYY-MM-DD') parses as UTC midnight, which *displays* as the previous
// day in negative-offset timezones — so the picker round-trips through local date
// parts. The form's payload keeps web's exact `new Date(date).toISOString()`.
const toLocalDate = (v: string): Date => {
  const [y, m, d] = v.split('-').map(Number)
  return new Date(y, (m ?? 1) - 1, d ?? 1)
}
const pad = (n: number) => String(n).padStart(2, '0')
const toValue = (d: Date): string => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`

// Mirrors web ui/DateInput / <input type=date>: a Field-look row that opens the
// platform date picker.
//
// Android's picker is its own self-dismissing dialog — fine to mount inline.
// iOS's inline calendar, however, needs ~320pt of width; when the DateInput sits
// in a half-width form column, an inline picker mounted in-place gets crushed to
// near-zero and looks like "the picker never opened". So on iOS we present it in a
// full-width Modal (its own view hierarchy → nest a SafeAreaProvider, same rule as
// ExercisePicker) where width is unconstrained.
export function DateInput({ label, value, onChange, maximumDate }: Props) {
  const { t } = useTranslation()
  const { colors, accent, isDark } = useTheme()
  const [open, setOpen] = useState(false)
  const date = toLocalDate(value)
  const isIOS = Platform.OS === 'ios'

  // A bound must never exclude the value it is currently showing. The native dialog
  // clamps its initial selection into range, and Android reports that clamped date as
  // a normal 'set' — so a value past the bound gets rewritten to the bound by merely
  // opening the picker and confirming, with the user having chosen nothing.
  //
  // Callers pass `new Date()` ("no future entries"), and an entry *can* sit past the
  // device's today without being in the future: log in UTC+14, fly to UTC-11, and
  // yesterday's entry is dated tomorrow. Silently moving it is the exact day-drift
  // this codebase stores logged_on to prevent.
  const effectiveMax = maximumDate && date > maximumDate ? date : maximumDate

  const handleChange = (event: DateTimePickerEvent, selected?: Date) => {
    // Android fires exactly once (set|dismissed) and the dialog is already gone.
    // On iOS the inline picker stays up (it's in our Modal) — emit on 'set', keep
    // the Modal open so the user can keep scrolling; Done closes it.
    if (event.type === 'set' && selected) onChange(toValue(selected))
    if (!isIOS) setOpen(false)
  }

  return (
    <View className="gap-1.5">
      {label ? <Label>{label}</Label> : null}
      <Pressable
        accessibilityRole="button"
        accessibilityLabel={label ?? t('common.pickDate')}
        onPress={() => setOpen((o) => !o)}
        className="h-12 flex-row items-center gap-2.5 rounded-lg border border-surface-border bg-surface-overlay px-3.5 active:scale-[0.99]"
      >
        <CalendarDays size={16} color={colors.txMuted} />
        <AppText variant="body">{formatDate(date, 'monthDayYear')}</AppText>
      </Pressable>

      {/* Android: mount inline — it's a self-contained dialog. */}
      {open && !isIOS && (
        <DateTimePicker
          value={date}
          mode="date"
          display="default"
          maximumDate={effectiveMax}
          onChange={handleChange}
        />
      )}

      {/* iOS: full-width Modal so the inline calendar isn't crushed by the column.
          Gate the whole Modal on `open` — an RN Modal renders its children even while
          hidden, so an always-mounted DateTimePicker would spin up its native view on
          every screen load; mount it only when the user opens the field. */}
      {isIOS && open && (
        <Modal visible transparent animationType="fade" onRequestClose={() => setOpen(false)}>
          <SafeAreaProvider>
            {/* Tap the scrim to dismiss; the card stops propagation. */}
            <Pressable className="flex-1 justify-end bg-black/40" onPress={() => setOpen(false)}>
              <SafeAreaView edges={['bottom']}>
                <Pressable
                  onPress={(e) => e.stopPropagation()}
                  className="mx-3 mb-3 rounded-2xl border border-surface-border bg-surface-base p-2"
                >
                  <View className="flex-row items-center justify-between px-2 pb-1 pt-2">
                    <AppText variant="bodySemibold">{label ?? t('common.selectDate')}</AppText>
                    <Pressable
                      accessibilityRole="button"
                      onPress={() => setOpen(false)}
                      hitSlop={8}
                      className="px-2 py-1 active:opacity-60"
                    >
                      <AppText variant="bodySemibold" color="brand">{t('common.done')}</AppText>
                    </Pressable>
                  </View>
                  <DateTimePicker
                    value={date}
                    mode="date"
                    display="inline"
                    maximumDate={effectiveMax}
                    onChange={handleChange}
                    themeVariant={isDark ? 'dark' : 'light'}
                    // Tint the selection to brand so it reads on-theme.
                    accentColor={accent}
                  />
                </Pressable>
              </SafeAreaView>
            </Pressable>
          </SafeAreaProvider>
        </Modal>
      )}
    </View>
  )
}