Skip to content

Commit

Permalink
Improve time formatting (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinmatte authored Feb 17, 2024
1 parent aef251a commit 288707c
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 39 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ module.exports = {
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
'prettier',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react', 'react-hooks', '@typescript-eslint', 'import', 'react-refresh'],
plugins: ['react', 'react-hooks', '@typescript-eslint', 'import', 'react-refresh', 'prettier'],
rules: {
'import/named': 'warn',
'import/no-self-import': 'error',
Expand All @@ -28,6 +29,7 @@ module.exports = {
ignoreTypeImports: false,
},
],
'prettier/prettier': 'error',
'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules
dist
dist-ssr
*.local
*.pyc

# Editor directories and files
.vscode/*
Expand Down
98 changes: 94 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
"@vitejs/plugin-react": "^4.2.1",
"autoprefixer": "^10.4.16",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.2",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"postcss": "^8.4.33",
"prettier": "3.2.1",
"prettier": "3.2.5",
"tailwindcss": "^3.4.1",
"typescript": "^5.2.2",
"vite": "^5.0.8"
Expand Down
8 changes: 2 additions & 6 deletions src/api/traveltimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ export const fetchAggregateData = async (
return name === QueryNameKeys.traveltimes ? responseJson : { by_date: responseJson };
};

export const useTripExplorerQueries = (
parameters: AggregateAPIOptions,
enabled = true
) => {
export const useTripExplorerQueries = (parameters: AggregateAPIOptions, enabled = true) => {
const queryTypes = [QueryNameKeys.traveltimes, QueryNameKeys.headways, QueryNameKeys.dwells];
const dependencies = aggregateQueryDependencies;
// Create objects with keys of query names which contains keys and parameters.
Expand All @@ -78,8 +75,7 @@ export const useTripExplorerQueries = (
queries: queryTypes.map((name) => {
return {
queryKey: [name, queries[name].params],
queryFn: () =>
fetchAggregateData(name, queries[name].params),
queryFn: () => fetchAggregateData(name, queries[name].params),
enabled,
staleTime: ONE_MINUTE,
};
Expand Down
1 change: 0 additions & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const QUERIES: { [key in RouteType]: QueryNameOptions[] } = {
cr: [QueryNameKeys.traveltimes, QueryNameKeys.headways],
};


export enum AggregateAPIParams {
stop = 'stop',
fromStop = 'from_stop',
Expand Down
15 changes: 15 additions & 0 deletions src/components/DirectionIndicator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ArrowDownCircleIcon } from '@heroicons/react/24/solid';
import { ArrowUpCircleIcon } from '@heroicons/react/24/solid';
import React from 'react';

interface DirectionIndicatorProps {
direction?: 'up' | 'down';
}

export const DirectionIndicator: React.FC<DirectionIndicatorProps> = ({ direction }) => {
if (direction === 'up') {
return <ArrowUpCircleIcon className={`ml-1 h-6 w-6 inline-block text-red-500`} />;
} else if (direction === 'down') {
return <ArrowDownCircleIcon className={`ml-1 h-6 w-6 inline-block text-green-500`} />;
}
};
29 changes: 15 additions & 14 deletions src/components/Shutdowns/ChartContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Shutdown } from '../../types';
import TravelTimesChart from '../charts/TravelTimesChart';
import { AggregateDataResponse } from '../charts/types';
import { useBreakpoint } from '../../hooks/useBreakpoint';
import { getFormattedTimeValue } from '../../utils/time';

interface ChartContainerProps {
shutdown: Shutdown;
Expand All @@ -19,13 +20,13 @@ const ChartContainer = ({ before, after, shutdown, title }: ChartContainerProps)
const beforeData = before.data!.by_date.filter((datapoint) => datapoint.peak === 'all');
const afterData = after.data!.by_date.filter((datapoint) => datapoint.peak === 'all');

const beforeAvg = (beforeData.reduce((a, b) => a + b['50%'], 0) / beforeData.length / 60).toFixed(
2
);
const beforeAvg = beforeData.reduce((a, b) => a + b['50%'], 0) / beforeData.length;

const afterAvg = afterData.reduce((a, b) => a + b['50%'], 0) / afterData.length;

const afterAvg = (afterData.reduce((a, b) => a + b['50%'], 0) / afterData.length / 60).toFixed(2);
const difference = Number(afterAvg) - Number(beforeAvg);
const direction = !isNaN(difference) ? (beforeAvg > afterAvg ? 'down' : 'up') : undefined;

const difference = (Number(afterAvg) - Number(beforeAvg)).toFixed(2);
return (
<div className="flex md:flex-row flex-col gap-4 ">
<div className={`flex-1 ${cardStyles}`}>
Expand All @@ -36,26 +37,26 @@ const ChartContainer = ({ before, after, shutdown, title }: ChartContainerProps)
</div>
<div className="flex md:flex-col flex-row md:gap-4 gap-2 ">
<div className={`flex flex-col flex-1 ${cardStyles} justify-center text-center`}>
<dt className="md:truncate text-sm font-medium text-gray-500 align-middle ">
<dt className="md:truncate text-sm font-medium text-gray-700 dark:text-white align-middle ">
{!isMobile ? 'Before' : 'Before shutdown'}
</dt>
<dd className="mt-1 text-xl md:text-3xl font-semibold tracking-tight text-gray-900">
{beforeAvg}
<dd className="mt-1 text-xl md:text-3xl font-semibold tracking-tight">
{getFormattedTimeValue(beforeAvg, true)}
</dd>
</div>
<div className={`flex flex-col flex-1 ${cardStyles} justify-center text-center`}>
<dt className="md:truncate text-sm font-medium text-gray-500">
<dt className="md:truncate text-sm font-medium text-gray-700 dark:text-white">
{' '}
{!isMobile ? 'After' : 'After shutdown'}
</dt>
<dd className="mt-1 text-xl md:text-3xl font-semibold tracking-tight text-gray-900">
{afterAvg}
<dd className="mt-1 text-xl md:text-3xl font-semibold tracking-tight">
{getFormattedTimeValue(afterAvg, true)}
</dd>
</div>
<div className={`flex flex-col flex-1 ${cardStyles} justify-center text-center`}>
<dt className="md:truncate text-sm font-medium text-gray-500">Change</dt>
<dd className="mt-1 text-xl md:text-3xl font-semibold tracking-tight text-gray-900">
{difference}
<dt className="md:truncate text-sm font-medium text-gray-700 dark:text-white">Change</dt>
<dd className="mt-1 text-xl md:text-3xl font-semibold tracking-tight">
{getFormattedTimeValue(difference, true, direction)}
</dd>
</div>
</div>
Expand Down
15 changes: 15 additions & 0 deletions src/components/UnitText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import classNames from 'classnames';
import React from 'react';

interface UnitTextProps {
text: string;
isLarge?: boolean;
}

export const UnitText: React.FC<UnitTextProps> = ({ text, isLarge = false }) => {
return (
<span className={classNames('text-gray-700 dark:text-white', isLarge ? 'text-sm' : 'text-xs')}>
{text}
</span>
);
};
15 changes: 15 additions & 0 deletions src/components/WidgetText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import classNames from 'classnames';
import React from 'react';

interface WidgetTextProps {
text: string;
isLarge?: boolean;
}

export const WidgetText: React.FC<WidgetTextProps> = ({ text, isLarge = false }) => {
return (
<span className={classNames('text-gray-700 dark:text-white', isLarge ? 'text-2xl' : 'text-sm')}>
{text}
</span>
);
};
4 changes: 3 additions & 1 deletion src/components/charts/AggregateLineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { writeError } from '../../utils/chartError';
import { CHART_COLORS } from '../../constants/colors';
import { watermarkLayout } from '../../utils/watermark';
import { AggregateDataPoint } from '../../api/types';
import { AggregateLineProps } from './types';
import { useStore } from '../../store';
import { AggregateLineProps } from './types';

const xAxisLabel = (startDate: string, endDate: string, hourly: boolean) => {
if (hourly) {
Expand Down Expand Up @@ -156,6 +156,8 @@ export const AggregateLineChart: React.FC<AggregateLineProps> = ({
afterDraw: (chart: ChartJS) => {
if (startDate === undefined || endDate === undefined || beforeData.length === 0) {
writeError(chart);
} else if (afterData.length === 0 || afterData.length < 7) {
writeError(chart, 'Analysis still in progress, numbers not final.');
}
},
},
Expand Down
6 changes: 3 additions & 3 deletions src/components/charts/TravelTimesChart.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AggregateLineChart } from './AggregateLineChart';
import dayjs from 'dayjs';
import { Shutdown } from '../../types';
import { AggregateDataPoint, PointFieldKeys } from './types';
import { CHART_COLORS } from '../../constants/colors';
import { getLocationDetails } from '../../utils/stations';
import dayjs from 'dayjs';
import { AggregateLineChart } from './AggregateLineChart';
import { AggregateDataPoint, PointFieldKeys } from './types';

interface TravelTimesChartProps {
shutdown: Shutdown;
Expand Down
3 changes: 0 additions & 3 deletions src/constants/colors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

export const hexWithAlpha = (hexColor: string, alpha: number) => {
const opacity = Math.round(Math.min(Math.max(alpha || 1, 0), 1) * 255);
return hexColor + opacity.toString(16).toUpperCase();
};


export const COLORS = {
mbta: {
red: '#D13434',
Expand Down Expand Up @@ -45,4 +43,3 @@ export const CHART_COLORS = {
ANNOTATIONS: hexWithAlpha('#202020', 0.4),
BLOCKS: hexWithAlpha('#202020', 0.2),
};

2 changes: 1 addition & 1 deletion src/constants/styles.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const cardStyles = 'rounded-lg bg-white dark:dark:bg-slate-700 dark:text-white p-4 shadow'
export const cardStyles = 'rounded-lg bg-white dark:dark:bg-slate-700 dark:text-white p-4 shadow';
Loading

0 comments on commit 288707c

Please sign in to comment.