feat(roadtrip): the phone dry band gets the desk's mascot and reserve lamp

"Tank runs out here" on the phone was a pink card with a black pill. It now
has the desktop band's design sized for a thumb: the mascot on its
skateboard and not enjoying it, a small uppercase warning, and the reserve
lamp as a 44px button. While a search runs or its offers show, the lamp
steps aside for a close (nothing in the band could clear the offer pins
before), and an empty answer leaves a retry.

Which control shows, which offers and which empty sentence is decided in
refuelBandState in refuelSuggestion.ts, next to the fuel and battery words.
The desktop RefuelBand reads the same helper now, with no visual change, so
the two shells hold markup only.
This commit is contained in:
Maurice
2026-09-16 22:23:01 +00:00
parent 79450a5380
commit bf03d91345
5 changed files with 416 additions and 85 deletions
@@ -8,7 +8,7 @@ import {
} from 'lucide-react'
import MDancingTrek from '../../mobile/components/MDancingTrek'
import type { RefuelSearch } from './useRefuelSearch'
import type { RefuelCandidate } from './refuelSuggestion'
import { REFUEL_EMPTY_KEY, REFUEL_WORDS, refuelBandState, type RefuelCandidate } from './refuelSuggestion'
import type { DryPoint } from './roadtripModel'
import { useTranslation } from '../../i18n/TranslationContext'
import { Tooltip } from '../shared/Tooltip'
@@ -445,9 +445,10 @@ function RefuelBand({ dry, refuel, dayId, onAsk, onAccept }: {
const { vehicleKind } = useVehicleRange()
const electric = vehicleKind === 'electric'
const DryIcon = electric ? Zap : Fuel
const key = `${dayId}:${dry.legIndex}`
const open = refuel.openFor === key
const settled = open && !refuel.loading && refuel.outcome
const words = REFUEL_WORDS[electric ? 'electric' : 'fuel']
// Which control, which sentence and which offers: decided in refuelSuggestion so the
// phone band reads the same answer instead of keeping its own copy of the conditions.
const band = refuelBandState(refuel, dayId, dry.legIndex)
return (
<div className="min-w-0">
@@ -490,7 +491,7 @@ function RefuelBand({ dry, refuel, dayId, onAsk, onAccept }: {
className="truncate font-geist font-semibold uppercase tracking-[0.16em] text-danger"
style={{ fontSize: FS.label }}
>
{t(electric ? 'roadtrip.refuel.dryElectric' : 'roadtrip.refuel.dry')}
{t(words.dry)}
</div>
{/* How far INTO this leg, where a drive band keeps its figures. Not the
range that was crossed: that is the traveller's own setting, says
@@ -513,7 +514,7 @@ function RefuelBand({ dry, refuel, dayId, onAsk, onAccept }: {
found nothing leaves a button rather than a dead end — the place search
is a shared public service that does time out, and "it did not answer"
with no way to retry reads as broken rather than as busy. */}
{open && (refuel.loading || refuel.results.length) ? (
{band.control === 'close' ? (
<Tooltip label={t('common.close')}>
<button
type="button"
@@ -525,18 +526,14 @@ function RefuelBand({ dry, refuel, dayId, onAsk, onAccept }: {
</button>
</Tooltip>
) : (
<Tooltip label={settled && refuel.outcome !== 'found'
? t('roadtrip.refuel.again')
: t(electric ? 'roadtrip.refuel.findElectric' : 'roadtrip.refuel.find')}>
<Tooltip label={t(band.control === 'again' ? 'roadtrip.refuel.again' : words.find)}>
<button
type="button"
onClick={onAsk}
aria-label={settled && refuel.outcome !== 'found'
? t('roadtrip.refuel.again')
: t(electric ? 'roadtrip.refuel.findElectric' : 'roadtrip.refuel.find')}
aria-label={t(band.control === 'again' ? 'roadtrip.refuel.again' : words.find)}
className="group/fuel grid h-[22px] w-[22px] shrink-0 place-items-center rounded-lg text-danger transition-colors hover:bg-danger-soft focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
>
{settled && refuel.outcome !== 'found'
{band.control === 'again'
? <RotateCcw size={13} strokeWidth={2} aria-hidden />
: (
/* The glow stops under the pointer: a lamp that keeps blinking
@@ -553,12 +550,11 @@ function RefuelBand({ dry, refuel, dayId, onAsk, onAccept }: {
)}
</div>
{open && refuel.loading ? (
{band.loading ? (
<span className="text-content-muted" style={{ fontSize: FS.meta }}>{t('roadtrip.refuel.looking')}</span>
) : null}
{settled ? (
refuel.results.length ? (
{band.offers.length ? (
<ul className="flex flex-col gap-1">
{/* Three at most. This is an offer beside a plan, not a list to browse;
the corridor panel is where somebody goes to see all of them.
@@ -569,7 +565,7 @@ function RefuelBand({ dry, refuel, dayId, onAsk, onAccept }: {
not enough to tell them apart — measured on a real day the top three
came back as "Vattenfall InCharge" three times over, identical but for
a number nobody could see the meaning of. */}
{refuel.results.slice(0, 3).map(poi => {
{band.offers.map(poi => {
const kind = STOP_KIND_BY_KEY[poi.category]
const KindIcon = kind?.Icon ?? Fuel
return (
@@ -632,11 +628,11 @@ function RefuelBand({ dry, refuel, dayId, onAsk, onAccept }: {
</span>
</span>
{onAccept ? (
<Tooltip label={t(electric ? 'roadtrip.refuel.addElectric' : 'roadtrip.refuel.add', { name: poi.name })}>
<Tooltip label={t(words.add, { name: poi.name })}>
<button
type="button"
onClick={() => onAccept(poi)}
aria-label={t(electric ? 'roadtrip.refuel.addElectric' : 'roadtrip.refuel.add', { name: poi.name })}
aria-label={t(words.add, { name: poi.name })}
className="grid h-[22px] w-[22px] shrink-0 place-items-center rounded-lg bg-surface-secondary text-content-secondary transition-colors hover:bg-accent hover:text-accent-text focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
>
<Plus size={13} strokeWidth={2.2} aria-hidden />
@@ -648,18 +644,15 @@ function RefuelBand({ dry, refuel, dayId, onAsk, onAccept }: {
)
})}
</ul>
) : (
/* Three different sentences for three different facts. "Nothing on this
stretch" after a request that failed or was cut short states something
that was never checked, which is worse than saying nothing. */
<span className="text-content-muted" style={{ fontSize: FS.meta }}>
{refuel.outcome === 'none'
? t('roadtrip.refuel.none')
: refuel.outcome === 'incomplete'
? t('roadtrip.refuel.incomplete')
: t('roadtrip.refuel.failed')}
</span>
)
) : null}
{/* Three different sentences for three different facts. "Nothing on this
stretch" after a request that failed or was cut short states something
that was never checked, which is worse than saying nothing. */}
{band.empty ? (
<span className="text-content-muted" style={{ fontSize: FS.meta }}>
{t(REFUEL_EMPTY_KEY[band.empty])}
</span>
) : null}
</div>
</div>
@@ -1,5 +1,8 @@
import { describe, it, expect } from 'vitest'
import { reachableRefuels, outcomeOf } from './refuelSuggestion'
import {
reachableRefuels, outcomeOf, refuelBandState, refuelKey, REFUEL_EMPTY_KEY, REFUEL_OFFER_LIMIT, REFUEL_WORDS,
type RefuelCandidate, type RefuelOutcome, type RefuelSearchView,
} from './refuelSuggestion'
import type { LatLng } from './corridor'
import type { Poi } from '../Map/poiCategories'
@@ -122,3 +125,67 @@ describe('reachableRefuels — one entry per station', () => {
expect(out.map(p => p.name).sort()).toEqual(['Aral', 'Shell'])
})
})
describe('refuelBandState', () => {
const candidate = (name: string): RefuelCandidate =>
({ ...poi(name, 52, 13.5), alongKm: 40, offRouteKm: 0.4, spareKm: 30 })
const search = (over: Partial<RefuelSearchView> = {}): RefuelSearchView =>
({ openFor: null, loading: false, outcome: null, results: [], ...over })
it('FE-ROADTRIP-REFUEL-013: a search filed under another leg leaves this band alone', () => {
// One search is open at a time and it names the dry point it belongs to, so a band
// three legs down the chain must not light up for a question asked about this one.
expect(refuelKey(7, 1)).toBe('7:1')
const band = refuelBandState(search({ openFor: '7:4', outcome: 'found', results: [candidate('a')] }), 7, 1)
expect(band).toEqual({ open: false, loading: false, offers: [], empty: null, control: 'find' })
})
it('FE-ROADTRIP-REFUEL-014: while the request runs the lamp steps aside for the way out', () => {
const band = refuelBandState(search({ openFor: '7:1', loading: true }), 7, 1)
expect(band).toEqual({ open: true, loading: true, offers: [], empty: null, control: 'close' })
})
it('FE-ROADTRIP-REFUEL-015: an answer lists three at most, in the order the search ranked them', () => {
const results = ['a', 'b', 'c', 'd', 'e'].map(candidate)
const band = refuelBandState(search({ openFor: '7:1', outcome: 'found', results }), 7, 1)
expect(REFUEL_OFFER_LIMIT).toBe(3)
expect(band.offers.map(o => o.name)).toEqual(['a', 'b', 'c'])
expect(band.control).toBe('close')
expect(band.empty).toBeNull()
})
it.each<Exclude<RefuelOutcome, 'found'>>(['none', 'incomplete', 'failed'])(
'FE-ROADTRIP-REFUEL-016: an empty answer (%s) says which nothing it was and leaves a retry',
outcome => {
// A retry rather than a dead end: the place search is a shared service that does
// time out, and no answer with no way to ask again reads as broken.
const band = refuelBandState(search({ openFor: '7:1', outcome }), 7, 1)
expect(band).toEqual({ open: true, loading: false, offers: [], empty: outcome, control: 'again' })
expect(REFUEL_EMPTY_KEY[outcome]).toBe(`roadtrip.refuel.${outcome}`)
},
)
it('FE-ROADTRIP-REFUEL-017: asked but nothing back yet keeps the lamp and says nothing', () => {
const band = refuelBandState(search({ openFor: '7:1' }), 7, 1)
expect(band).toEqual({ open: true, loading: false, offers: [], empty: null, control: 'find' })
})
it('FE-ROADTRIP-REFUEL-018: the battery never borrows the tank\'s words', () => {
expect(REFUEL_WORDS.fuel).toEqual({
dry: 'roadtrip.refuel.dry', find: 'roadtrip.refuel.find', add: 'roadtrip.refuel.add',
})
expect(REFUEL_WORDS.electric).toEqual({
dry: 'roadtrip.refuel.dryElectric', find: 'roadtrip.refuel.findElectric', add: 'roadtrip.refuel.addElectric',
})
})
it('FE-ROADTRIP-REFUEL-019: results that arrive before their outcome already offer the way out', () => {
// The close control follows the results rather than the outcome, which is how the
// desktop rail always decided it: once there is something on the map to dismiss,
// the band offers to dismiss it.
const band = refuelBandState(search({ openFor: '7:1', results: [candidate('a')] }), 7, 1)
expect(band.control).toBe('close')
expect(band.offers).toEqual([])
expect(band.empty).toBeNull()
})
})
@@ -121,3 +121,103 @@ function oneEach(sorted: RefuelCandidate[]): RefuelCandidate[] {
}
return kept
}
/**
* How many answers a band lists, on the desktop rail and on the phone alike.
*
* Three at most. This is an offer beside a plan, not a list to browse; the corridor
* search is where somebody goes to see all of them. Named once so the two bands can never
* disagree about what "the offers" are.
*/
export const REFUEL_OFFER_LIMIT = 3
/**
* The name a search is filed under: the card day the band is drawn on and the leg.
*
* One search is open at a time and it names the dry point it belongs to, so a band three
* legs down the chain does not light up for a question asked about this one. The card
* day rather than the day a borrowed stop is stored on, because the answer has to arrive
* under a key the band that asked is watching.
*/
export function refuelKey(dayId: number, legIndex: number): string {
return `${dayId}:${legIndex}`
}
/** What sits in the band's trailing slot: the lamp that goes looking, a retry, or a close. */
export type RefuelControl = 'find' | 'again' | 'close'
/**
* The part of the search a band reads.
*
* Structural rather than the hook's own type, because useRefuelSearch.ts imports this
* module and the pure half should not have to know about the half with the network in it.
*/
export interface RefuelSearchView {
openFor: string | null
loading: boolean
outcome: RefuelOutcome | null
results: readonly RefuelCandidate[]
}
/** Everything a band shows that is not markup, decided once for both shells. */
export interface RefuelBandState {
/** The open search belongs to this band. */
open: boolean
/** This band's question is out and has not come back yet. */
loading: boolean
/** The answers to list, already capped at REFUEL_OFFER_LIMIT; empty until there are some. */
offers: RefuelCandidate[]
/** Which kind of nothing came back, or null while there is no empty answer to report. */
empty: Exclude<RefuelOutcome, 'found'> | null
/** What fills the trailing slot; `refuelBandState` says why each one wins when it does. */
control: RefuelControl
}
/**
* The state of one dry band, given the one search that may be open.
*
* The lamp steps aside while a request runs or offers are showing, for the way to close
* the answer: every press is a real request against a shared service, and a lamp still
* standing there invites a second one nobody asked for. An answer that found nothing
* leaves a retry rather than a dead end, because the place search is a public service
* that does time out, and "it did not answer" with no way to ask again reads as broken
* rather than as busy.
*
* The empty answer keeps three different sentences for three different facts. "Nothing
* on this stretch" after a request that failed or was cut short states something that was
* never checked, which is worse than saying nothing, so anything that is not a plain
* `none` or `incomplete` reads as `failed`.
*/
export function refuelBandState(search: RefuelSearchView, dayId: number, legIndex: number): RefuelBandState {
const open = search.openFor === refuelKey(dayId, legIndex)
const loading = open && search.loading
const settled = open && !search.loading && search.outcome != null
const offers = settled && search.results.length > 0 ? search.results.slice(0, REFUEL_OFFER_LIMIT) : []
let empty: RefuelBandState['empty'] = null
if (settled && offers.length === 0) {
empty = search.outcome === 'none' || search.outcome === 'incomplete' ? search.outcome : 'failed'
}
let control: RefuelControl = 'find'
if (loading || (open && search.results.length > 0)) control = 'close'
else if (settled && search.outcome !== 'found') control = 'again'
return { open, loading, offers, empty, control }
}
/** Which sentence each kind of empty answer gets. */
export const REFUEL_EMPTY_KEY: Record<Exclude<RefuelOutcome, 'found'>, string> = {
none: 'roadtrip.refuel.none',
incomplete: 'roadtrip.refuel.incomplete',
failed: 'roadtrip.refuel.failed',
}
/**
* The band's words for a tank and for a battery.
*
* An electric car does not run out of TANK, and a battery band that talks about fuel is
* the kind of detail that makes the rest look careless. Kept as one table so the title,
* the lamp's label and the offer's label never mix the two vocabularies.
*/
export const REFUEL_WORDS = {
fuel: { dry: 'roadtrip.refuel.dry', find: 'roadtrip.refuel.find', add: 'roadtrip.refuel.add' },
electric: { dry: 'roadtrip.refuel.dryElectric', find: 'roadtrip.refuel.findElectric', add: 'roadtrip.refuel.addElectric' },
} as const