refactor(roadtrip): share the stay length draft between desktop and phone

The stay dialog and the phone stay sheet take their presets, the five minute step, the full day limit, the draft and the arrival and departure preview from useStayDraft. Each still seeds the draft when it opens and saves it its own way; the phone keeps whole minutes when a step moves a stored fraction, the desktop does not.
This commit is contained in:
Maurice
2026-10-09 10:58:06 +02:00
parent 97d3512aa7
commit 745cd5f65d
4 changed files with 158 additions and 99 deletions
@@ -1,16 +1,18 @@
import { useEffect, useMemo, useRef, useState } from 'react'
import { useEffect, useMemo, useRef } from 'react'
import { AlertTriangle, ArrowRight, Minus, Plus } from 'lucide-react'
import MSheet from '../../../components/MSheet'
import MChip from '../../../components/MChip'
import type { MTripSheetsProps } from '../MTripShell'
import { useTranslation } from '../../../../i18n'
import { useSettingsStore } from '../../../../store/settingsStore'
import { formatClock, formatDurationShort, parseClock } from '../../../../components/Roadtrip/roadtripModel'
import { formatDurationShort } from '../../../../components/Roadtrip/roadtripModel'
import { stageOf } from '../../../../components/Roadtrip/roadtripRowModel'
import { isStoredStop } from '@trek/shared/roadtrip'
import { locateStop, missedLeaveOf } from '../../../../components/Roadtrip/stayReading'
import { useLeaveMode, type LeaveMode } from '../../../../components/Roadtrip/useLeaveMode'
import { formatClockTime } from '../../../../utils/formatters'
import {
STAY_MAX as MAX, STAY_PRESETS as PRESETS, STAY_STEP as STEP, clampStay as clampMinutes, useStayDraft,
} from '../../../../components/Roadtrip/useStayDraft'
import { FormSheetHeader } from '../sheets/PlSheetChrome'
import { INNER_CLS, displayTime } from '../sheets/MTripSheetUi'
@@ -25,30 +27,6 @@ interface RtStaySheetPayload {
assignmentId?: number
}
/** The step the two buttons move in. Same five minutes as the desktop dialog. */
const STEP = 5
const DAY_MINUTES = 24 * 60
/**
* As far as a stay goes, in minutes: a full day, like the desktop slider.
*
* Kept even though there is no slider here: the plus button has to stop somewhere, and
* a stay longer than a day is a second day rather than a longer stop.
*/
const MAX = DAY_MINUTES
/**
* The lengths a stop usually takes, so the common answer is one tap.
*
* The same eight the desktop offers. They are the whole of the coarse control here: the
* desktop's slider runs 0 to 1440, which on a 343px phone row is four minutes a pixel,
* and a control that cannot hit the value it is dragged to is worse than no control.
*/
const PRESETS = [15, 30, 45, 60, 90, 120, 480, 720]
const clampMinutes = (value: number): number => Math.min(MAX, Math.max(0, Math.round(value)))
/**
* How long the traveller stays at one stop, on the phone ('rtstay', payload
* { placeId, minutes, name }).
@@ -84,17 +62,6 @@ export default function MRtStaySheet({ planner, shell }: MTripSheetsProps) {
/** What the place carries now: the value the draft starts from and falls back to. */
const stored = clampMinutes(stop?.minutes ?? 0)
const [minutes, setMinutes] = useState(stored)
const [saving, setSaving] = useState(false)
// Reopened on a different stop, so it starts from that stop's own value rather than
// from whatever the last one was left on.
useEffect(() => {
if (!open) return
setMinutes(clampMinutes(payload.minutes ?? 0))
setSaving(false)
}, [open, payload.placeId, payload.minutes])
/**
* When the drive gets here.
*
@@ -117,6 +84,20 @@ export default function MRtStaySheet({ planner, shell }: MTripSheetsProps) {
if (index === -1) return null
return stage.schedule.entries[index]?.arrival ?? null
}, [located, stage, stopPlaceId])
// What the stay does to this stop: the arrival is fixed by the drive, the departure is
// the one end this sheet moves.
const { minutes, setMinutes, saving, setSaving, nudge, preview } = useStayDraft({
arrival, is12h, initial: stored, wholeMinutes: true,
})
// Reopened on a different stop, so it starts from that stop's own value rather than
// from whatever the last one was left on.
useEffect(() => {
if (!open) return
setMinutes(clampMinutes(payload.minutes ?? 0))
setSaving(false)
}, [open, payload.placeId, payload.minutes, setMinutes, setSaving])
const leave = useLeaveMode(located && {
leaveAt: located.stop.leaveAt,
arrival: located.entry?.arrival,
@@ -126,23 +107,6 @@ export default function MRtStaySheet({ planner, shell }: MTripSheetsProps) {
dayId: located.stop.ownerDayId,
})
// What the stay does to this stop: the arrival is fixed by the drive, the departure is
// the one end this sheet moves.
const preview = useMemo(() => {
const at = parseClock(arrival)
if (at === null) return null
return {
arrive: formatClockTime(formatClock(at), is12h),
leave: formatClockTime(formatClock(at + minutes), is12h),
// `formatClock` wraps modulo 24 h, so a stay running past midnight reads as a small
// number again. Without the carry the sheet would quietly promise "leave 01:00" for
// a departure the chain places on the next day.
carry: Math.floor((at + minutes) / DAY_MINUTES) - Math.floor(at / DAY_MINUTES),
}
}, [arrival, minutes, is12h])
const nudge = (delta: number) => setMinutes(m => clampMinutes(m + delta))
/**
* Writes the draft, then hands the traveller back to the stop they came from.
*