From 54ccb64cb5521d5afa9aa83562f70878705e4ae0 Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Sat, 15 Jul 2023 09:00:37 -0400 Subject: [PATCH] Clean up a bunch of code and eslint warnings (#765) --- .eslintrc.js | 8 +++ common/api/slowzones.ts | 14 ++++-- common/components/inputs/styles/tailwind.tsx | 53 -------------------- common/components/notices/ErrorNotice.tsx | 2 +- common/components/notices/NoDataNotice.tsx | 2 +- common/constants/time.ts | 1 + common/styles/general.ts | 9 ++++ common/types/api.ts | 3 +- common/types/dataPoints.ts | 2 +- common/utils/headways.ts | 6 +-- common/utils/ridership.ts | 49 ------------------ common/utils/slowZoneUtils.ts | 8 --- common/utils/stations.ts | 11 ---- modules/dashboard/HomescreenWidgetTitle.tsx | 2 +- 14 files changed, 38 insertions(+), 132 deletions(-) delete mode 100644 common/components/inputs/styles/tailwind.tsx delete mode 100644 common/utils/ridership.ts diff --git a/.eslintrc.js b/.eslintrc.js index f5ca9bc5a..6aadadd6b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -78,6 +78,14 @@ module.exports = { 'import/no-unused-modules': ['off', { unusedExports: false }], }, }, + // Temporarily don't enforce some rules on types and constants + { + files: ['common/styles/*.ts', 'common/constants/**/*.ts', 'common/types/**/*.ts'], + rules: { + '@typescript-eslint/no-non-null-assertion': 'error', + 'import/no-unused-modules': ['off', { unusedExports: false }], + }, + }, ], ignorePatterns: ['node_modules/**/*', 'build/**/*', 'out/**/*'], }; diff --git a/common/api/slowzones.ts b/common/api/slowzones.ts index 680e79742..875272a20 100644 --- a/common/api/slowzones.ts +++ b/common/api/slowzones.ts @@ -3,7 +3,7 @@ import type { SlowZoneResponse, SpeedRestriction, } from '../../common/types/dataPoints'; -import type { FetchSpeedRestrictionsOptions } from '../types/api'; +import type { FetchSpeedRestrictionsOptions, FetchSpeedRestrictionsResponse } from '../types/api'; import { APP_DATA_BASE_PATH } from '../utils/constants'; import { getGtfsRailLineId } from '../utils/lines'; @@ -28,9 +28,17 @@ export const fetchSpeedRestrictions = async ( ); const today = new Date(); const response = await fetch(speedRestrictionsUrl.toString()); - const { available, date: resolvedDate, zones } = await response.json(); + const { + available, + date: resolvedDate, + zones, + }: FetchSpeedRestrictionsResponse = await response.json(); if (available) { - return zones.map((zone) => ({ ...zone, currentAsOf: resolvedDate, validAsOf: today })); + return zones.map((zone) => ({ + ...zone, + currentAsOf: new Date(resolvedDate), + validAsOf: today, + })); } return []; }; diff --git a/common/components/inputs/styles/tailwind.tsx b/common/components/inputs/styles/tailwind.tsx deleted file mode 100644 index ca2c7110a..000000000 --- a/common/components/inputs/styles/tailwind.tsx +++ /dev/null @@ -1,53 +0,0 @@ -import type { DefaultStyleMap } from '../../../types/styles'; - -export const formConfig = { - default: 'whitespace-nowrap border-b-2 px-1 pb-4 text-sm font-medium', - active: { - red: `bg-mbta-red text-white`, - orange: `bg-mbta-orange text-white`, - green: `bg-mbta-green text-white`, - blue: `bg-mbta-blue text-white`, - bus: `bg-mbta-bus text-white`, - }, -}; - -export const mbtaTextConfig: DefaultStyleMap = { - 'line-red': `text-mbta-red`, - 'line-orange': `text-mbta-orange`, - 'line-green': `text-mbta-green`, - 'line-blue': `text-mbta-blue`, - 'line-bus': `text-mbta-bus`, - DEFAULT: `text-black`, -}; - -export const otherConfig = { - selected: { - red: `text-mbta-red`, - orange: `text-mbta-orange`, - green: `text-mbta-green`, - blue: `text-mbta-blue`, - bus: `text-mbta-bus`, - }, -}; - -export const buttonConfig = { - default: - 'relative w-full cursor-default rounded-md border border-gray-300 bg-white py-2 pl-3 pr-10 text-left shadow-sm focus:outline-none focus:ring-1 sm:text-sm', - red: 'focus:ring-mbta-red focus:border-mbta-red', - orange: 'focus:ring-mbta-orange focus:border-mbta-orange', - green: 'focus:ring-mbta-green focus:border-mbta-green', - blue: 'focus:ring-mbta-blue focus:border-mbta-blue', - bus: 'focus:ring-mbta-bus focus:border-mbta-bus', -}; - -export const dateInputConfig = { - range: { - default: - 'rounded border bg-transparent py-2 px-4 font-medium text-gray-700 hover:border-transparent hover:text-white', - red: 'hover:bg-mbta-red border-mbta-red', - orange: 'hover:bg-mbta-orange border-mbta-orange', - green: 'hover:bg-mbta-green border-mbta-green', - blue: 'hover:bg-mbta-blue border-mbta-blue', - bus: 'hover:bg-mbta-bus border-mbta-bus', - }, -}; diff --git a/common/components/notices/ErrorNotice.tsx b/common/components/notices/ErrorNotice.tsx index f9fc34eec..b997707b1 100644 --- a/common/components/notices/ErrorNotice.tsx +++ b/common/components/notices/ErrorNotice.tsx @@ -3,7 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import classNames from 'classnames'; import React from 'react'; import { useDelimitatedRoute } from '../../utils/router'; -import { mbtaTextConfig } from '../inputs/styles/tailwind'; +import { mbtaTextConfig } from '../../styles/general'; interface ErrorNoticeProps { isWidget?: boolean; diff --git a/common/components/notices/NoDataNotice.tsx b/common/components/notices/NoDataNotice.tsx index c892c9b08..0c3b6f621 100644 --- a/common/components/notices/NoDataNotice.tsx +++ b/common/components/notices/NoDataNotice.tsx @@ -3,7 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import classNames from 'classnames'; import React from 'react'; import { useDelimitatedRoute } from '../../utils/router'; -import { mbtaTextConfig } from '../inputs/styles/tailwind'; +import { mbtaTextConfig } from '../../styles/general'; interface NoDataNoticeProps { isWidget?: boolean; diff --git a/common/constants/time.ts b/common/constants/time.ts index 825a4fc5b..34362475a 100644 --- a/common/constants/time.ts +++ b/common/constants/time.ts @@ -11,3 +11,4 @@ export const TWO_HOURS = 2 * ONE_HOUR; export const THREE_HOURS = 3 * ONE_HOUR; export const FOUR_HOURS = 4 * ONE_HOUR; export const TWELVE_HOURS = 12 * ONE_HOUR; +export const ONE_DAY = 24 * ONE_HOUR; diff --git a/common/styles/general.ts b/common/styles/general.ts index 57350eb60..92300517b 100644 --- a/common/styles/general.ts +++ b/common/styles/general.ts @@ -18,6 +18,15 @@ export const lineColorLightBorder = { DEFAULT: `border-stone-900`, }; +export const mbtaTextConfig: DefaultStyleMap = { + 'line-red': `text-mbta-red`, + 'line-orange': `text-mbta-orange`, + 'line-green': `text-mbta-green`, + 'line-blue': `text-mbta-blue`, + 'line-bus': `text-mbta-bus`, + DEFAULT: `text-black`, +}; + export const lineColorLightBackground: DefaultStyleMap = { 'line-red': `bg-mbta-lightRed`, 'line-orange': `bg-mbta-lightOrange`, diff --git a/common/types/api.ts b/common/types/api.ts index c7a83d32b..e093f3a8b 100644 --- a/common/types/api.ts +++ b/common/types/api.ts @@ -1,4 +1,5 @@ import type { AggType } from '../../modules/speed/constants/speeds'; +import type { SpeedRestriction } from './dataPoints'; import type { Line } from './lines'; export enum QueryNameKeys { @@ -92,5 +93,5 @@ export type FetchSpeedRestrictionsOptions = { export type FetchSpeedRestrictionsResponse = { available: boolean; date: string; - zones: Record[]; + zones: SpeedRestriction[]; }; diff --git a/common/types/dataPoints.ts b/common/types/dataPoints.ts index f4507f2bb..b6f515799 100644 --- a/common/types/dataPoints.ts +++ b/common/types/dataPoints.ts @@ -105,8 +105,8 @@ export type SpeedRestriction = { reported: string; speedMph: number; trackFeet: number; - currentAsOf: Date; lineId: Line; + currentAsOf: Date; validAsOf: Date; }; diff --git a/common/utils/headways.ts b/common/utils/headways.ts index 7e2212fc3..18191f177 100644 --- a/common/utils/headways.ts +++ b/common/utils/headways.ts @@ -32,14 +32,14 @@ export const longestHeadway = (headways: SingleDayDataPoint[] | AggregateDataPoi } }; -export const longestAggregateHeadway = (headways: AggregateDataPoint[]) => { +const longestAggregateHeadway = (headways: AggregateDataPoint[]) => { return headways.reduce( (current, datapoint) => (datapoint.min < current.min ? datapoint : current), headways[0] ); }; -export const longestSingleHeadway = (headways: SingleDayDataPoint[]) => { +const longestSingleHeadway = (headways: SingleDayDataPoint[]) => { return headways.reduce((current, datapoint) => { if (datapoint.headway_time_sec && current.headway_time_sec) return datapoint.headway_time_sec > current.headway_time_sec ? datapoint : current; @@ -47,7 +47,7 @@ export const longestSingleHeadway = (headways: SingleDayDataPoint[]) => { }, headways[0]); }; -export const shortestSingleHeadway = (headways: SingleDayDataPoint[]) => { +const shortestSingleHeadway = (headways: SingleDayDataPoint[]) => { return headways.reduce((current, datapoint) => { if (datapoint.headway_time_sec && current.headway_time_sec) return datapoint.headway_time_sec < current.headway_time_sec ? datapoint : current; diff --git a/common/utils/ridership.ts b/common/utils/ridership.ts deleted file mode 100644 index 9dc78472f..000000000 --- a/common/utils/ridership.ts +++ /dev/null @@ -1,49 +0,0 @@ -import type { LineData, Time } from '../types/ridership'; - -export const MINUTE = 60; -export const HOUR = 60 * MINUTE; -export const DAY = 24 * HOUR; - -export const asPercentString = (p: number) => Math.round(100 * p).toString() + '%'; - -export const getRidershipNoun = (lineId: string) => { - if (['line-Red', 'line-Orange', 'line-Blue', 'line-Green'].includes(lineId)) { - return 'faregate validations'; - } - return 'riders'; -}; - -export const normalizeToPercent = (timeSeries: number[]) => { - const firstValue = timeSeries[0]; - return timeSeries.map((n) => n / firstValue); -}; - -export const stringify12Hour = (time: Time) => { - const hours = Math.floor(time / HOUR); - const isPM = hours >= 12 && hours < 24; - return `${hours > 12 ? hours - 12 : hours === 0 ? 12 : hours} ${isPM ? 'PM' : 'AM'}`; -}; - -export const getHighestTphValue = (lineData: LineData) => { - let max = 0; - Object.entries(lineData.serviceRegimes).forEach(([key, regime]) => { - if (key === 'baseline' || key === 'current') { - Object.values(regime).forEach((serviceLevel) => { - if (serviceLevel.tripsPerHour) { - max = Math.max(max, ...serviceLevel.tripsPerHour); - } - }); - } - }); - return max; -}; - -export const getHourlyTickValues = (periodHours: number, start = 0, end = 24, offset = 0) => { - const values: string[] = []; - for (let i = start; i < end; i++) { - if ((i - offset) % periodHours === 0) { - values.push(stringify12Hour(i * HOUR)); - } - } - return values; -}; diff --git a/common/utils/slowZoneUtils.ts b/common/utils/slowZoneUtils.ts index 019bc6f92..e8fd6500a 100644 --- a/common/utils/slowZoneUtils.ts +++ b/common/utils/slowZoneUtils.ts @@ -57,14 +57,6 @@ export const getStationPairName = (from: Station, to: Station, short?: boolean): return `${from.stop_name}-${to.stop_name}`; }; -const groupByRoute = (data: SlowZone[]) => - data.reduce((series: Record, sz: SlowZone) => { - const key = sz.id; - const s = (series[key] || []).concat(sz); - series[key] = s; - return series; - }, {}); - const groupByLine = (data: SlowZone[]) => data.reduce((series: Record, sz) => { const key = sz.color; diff --git a/common/utils/stations.ts b/common/utils/stations.ts index bf00a4f50..18607fbd2 100644 --- a/common/utils/stations.ts +++ b/common/utils/stations.ts @@ -1,4 +1,3 @@ -import type { SelectOption } from '../../common/types/inputs'; import type { Line, LineShort } from '../../common/types/lines'; import type { Station } from '../../common/types/stations'; import type { Location } from '../types/charts'; @@ -40,16 +39,6 @@ export const optionsStation = (line: LineShort, busRoute?: string): Station[] | return stations[line].stations.sort((a, b) => a.order - b.order); }; -export const swapStations = ( - fromStation: SelectOption | null, - toStation: SelectOption | null, - setFromStation: (fromStation: SelectOption | null) => void, - setToStation: (toStation: SelectOption | null) => void -) => { - setFromStation(toStation); - setToStation(fromStation); -}; - const createStationIndex = () => { const index: Record = {}; Object.values({ ...rtStations, ...busStations }).forEach((line) => { diff --git a/modules/dashboard/HomescreenWidgetTitle.tsx b/modules/dashboard/HomescreenWidgetTitle.tsx index 554fa6a80..f90e86ed5 100644 --- a/modules/dashboard/HomescreenWidgetTitle.tsx +++ b/modules/dashboard/HomescreenWidgetTitle.tsx @@ -3,7 +3,6 @@ import classNames from 'classnames'; import Link from 'next/link'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faChevronRight } from '@fortawesome/free-solid-svg-icons'; -import { mbtaTextConfig } from '../../common/components/inputs/styles/tailwind'; import { useDelimitatedRoute, useGenerateHref, @@ -13,6 +12,7 @@ import { LINE_COLORS } from '../../common/constants/colors'; import type { Page } from '../../common/constants/pages'; import { ALL_PAGES } from '../../common/constants/pages'; import { getSelectedDates } from '../../common/state/utils/dateStoreUtils'; +import { mbtaTextConfig } from '../../common/styles/general'; interface HomescreenWidgetTitle { title: string;