diff --git a/src/components/TrainInfoContainer.tsx b/src/components/TrainInfoContainer.tsx index c7ea184f..bb13799f 100644 --- a/src/components/TrainInfoContainer.tsx +++ b/src/components/TrainInfoContainer.tsx @@ -1,10 +1,6 @@ import { useState } from 'react'; -import { Timeline, TimelineContent, TimelineItem } from '@mui/lab'; -import { Box, Link, Grid, Skeleton } from '@mui/material'; -import { parseISO } from 'date-fns'; -import RouterLink from 'next/link'; -import { useTranslation } from 'react-i18next'; +import { Box, Skeleton } from '@mui/material'; import { gqlClients } from '../graphql/client'; import { @@ -13,18 +9,10 @@ import { Wagon, } from '../graphql/generated/digitraffic'; import { formatEET } from '../utils/date'; -import getTrainCurrentStation from '../utils/getTrainCurrentStation'; -import getTrainLatestArrivalRow from '../utils/getTrainLatestArrivalRow'; -import getTrainLatestDepartureTimeTableRow from '../utils/getTrainLatestDepartureTimeTableRow'; -import getTrainPreviousStation from '../utils/getTrainPreviousStation'; -import { - getTrainScheduledDepartureTime, - getTrainStationName, -} from '../utils/train'; +import { getTrainScheduledDepartureTime } from '../utils/train'; -import TimelineRouteStopSeparator from './TimelineRouteStopSeparator'; -import TimeTableRowTime from './TimeTableRowTime'; import TrainComposition from './TrainComposition'; +import TrainStationTimeline from './TrainStationTimeline'; import TrainWagonDetailsDialog from './TrainWagonDetailsDialog'; type TrainInfoContainerProps = { @@ -32,7 +20,6 @@ type TrainInfoContainerProps = { }; function TrainInfoContainer({ train }: TrainInfoContainerProps) { - const { t } = useTranslation(); const [wagonDialogOpen, setWagonDialogOpen] = useState(false); const [selectedWagon, setSelectedWagon] = useState(null); const departureDate = train ? getTrainScheduledDepartureTime(train) : null; @@ -66,108 +53,6 @@ function TrainInfoContainer({ train }: TrainInfoContainerProps) { setWagonDialogOpen(true); }; - const getStops = () => { - if (!train) return null; - const trainCurrentStation = getTrainCurrentStation(train); - const trainPreviousStation = getTrainPreviousStation(train); - const trainLatestArrivalRow = getTrainLatestArrivalRow(train); - const trainLatestDepartureRow = getTrainLatestDepartureTimeTableRow(train); - - const timeTableRows = (realTimeTrain ?? train).timeTableGroups; - - return timeTableRows?.map((g, i, { length }) => { - const r = g.departure ?? g.arrival; - if (!r) return <>; - const station = r.station; - const stationPassed = - (r.actualTime != null && parseISO(r.actualTime) < new Date()) || - (trainLatestDepartureRow != null && - r.scheduledTime < trainLatestDepartureRow?.scheduledTime); - return ( - - - - - - - {getTrainStationName(station)} - - - {t('track')} {r.commercialTrack} - - - - {g.arrival ? : '-'} - - - {g.departure ? : '-'} - - - {realTimeTrain && ( -
- -
- )} - ({ - borderBottom: `solid 1px ${theme.palette.divider}`, - })} - > -
-
-
-
- ); - }); - }; - - const getLoadingSkeleton = () => { - return ( - - {Array.from(Array(7).keys()).map((i) => ( - - ))} - - ); - }; - return ( <> )} - ({ - marginTop: 0, - paddingTop: 0, - '.MuiTimelineContent-root': { - ...theme.typography.body2, - paddingRight: 0, - }, - })} - > - ({ - position: 'sticky', - top: '3.5rem', - zIndex: 1002, - backgroundColor: theme.palette.common.secondaryBackground.default, - boxShadow: `inset 0px -1px 1px ${theme.palette.divider}`, - minHeight: 'auto', - marginX: '-16px', // Negate parent padding to span whole horizontal space - padding: '0.4rem', - paddingRight: '16px', // Original parent padding - paddingLeft: 'calc(16px + 12px)', // Original parent padding + timeline dot width - marginBottom: '0.4rem', - '&:before': { - display: 'none', - }, - })} - > - - ({ - fontWeight: theme.typography.fontWeightMedium, - })} - > - - {t('station')} - - - {t('arrival')} - - - {t('departure')} - - - - - {getStops()} - {!train && getLoadingSkeleton()} - + {train && ( void; +}; + +const TrainStationTimeline = ({ + train, + realTimeTrain, + onWagonClick, +}: TrainStationTimelineProps) => { + const { t } = useTranslation(); + + const getStops = () => { + if (!train) return null; + const trainCurrentStation = getTrainCurrentStation(train); + const trainPreviousStation = getTrainPreviousStation(train); + const trainLatestArrivalRow = getTrainLatestArrivalRow(train); + const trainLatestDepartureRow = getTrainLatestDepartureTimeTableRow(train); + + const timeTableRows = (realTimeTrain ?? train).timeTableGroups; + + return timeTableRows?.map((g, i, { length }) => { + const r = g.departure ?? g.arrival; + if (!r) return <>; + const station = r.station; + const stationPassed = + (r.actualTime != null && parseISO(r.actualTime) < new Date()) || + (trainLatestDepartureRow != null && + r.scheduledTime < trainLatestDepartureRow?.scheduledTime); + return ( + + + + + + + {getTrainStationName(station)} + + + {t('track')} {r.commercialTrack} + + + + {g.arrival ? : '-'} + + + {g.departure ? : '-'} + + + {realTimeTrain && ( +
+ +
+ )} + +
+
+
+
+ ); + }); + }; + + const getLoadingSkeleton = () => { + return ( + + {Array.from(Array(7).keys()).map((i) => ( + + ))} + + ); + }; + + return ( + ({ + marginTop: 0, + paddingTop: 0, + '.MuiTimelineContent-root': { + ...theme.typography.body2, + paddingRight: 0, + }, + })} + > + ({ + position: 'sticky', + top: '3.5rem', + zIndex: 1002, + backgroundColor: theme.palette.common.secondaryBackground.default, + boxShadow: `inset 0px -1px 1px ${theme.palette.divider}`, + minHeight: 'auto', + marginX: '-16px', // Negate parent padding to span whole horizontal space + padding: '0.4rem', + paddingRight: '16px', // Original parent padding + paddingLeft: 'calc(16px + 12px)', // Original parent padding + timeline dot width + marginBottom: '0.4rem', + '&:before': { + display: 'none', + }, + })} + > + + ({ + fontWeight: theme.typography.fontWeightMedium, + })} + > + + {t('station')} + + + {t('arrival')} + + + {t('departure')} + + + + + {getStops()} + {!train && getLoadingSkeleton()} + + ); +}; + +export default TrainStationTimeline;