mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-20 05:56:53 +00:00
fix(mobile): drop stale up-next cards and open the plan on the next day (#2057)
The UP NEXT card kept pointing at this morning's first stop hours after it had started, and at day one of a trip that ended last month. A header promising what comes next, sitting above something that is over. It now stays away on a day that has passed and once today's timed plan has run out — a day whose stops carry no times at all still shows its first stop, there is nothing stale about that. The timeline card already collapses the reserved space when there is no up-next, so the layout closes cleanly. Seeding the plan day also lands on the next day still ahead when today falls into a gap in the dates, instead of dropping back to day one. A trip that is already over keeps opening on its first day. And a day that was selected before the shell mounted now counts as seeded, so clearing the selection is left alone instead of being re-seeded on the next render — which is what the guard was there for in the first place. Based on #2055.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { findTodayDayId, localToday } from './today'
|
||||
import { findFocusDayId, findTodayDayId, localToday } from './today'
|
||||
|
||||
describe('localToday', () => {
|
||||
it('FE-TODAY-001: reads the date off the local clock, not UTC', () => {
|
||||
@@ -44,3 +44,39 @@ describe('findTodayDayId', () => {
|
||||
expect(findTodayDayId([{ id: 7, date: '2026-08-11T00:00:00.000Z' }], at(2026, 8, 11))).toBe(7)
|
||||
})
|
||||
})
|
||||
|
||||
describe('findFocusDayId', () => {
|
||||
const days = [
|
||||
{ id: 1, date: '2026-08-10' },
|
||||
{ id: 2, date: '2026-08-12' },
|
||||
{ id: 3, date: '2026-08-14' },
|
||||
]
|
||||
const at = (y: number, m: number, d: number) => new Date(y, m - 1, d, 10, 0, 0)
|
||||
|
||||
it('FE-TODAY-007: is today while the trip is running', () => {
|
||||
expect(findFocusDayId(days, at(2026, 8, 12))).toBe(2)
|
||||
})
|
||||
|
||||
it('FE-TODAY-008: skips to the next dated day across a gap and before the trip', () => {
|
||||
expect(findFocusDayId(days, at(2026, 8, 11))).toBe(2)
|
||||
expect(findFocusDayId(days, at(2026, 8, 9))).toBe(1)
|
||||
})
|
||||
|
||||
it('FE-TODAY-009: is null once the trip is over, leaving the caller its own fallback', () => {
|
||||
expect(findFocusDayId(days, at(2026, 8, 15))).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-TODAY-010: has nothing to focus without dates', () => {
|
||||
expect(findFocusDayId([{ id: 1, date: null }, { id: 2 }], at(2026, 8, 11))).toBeNull()
|
||||
expect(findFocusDayId([], at(2026, 8, 11))).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-TODAY-011: tolerates full timestamps and leaves the caller’s array alone', () => {
|
||||
const unordered = [
|
||||
{ id: 2, date: '2026-08-12T00:00:00.000Z' },
|
||||
{ id: 1, date: '2026-08-10T00:00:00.000Z' },
|
||||
]
|
||||
expect(findFocusDayId(unordered, at(2026, 8, 11))).toBe(2)
|
||||
expect(unordered.map(d => d.id)).toEqual([2, 1])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -28,3 +28,26 @@ export function findTodayDayId(days: Array<{ id: number; date?: string | null }>
|
||||
const match = days.find(d => typeof d.date === 'string' && d.date.slice(0, 10) === today)
|
||||
return match ? match.id : null
|
||||
}
|
||||
|
||||
/**
|
||||
* The day a single-day view should open on: today while the trip is running,
|
||||
* otherwise the next dated day still ahead. A trip with a gap in its dates —
|
||||
* or one that has not started yet — lands on the day that is actually coming
|
||||
* instead of on day one.
|
||||
*
|
||||
* Null once every dated day has passed, and for an undated itinerary, so the
|
||||
* caller keeps its own fallback: a finished trip opens where it always did,
|
||||
* on its first day.
|
||||
*
|
||||
* The desktop plan shows every day at once and only needs findTodayDayId; the
|
||||
* phone plan is single-day and has to land somewhere, which is the only reason
|
||||
* the two differ. Both read "today" off localToday, so that part cannot drift.
|
||||
*/
|
||||
export function findFocusDayId(days: Array<{ id: number; date?: string | null }>, now?: Date): number | null {
|
||||
const today = localToday(now)
|
||||
const ahead = days
|
||||
.map(d => ({ id: d.id, date: typeof d.date === 'string' ? d.date.slice(0, 10) : '' }))
|
||||
.filter(d => d.date.length === 10 && d.date >= today)
|
||||
.sort((a, b) => a.date.localeCompare(b.date))
|
||||
return ahead[0]?.id ?? null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user