forked from hasadna/open-bus-map-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
143 lines (132 loc) · 4.79 KB
/
Copy pathindex.tsx
File metadata and controls
143 lines (132 loc) · 4.79 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
import {
Box,
CircularProgress,
Stack,
ToggleButton,
ToggleButtonGroup,
Typography,
useMediaQuery,
useTheme,
} from '@mui/material'
import React, { useContext, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { TileLayer } from 'react-leaflet'
import dayjs, { ISRAEL_TIMEZONE, toIsraelTimezone } from 'src/dayjs'
import { GlobalSearchContext } from 'src/model/globalState'
import { MapShell } from 'src/pages/components/map-related/MapShell'
import { DateNavigator } from '../components/dateNavigator/DateNavigator'
import { DateSelector } from '../components/DateSelector'
import { PageContainer } from '../components/PageContainer'
import { VelocityHeatmapLegend } from './components/VelocityHeatmapLegend'
import { VelocityHeatmapRectangles } from './components/VelocityHeatmapRectangles'
const VIS_MODES = [
{ key: 'avg', labelKey: 'velocity_vis_avg' },
{ key: 'std', labelKey: 'velocity_vis_std' },
{ key: 'cv', labelKey: 'velocity_vis_cv' },
] as const
const DEFAULT_ZOOM_LEVEL = 10
const VelocityHeatmapPage: React.FC = () => {
const { t } = useTranslation()
const theme = useTheme()
// Long labels are cramped as 3 columns on a phone, so stack the selector
// vertically (one button per row) there and keep a single row on wider screens.
const stackVisSelector = useMediaQuery(theme.breakpoints.down('sm'))
const { search, setSearch } = useContext(GlobalSearchContext)
const dateDayjs = dayjs.tz(search.date, ISRAEL_TIMEZONE)
const [visMode, setVisMode] = useState<'avg' | 'std' | 'cv'>('avg')
const [min, setMin] = useState(0)
const [max, setMax] = useState(1)
const [loading, setLoading] = useState(false)
const [hasError, setHasError] = useState(false)
const handleDateChange = (time: dayjs.Dayjs | null) => {
setSearch((current) => ({
...current,
date: toIsraelTimezone(time ?? dayjs()).format('YYYY-MM-DD'),
}))
}
const handleVisModeChange = (
_: React.MouseEvent<HTMLElement>,
value: 'avg' | 'std' | 'cv' | null,
) => {
if (value) setVisMode(value)
}
return (
<PageContainer>
<Typography variant="h4" component="h1" gutterBottom>
{t('velocity_heatmap_page_title')}
</Typography>
{/* choose date + visualization — centered block */}
<Box sx={{ width: '100%', maxWidth: 520, mx: 'auto' }}>
<Stack direction="column" spacing={2} sx={{ mb: 2 }}>
<DateSelector time={dateDayjs} onChange={handleDateChange} />
<DateNavigator currentTime={dateDayjs} onChange={handleDateChange} />
</Stack>
<ToggleButtonGroup
value={visMode}
color="primary"
exclusive
fullWidth
orientation={stackVisSelector ? 'vertical' : 'horizontal'}
onChange={handleVisModeChange}
sx={{ mt: 2 }}>
{VIS_MODES.map((mode) => (
<ToggleButton key={mode.key} value={mode.key}>
{t(mode.labelKey)}
</ToggleButton>
))}
</ToggleButtonGroup>
</Box>
<Box sx={{ position: 'relative', height: '100%', width: '100%' }}>
{(loading || hasError) && (
<Box
sx={{
position: 'absolute',
inset: 0,
zIndex: 1000,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
gap: 1,
pointerEvents: 'none',
backgroundColor:
theme.palette.mode === 'dark'
? 'rgba(40, 40, 40, 0.92)'
: 'rgba(255, 255, 255, 0.9)',
}}>
{loading && <CircularProgress />}
<Typography
variant="h6"
component="span"
sx={{ color: theme.palette.mode === 'dark' ? '#fff' : '#000' }}>
{hasError ? t('loading_error') : t('loading')}
</Typography>
</Box>
)}
<MapShell
center={[29.65, 34.6]}
zoom={DEFAULT_ZOOM_LEVEL}
scrollWheelZoom={true}
style={{ height: '100%', width: '100%' }}
legend={<VelocityHeatmapLegend visMode={visMode} min={min} max={max} />}>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://tile-a.openstreetmap.fr/hot/{z}/{x}/{y}.png"
/>
<VelocityHeatmapRectangles
visMode={visMode}
setMinMax={(min, max) => {
setMin(min)
setMax(max)
}}
setStatus={(loading, hasError) => {
setLoading(loading)
setHasError(hasError)
}}
/>
</MapShell>
</Box>
</PageContainer>
)
}
export default VelocityHeatmapPage