Skip to content

Commit

Permalink
on timeline refresh, recompute "past week" date range
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JGreenlee committed Oct 8, 2024
1 parent 7988b05 commit a437302
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions www/js/TimelineContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<TimelineMap | null>(null);
const [timelineIsLoading, setTimelineIsLoading] = useState<string | false>('replace');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -257,7 +260,7 @@ export const useTimelineContext = (): ContextProps => {
try {
logDebug('timelineContext: refreshTimeline');
setTimelineIsLoading('replace');
setDateRange(INITIAL_DATE_RANGE);
setDateRange(getPastWeekDateRange());

Check warning on line 263 in www/js/TimelineContext.ts

View check run for this annotation

Codecov / codecov/patch

www/js/TimelineContext.ts#L263

Added line #L263 was not covered by tests
setQueriedDateRange(null);
setTimelineMap(null);
setRefreshTime(new Date());
Expand Down

0 comments on commit a437302

Please sign in to comment.