From 9a7bf81f14fe926842d101d67b393ba690b6f4cb Mon Sep 17 00:00:00 2001 From: austinjpaul Date: Tue, 5 Sep 2023 20:39:33 -0400 Subject: [PATCH] Extend slowzone segment link to include 7 days of trailing data. (#857) * Extend slowzone segment link to include 7 days of trailing data. * fix linting --- modules/slowzones/charts/LineSegments.tsx | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/modules/slowzones/charts/LineSegments.tsx b/modules/slowzones/charts/LineSegments.tsx index 773b291b6..114cce220 100644 --- a/modules/slowzones/charts/LineSegments.tsx +++ b/modules/slowzones/charts/LineSegments.tsx @@ -3,10 +3,16 @@ import { Bar } from 'react-chartjs-2'; import React, { useMemo, useRef } from 'react'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; +import minMax from 'dayjs/plugin/minMax'; import ChartjsPluginWatermark from 'chartjs-plugin-watermark'; import type { ChartDataset } from 'chart.js'; -import { DATE_FORMAT, YESTERDAY_MIDNIGHT, YESTERDAY_STRING } from '../../../common/constants/dates'; +import { + DATE_FORMAT, + YESTERDAY_MIDNIGHT, + YESTERDAY_STRING, + TODAY, +} from '../../../common/constants/dates'; import { COLORS } from '../../../common/constants/colors'; import type { Direction, LineSegmentData, SlowZone } from '../../../common/types/dataPoints'; import type { LinePath } from '../../../common/types/lines'; @@ -24,6 +30,7 @@ import { stopIdsForStations } from '../../../common/utils/stations'; import { ALL_PAGES } from '../../../common/constants/pages'; import type { QueryParams } from '../../../common/types/router'; dayjs.extend(utc); +dayjs.extend(minMax); interface LineSegmentsProps { data: SlowZone[]; @@ -128,7 +135,7 @@ export const LineSegments: React.FC = ({ const queryParams: QueryParams = { // Show 7 days before slowzone start for comparison startDate: dayjs(x[0]).subtract(7, 'days').format(DATE_FORMAT), - endDate: x[1], + endDate: dayjs.min(TODAY, dayjs(x[1]).add(7, 'days'))?.format(DATE_FORMAT), to: stations.toStopIds?.[0], from: stations.fromStopIds?.[0], }; @@ -170,11 +177,12 @@ export const LineSegments: React.FC = ({ if (!(start && end)) return 'Unknown dates'; const startUTC = dayjs.utc(start); const endUTC = dayjs.utc(end); - return `${startUTC.format('MMM D, YYYY')} - ${ - dayjs.utc(endUTC).isSame(YESTERDAY_MIDNIGHT) - ? 'Ongoing' - : dayjs(endUTC).format('MMM D, YYYY') - }`; + return `${startUTC.format('MMM D, YYYY')} - + ${ + dayjs.utc(endUTC).isSame(YESTERDAY_MIDNIGHT) + ? 'Ongoing' + : dayjs(endUTC).format('MMM D, YYYY') + }`; }, }, },