Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

13.3 KB
Raw Blame
import type { ReactNode } from 'react'
import { Car, Fuel, Zap } from 'lucide-react'
import MSheet from '../../../components/MSheet'
import MToggle from '../../../components/MToggle'
import type { MTripSheetsProps } from '../MTripShell'
import { useTranslation } from '../../../../i18n'
import { useSettingsStore } from '../../../../store/settingsStore'
import { useRoadtripSettings } from '../../../../hooks/useRoadtripSettings'
import { useVehicleRange } from '../../../../components/Roadtrip/useVehicleRange'
import { useHotelBookends } from '../../../../components/Roadtrip/useHotelBookends'
import { blockMask } from '../../../../components/Roadtrip/RangeStrip'
import { dayWindow } from '../../../../components/Roadtrip/dayWindow'
import { formatDurationShort, parseAvoid } from '../../../../components/Roadtrip/roadtripModel'
import { valhallaAvailable } from '../../../../components/Map/valhallaRoute'
import { convertDistance, formatDistance, getDistanceUnitLabel } from '../../../../utils/units'
import { Eyebrow, TileHeader } from '../sheets/MTripSheetUi'
import { useIsWide } from '../../../../hooks/sizeClass'
import MRtLimitsEditor from './MRtLimitsEditor'
import type { DistanceUnit } from '../../../../types'

/**
 * The figures the stage plans with, read out loud.
 *
 * The chain says a stretch is too long, or that there is nothing to fill up at before the
 * tank runs dry, and on a phone there was nothing behind those sentences: the numbers they
 * are measured against live in a desktop dialog with seven panels and two columns. Without
 * them a warning is an opinion. So two of those panels come along, the two that decide what
 * the stage is allowed to say, and of a third only the one switch below.
 *
 * The figures cannot be changed here. That is the point rather than a gap: driving limits
 * are set once, at a table, before anybody is in the car, and a form in the passenger seat is
 * a way to break a trip while it is happening. So there is no field, no slider and no
 * greyed-out button that looks like one, and the line under the figures says plainly where
 * they are set and what else that screen can do that this one cannot.
 *
 * One switch is the exception: whether a day starts and ends at the stay. It is no figure and
 * breaks nothing. It moves where a drawn day begins and ends, never a stored stop, and
 * switched back off it gives exactly the stored plan again. It is also the one a traveller
 * on the road reaches for, when the day on the phone sets off from its first place instead
 * of the hotel they woke up in. It writes through the desktop dialog's own save
 * (`useHotelBookends` over the planner's `saveRoadtripLimit`), and stays disabled for a
 * reader who may not edit days. It sits below that line rather than above it, so the
 * sentence about figures set at the desktop never reads as being about the switch too.
 */

/**
 * How many kilometres one block of the bar stands for.
 *
 * The same fifty as `RangeStrip`, whose `blockMask` cuts the bar here too. The mask comes
 * from that module so the two pictures cannot drift apart; the figure the note quotes is
 * the one thing it does not hand out, so it is stated once here.
 */
const BLOCK_KM = 50

/** One value of the driving limits, with the row rule above every row but the first. */
function LimitLine({ label, value, fallback, first }: {
  label: string
  value: string | null
  fallback: string
  first: boolean
}) {
  return (
    <div className={`flex items-center justify-between gap-3 py-2 ${first ? '' : 'border-t border-[color:var(--m-rowbr)]'}`}>
      <span className="min-w-0 flex-1 text-[0.8125rem] text-m-muted">{label}</span>
      <span
        className={`flex-none text-[0.8125rem] font-semibold tabular-nums ${value ? 'text-m-ink' : 'text-m-faint'}`}
      >
        {value ?? fallback}
      </span>
    </div>
  )
}

/** One card of the sheet: eyebrow plus whatever the section is made of. */
function InfoCard({ label, children }: { label: string; children: ReactNode }) {
  return (
    <div className="rounded-[16px] bg-[color:var(--m-ic)] px-3.5 py-3">
      <Eyebrow>{label}</Eyebrow>
      {children}
    </div>
  )
}

/**
 * Driving figures sheet ('rtinfo'): the vehicle's range as the tank it leaves the factory
 * with, and the limits every warning on the stage is measured against. Read-only but for
 * the switch that starts and ends each day at the stay.
 */
export default function MRtInfoSheet({ planner, shell }: MTripSheetsProps) {
  const { t } = useTranslation()
  const open = shell.sheet?.id === 'rtinfo'
  const tripId = planner.tripId

  const unitSetting = useSettingsStore(s => s.settings.distance_unit)
  const unit: DistanceUnit = unitSetting === 'imperial' ? 'imperial' : 'metric'

  // The same range the stage plans with: the vehicle's own figures when they add up,
  // the typed one otherwise. Reading it through the shared hook is what keeps the sheet
  // from becoming a second opinion about the tank.
  const { vehicleKind, rangeKm } = useVehicleRange(tripId)
  const legMinutes = useRoadtripSettings(s => s.roadtrip_leg_minutes, tripId)
  const dayMinutes = useRoadtripSettings(s => s.roadtrip_day_minutes, tripId)
  const dayStart = useRoadtripSettings(s => s.roadtrip_day_start, tripId)
  const dayEnd = useRoadtripSettings(s => s.roadtrip_day_end, tripId)
  const fillPercent = useRoadtripSettings(s => s.roadtrip_fill_percent, tripId)
  const degradation = useRoadtripSettings(s => s.roadtrip_battery_degradation, tripId)
  const avoidRaw = useRoadtripSettings(s => s.roadtrip_avoid, tripId)
  const bookends = useHotelBookends(planner.saveRoadtripLimit, tripId)
  // An iPad or an unfolded Duo is the table the figures are set at: there the limits are a
  // form (MRtLimitsEditor). The phone and the folded Duo keep the read-out below.
  const large = useIsWide()

  const electric = vehicleKind === 'electric'
  const VehicleIcon = electric ? Zap : vehicleKind === 'combustion' ? Fuel : Car

  // Capped where the arithmetic caps it, so no picture can claim a battery is gone. The
  // range above the bar already has this taken off it, which is exactly why it gets said.
  const wear = electric ? Math.min(90, Math.max(0, degradation ?? 0)) : 0
  const fill = fillPercent && fillPercent > 0 && fillPercent < 100 ? fillPercent : 100

  // Split rather than formatted whole, so the unit can sit smaller beside the figure. The
  // rounding is formatDistance's own, because the stage header uses that and the two must
  // not disagree by a tenth.
  const shown = rangeKm ? Math.round(convertDistance(rangeKm, unit) * 10) / 10 : null
  const mask = blockMask(rangeKm)

  const notes = [
    shown === null ? t('roadtrip.limit.rangeEmptyHint') : null,
    shown !== null && fill < 100
      ? t('roadtrip.limit.afterFill', { percent: fill, distance: formatDistance((rangeKm ?? 0) * fill / 100, unit) })
      : null,
    mask ? t('roadtrip.limit.blockNote', { distance: formatDistance(BLOCK_KM, unit) }) : null,
    wear > 0 ? t('roadtrip.limit.wearNote', { percent: wear }) : null,
  ].filter((note): note is string => note !== null)

  const unitLabel = getDistanceUnitLabel(unit)
  const perLabel = t(electric ? 'roadtrip.limit.perCharge' : 'roadtrip.limit.perFill')
  const barLabel = shown === null
    ? t('roadtrip.limit.rangeEmpty')
    : `${shown} ${unitLabel} ${perLabel}`

  // Both times on one line, in the separator the desktop badge already uses. A dash
  // between two clock readings is the one place a phone line breaks in the wrong spot.
  const windowText = dayWindow(dayStart, dayEnd) ? `${dayStart} · ${dayEnd}` : null

  // No second routing engine, no avoidance: the stored classes would be a promise the
  // router cannot keep, so the row says the same "off" it would say with none set.
  const avoiding = parseAvoid(avoidRaw)
  const avoidText = valhallaAvailable() && avoiding.length > 0
    ? t('roadtrip.avoid.badge', { count: avoiding.length })
    : null

  const off = t('roadtrip.limit.off')
  const lines: { key: string; label: string; value: string | null; fallback: string }[] = [
    {
      key: 'leg',
      label: t('roadtrip.limit.legLabel'),
      value: legMinutes ? formatDurationShort(legMinutes * 60) : null,
      fallback: off,
    },
    {
      key: 'day',
      label: t('roadtrip.limit.dayLabel'),
      value: dayMinutes ? formatDurationShort(dayMinutes * 60) : null,
      fallback: off,
    },
    { key: 'window', label: t('roadtrip.window.title'), value: windowText, fallback: off },
    {
      key: 'fill',
      label: t('roadtrip.limit.fillLabel'),
      value: fill < 100 ? `${fill} %` : null,
      // "off" is the wrong word for a tank: not saying how far you fill it means you fill
      // it right up, which is what the arithmetic then does.
      fallback: t('roadtrip.limit.fillFull'),
    },
    { key: 'avoid', label: t('roadtrip.avoid.section'), value: avoidText, fallback: off },
  ]

  return (
    <MSheet open={open} onClose={shell.closeSheet} variant="bottom" material="glass" ariaLabel={t('mobileTrip.rtInfoTitle')}>
      <div className="flex-none px-[18px] pt-4">
        <TileHeader
          icon={<VehicleIcon size={19} strokeWidth={1.8} />}
          title={<span className="truncate">{t('mobileTrip.rtInfoTitle')}</span>}
          onClose={shell.closeSheet}
          closeLabel={t('common.close')}
        />
      </div>

      <div className="m-scroll-fade mt-2 min-h-0 flex-1 overflow-y-auto px-[18px] pb-[18px] pt-[14px]">
        {/* ── Vehicle: how far it goes, drawn as the tank it leaves the factory with ── */}
        <div className="mt-[14px]">
          <InfoCard label={t('roadtrip.limit.sectionVehicle')}>
            <div className="mt-1.5 flex flex-wrap items-baseline justify-between gap-x-3 gap-y-0.5">
              <span className="flex items-baseline gap-1">
                <span className="text-[1.375rem] font-extrabold leading-none tabular-nums text-m-ink">
                  {shown ?? '-'}
                </span>
                {shown !== null && (
                  <span className="text-[0.875rem] font-semibold leading-none text-m-muted">{unitLabel}</span>
                )}
              </span>
              <span className="font-geist text-[0.65625rem] text-m-faint">
                {shown === null ? t('roadtrip.limit.rangeEmpty') : perLabel}
              </span>
            </div>

            <div
              className="mt-2.5 h-2.5 w-full overflow-hidden rounded-full bg-[color:var(--m-trackoff)]"
              role="img"
              aria-label={barLabel}
            >
              {/* Cut as a mask rather than painted over, so the gaps are real holes and
                  the glass behind the sheet shows through them. */}
              <div
                className="flex h-full w-full"
                style={mask ? { maskImage: mask, WebkitMaskImage: mask } : undefined}
                aria-hidden="true"
              >
                <div className="h-full bg-m-act" style={{ width: `${shown === null ? 0 : fill}%` }} />
              </div>
            </div>

            {notes.length > 0 && (
              <div className="mt-2 font-geist text-[0.65625rem] leading-snug text-m-faint">
                {notes.join(' · ')}
              </div>
            )}
          </InfoCard>
        </div>

        {/* ── Limits: what every warning on the stage is measured against ── */}
        {large ? (
          <div className="mt-2.5">
            <MRtLimitsEditor
              tripId={tripId}
              // No save is the planner saying the reader may not edit days, or that the
              // trip's settings are not in yet: the form shows its figures, disabled.
              readOnly={!planner.saveRoadtripLimit}
              onSave={(key, value) => void planner.saveRoadtripLimit?.(key, value)}
              onResetDayBoundaries={planner.resetDayBoundaries}
            />
          </div>
        ) : (
          <>
            <div className="mt-2.5">
              <InfoCard label={t('roadtrip.limit.title')}>
                <div className="mt-1">
                  {lines.map((line, i) => (
                    <LimitLine
                      key={line.key}
                      label={line.label}
                      value={line.value}
                      fallback={line.fallback}
                      first={i === 0}
                    />
                  ))}
                </div>
              </InfoCard>
            </div>

            {/* Under the figures it speaks of, and above the switch it does not. */}
            <p className="mt-2.5 font-geist text-[0.6875rem] leading-snug text-m-faint">
              {t('mobileTrip.rtDesktopNote')}
            </p>
          </>
        )}

        {/* ── The one switch: whether a day starts and ends at the stay ── */}
        <div className="mt-3">
          <InfoCard label={t('roadtrip.line.section')}>
            <div className="mt-1 flex items-center justify-between gap-3 py-2">
              <span className="min-w-0 flex-1 text-[0.8125rem] text-m-muted">{t('roadtrip.line.hotelBookends')}</span>
              <MToggle
                checked={bookends.on}
                onChange={() => bookends.toggle?.()}
                ariaLabel={t('roadtrip.line.hotelBookends')}
                disabled={!bookends.toggle}
              />
            </div>
            <div className="font-geist text-[0.65625rem] leading-snug text-m-faint">
              {t('roadtrip.line.hotelBookendsHint')}
            </div>
          </InfoCard>
        </div>
      </div>
    </MSheet>
  )
}