From a437302c5ddb6a66a9f0c8b15b659056db8350cc Mon Sep 17 00:00:00 2001 From: Jack Greenlee Date: Tue, 8 Oct 2024 13:15:26 -0400 Subject: [PATCH] on timeline refresh, recompute "past week" date range Within one app session, the constants TODAY_DATE and INITIAL_DATE_RANGE do not change. On app resume, we call refreshTimeline which sets dateRange to INITIAL_DATE_RANGE. But this is a problem if the app stays running for multiple days because INITIAL_DATE_RANGE is old. Instead we need to recompute the date range, using an up-to-date "today" date. Thus, TODAY_DATE and INITIAL_DATE_RANGE are replaced with functions: getTodayDate and getPastWeekDateRange. --- www/js/TimelineContext.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/www/js/TimelineContext.ts b/www/js/TimelineContext.ts index 9b53d2a27..855f8958f 100644 --- a/www/js/TimelineContext.ts +++ b/www/js/TimelineContext.ts @@ -25,9 +25,12 @@ import useAppStateChange from './useAppStateChange'; import { isoDateRangeToTsRange, isoDateWithOffset } from './datetimeUtil'; import { base_modes } from 'e-mission-common'; -const TODAY_DATE = DateTime.now().toISODate(); +const getTodayDate = () => DateTime.now().toISODate(); // initial date range is the past week: [TODAY - 6 days, TODAY] -const INITIAL_DATE_RANGE: [string, string] = [isoDateWithOffset(TODAY_DATE, -6), TODAY_DATE]; +const getPastWeekDateRange = (): [string, string] => { + const todayDate = getTodayDate(); + return [isoDateWithOffset(todayDate, -6), todayDate]; +}; type ContextProps = { labelOptions: LabelOptions | null; @@ -60,7 +63,7 @@ export const useTimelineContext = (): ContextProps => { // date range (inclusive) that has been loaded into the UI [YYYY-MM-DD, YYYY-MM-DD] const [queriedDateRange, setQueriedDateRange] = useState<[string, string] | null>(null); // date range (inclusive) chosen by datepicker [YYYY-MM-DD, YYYY-MM-DD] - const [dateRange, setDateRange] = useState<[string, string]>(INITIAL_DATE_RANGE); + const [dateRange, setDateRange] = useState<[string, string]>(getPastWeekDateRange); // map of timeline entries (trips, places, untracked time), ids to objects const [timelineMap, setTimelineMap] = useState(null); const [timelineIsLoading, setTimelineIsLoading] = useState('replace'); @@ -184,7 +187,7 @@ export const useTimelineContext = (): ContextProps => { // clamp range to ensure it is within [pipelineStartDate, TODAY_DATE] const clampedDateRange: [string, string] = [ new Date(range[0]) < new Date(pipelineStartDate) ? pipelineStartDate : range[0], - new Date(range[1]) > new Date(TODAY_DATE) ? TODAY_DATE : range[1], + new Date(range[1]) > new Date(getTodayDate()) ? getTodayDate() : range[1], ]; if (clampedDateRange[0] != dateRange?.[0] || clampedDateRange[1] != dateRange?.[1]) { logDebug('Timeline: loadDateRange setting new date range = ' + clampedDateRange); @@ -257,7 +260,7 @@ export const useTimelineContext = (): ContextProps => { try { logDebug('timelineContext: refreshTimeline'); setTimelineIsLoading('replace'); - setDateRange(INITIAL_DATE_RANGE); + setDateRange(getPastWeekDateRange()); setQueriedDateRange(null); setTimelineMap(null); setRefreshTime(new Date());