-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathindex.tsx
More file actions
217 lines (207 loc) · 7.52 KB
/
Copy pathindex.tsx
File metadata and controls
217 lines (207 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { Alert, CircularProgress, Grid, Typography } from '@mui/material'
import { useQuery } from '@tanstack/react-query'
import { useContext, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import {
getGtfsStopHitTimesAsync,
getRoutesAsync,
getStopsForRouteAsync,
} from 'src/api/gtfsService'
import { getSiriStopHitTimesAsync } from 'src/api/siriService'
import dayjs, { ISRAEL_TIMEZONE } from 'src/dayjs'
import { GlobalSearchContext } from 'src/model/globalState'
import { Label } from 'src/pages/components/Label'
import LineNumberSelector from 'src/pages/components/LineSelector'
import OperatorSelector from 'src/pages/components/OperatorSelector'
import RouteSelector from 'src/pages/components/RouteSelector'
import { Row } from 'src/pages/components/Row'
import { StickyInputs } from 'src/pages/components/StickyInputs'
import StopSelector from 'src/pages/components/StopSelector'
import { TimelineBoard } from 'src/pages/components/timeline/TimelineBoard'
import Widget from 'src/shared/Widget'
import { DateSelector } from '../components/DateSelector'
import { NotFound } from '../components/NotFound'
import { PageContainer } from '../components/PageContainer'
import { TimeSelector } from '../components/TimeSelector'
const TimelinePage = () => {
const { t } = useTranslation()
const { search, setSearch } = useContext(GlobalSearchContext)
const { operatorId, lineNumber, date, routeKey } = search
// Time-of-day is page-local (only /timeline has a time picker)
const [timeOfDay, setTimeOfDay] = useState(() => dayjs().startOf('minute'))
const time = useMemo(() => {
return dayjs
.tz(date, ISRAEL_TIMEZONE)
.hour(timeOfDay.hour())
.minute(timeOfDay.minute())
.startOf('minute')
}, [date, timeOfDay])
const routesQuery = useQuery({
queryFn: async () => {
if (operatorId && lineNumber) {
try {
const routes = await getRoutesAsync(time, time, operatorId, lineNumber)
return routes
} catch (error) {
console.error(error)
setSearch((current) => ({ ...current, routeKey: null }))
setSearch((current) => ({ ...current, stopKey: null }))
}
}
return null
},
queryKey: ['routes', operatorId, lineNumber, time.valueOf()],
})
const selectedRoute = useMemo(() => {
return routesQuery.data?.find((route) => route.key === routeKey)
}, [routesQuery.data, routeKey])
const stopsQuery = useQuery({
queryFn: async () => {
if (selectedRoute) {
try {
return await getStopsForRouteAsync(selectedRoute.routeIds, time)
} catch (error) {
console.error(error)
setSearch((current) => ({ ...current, stopKey: null }))
}
}
return null
},
queryKey: ['stops', selectedRoute?.lineRef, time.valueOf()],
})
const selectedStop = useMemo(() => {
return stopsQuery.data?.find((stop) => stop.key === search.stopKey)
}, [stopsQuery.data, search.stopKey])
const hitsQuery = useQuery({
queryFn: async () => {
if (selectedStop && selectedRoute) {
const [gtfsTime, siriTime] = await Promise.all([
getGtfsStopHitTimesAsync(selectedStop, time),
getSiriStopHitTimesAsync(selectedRoute, selectedStop, time),
])
return { gtfsTime, siriTime }
}
return null
},
queryKey: ['hits', selectedRoute?.lineRef, selectedStop?.stopId, time.valueOf()],
})
return (
<PageContainer>
<Typography variant="h4" gutterBottom className="page-title">
{t('timeline_page_title')}
</Typography>
<Alert severity="info" variant="outlined" icon={false}>
{t('timeline_page_description')}
</Alert>
{hitsQuery.data &&
hitsQuery.data.gtfsTime.length > 0 &&
hitsQuery.data.siriTime.length === 0 && (
<Alert severity="warning" variant="outlined">
{t('no_data_from_ETL')}
</Alert>
)}
<StickyInputs>
<Grid container spacing={2}>
{/* choose date */}
<Grid size={{ lg: 4, md: 6, xs: 12 }}>
<DateSelector
time={dayjs.tz(date, ISRAEL_TIMEZONE)}
onChange={(ts) => {
if (!ts) return
setSearch((current) => ({
...current,
date: ts.format('YYYY-MM-DD'),
}))
}}
/>
</Grid>
{/* choose time */}
<Grid size={{ lg: 4, md: 6, xs: 12 }}>
<TimeSelector
time={timeOfDay}
onChange={(ts) => {
if (!ts) return
setTimeOfDay(ts.startOf('minute'))
}}
/>
</Grid>
{/* choose operator */}
<Grid size={{ lg: 4, md: 6, xs: 12 }}>
<OperatorSelector
operatorId={operatorId ?? undefined}
setOperatorId={(id) => setSearch((current) => ({ ...current, operatorId: id }))}
/>
</Grid>
{/* choose line */}
<Grid size={{ lg: 4, md: 6, xs: 12 }}>
<LineNumberSelector
lineNumber={lineNumber ?? undefined}
setLineNumber={(number) =>
setSearch((current) => ({ ...current, lineNumber: number }))
}
/>
</Grid>
{/* routes */}
<Grid container size={{ lg: 4, md: 6, xs: 12 }}>
<Row style={{ width: '100%' }}>
<div style={{ width: '100%' }}>
{routesQuery.data?.length === 0 ? (
<NotFound>{t('line_not_found')}</NotFound>
) : (
<RouteSelector
disabled={!routesQuery.data}
routes={routesQuery.data || []}
routeKey={routeKey ?? undefined}
setRouteKey={(key) =>
setSearch((current) => ({ ...current, routeKey: key ?? null }))
}
/>
)}
</div>
{routesQuery.isLoading && <CircularProgress />}
</Row>
</Grid>
{/* stops */}
<Grid container size={{ lg: 4, md: 6, xs: 12 }}>
<Row style={{ width: '100%' }}>
<div style={{ width: '100%' }}>
<StopSelector
disabled={!stopsQuery.data}
stops={stopsQuery.data || []}
stopKey={search.stopKey ?? undefined}
setStopKey={(key) =>
setSearch((current) => ({ ...current, stopKey: key ?? null }))
}
/>
</div>
{stopsQuery.isLoading && <CircularProgress />}
</Row>
</Grid>
</Grid>
</StickyInputs>
{/* hits timeline */}
{selectedRoute && selectedStop && (
<Widget marginBottom>
{hitsQuery.isLoading && (
<Row>
<Label text={t('loading_hits')} />
<CircularProgress />
</Row>
)}
{!hitsQuery.isLoading &&
((hitsQuery.data?.gtfsTime && hitsQuery.data.gtfsTime.length > 0) ||
(hitsQuery.data?.siriTime && hitsQuery.data.siriTime.length > 0) ? (
<TimelineBoard
target={time}
gtfsTimes={hitsQuery.data.gtfsTime}
siriTimes={hitsQuery.data.siriTime}
/>
) : (
<NotFound>{t('hits_not_found')}</NotFound>
))}
</Widget>
)}
</PageContainer>
)
}
export default TimelinePage