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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
import React, {
useState, useEffect, useCallback, useRef,
} from 'react';
import { useNavigate } from 'react-router-dom';
import {
Paper, Toolbar, IconButton, Button, OutlinedInput, InputAdornment, Popover, FormControl, InputLabel, Select, MenuItem, FormGroup, FormControlLabel, Checkbox, Badge,
} from '@mui/material';
import { makeStyles } from '@mui/styles';
import { useTheme } from '@mui/material/styles';
import useMediaQuery from '@mui/material/useMediaQuery';
import AddIcon from '@mui/icons-material/Add';
import CloseIcon from '@mui/icons-material/Close';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import ListIcon from '@mui/icons-material/ViewList';
import TuneIcon from '@mui/icons-material/Tune';
import { useDispatch, useSelector } from 'react-redux';
import moment from 'moment';
import DevicesList from './DevicesList';
import MapView from '../map/core/MapView';
import MapSelectedDevice from '../map/main/MapSelectedDevice';
import MapAccuracy from '../map/main/MapAccuracy';
import MapGeofence from '../map/MapGeofence';
import MapCurrentLocation from '../map/MapCurrentLocation';
import BottomMenu from '../common/components/BottomMenu';
import { useTranslation } from '../common/components/LocalizationProvider';
import PoiMap from '../map/main/PoiMap';
import MapPadding from '../map/MapPadding';
import StatusCard from '../common/components/StatusCard';
import { devicesActions } from '../store';
import MapDefaultCamera from '../map/main/MapDefaultCamera';
import usePersistedState from '../common/util/usePersistedState';
import MapLiveRoutes from '../map/main/MapLiveRoutes';
import { useDeviceReadonly } from '../common/util/permissions';
import MapPositions from '../map/MapPositions';
import MapOverlay from '../map/overlay/MapOverlay';
import MapGeocoder from '../map/geocoder/MapGeocoder';
import MapScale from '../map/MapScale';
import MapNotification from '../map/notification/MapNotification';
import EventsDrawer from './EventsDrawer';
import useFeatures from '../common/util/useFeatures';
const useStyles = makeStyles((theme) => ({
root: {
height: '100%',
},
sidebar: {
display: 'flex',
flexDirection: 'column',
position: 'fixed',
left: 0,
top: 0,
zIndex: 3,
margin: theme.spacing(1.5),
width: theme.dimensions.drawerWidthDesktop,
bottom: theme.dimensions.bottomBarHeight,
transition: 'transform .5s ease',
backgroundColor: 'white',
[theme.breakpoints.down('md')]: {
width: '100%',
margin: 0,
},
},
sidebarCollapsed: {
transform: `translateX(-${theme.dimensions.drawerWidthDesktop})`,
marginLeft: 0,
[theme.breakpoints.down('md')]: {
transform: 'translateX(-100vw)',
},
},
toolbarContainer: {
zIndex: 4,
},
toolbar: {
display: 'flex',
padding: theme.spacing(0, 1),
'& > *': {
margin: theme.spacing(0, 1),
},
},
deviceList: {
flex: 1,
},
sidebarToggle: {
position: 'fixed',
left: theme.spacing(1.5),
top: theme.spacing(3),
borderRadius: '0px',
minWidth: 0,
[theme.breakpoints.down('md')]: {
left: 0,
},
},
sidebarToggleText: {
marginLeft: theme.spacing(1),
[theme.breakpoints.only('xs')]: {
display: 'none',
},
},
sidebarToggleBg: {
backgroundColor: 'white',
color: 'rgba(0, 0, 0, 0.6)',
'&:hover': {
backgroundColor: 'white',
},
},
bottomMenu: {
position: 'fixed',
left: theme.spacing(1.5),
bottom: theme.spacing(1.5),
zIndex: 4,
width: theme.dimensions.drawerWidthDesktop,
},
filterPanel: {
display: 'flex',
flexDirection: 'column',
padding: theme.spacing(2),
gap: theme.spacing(2),
width: theme.dimensions.drawerWidthTablet,
},
}));
const MainPage = () => {
const classes = useStyles();
const navigate = useNavigate();
const dispatch = useDispatch();
const theme = useTheme();
const t = useTranslation();
const deviceReadonly = useDeviceReadonly();
const desktop = useMediaQuery(theme.breakpoints.up('md'));
const phone = useMediaQuery(theme.breakpoints.down('sm'));
const features = useFeatures();
const [mapOnSelect] = usePersistedState('mapOnSelect', false);
const [mapLiveRoutes] = usePersistedState('mapLiveRoutes', false);
const selectedDeviceId = useSelector((state) => state.devices.selectedId);
const positions = useSelector((state) => state.positions.items);
const [filteredPositions, setFilteredPositions] = useState([]);
const selectedPosition = filteredPositions.find((position) => selectedDeviceId && position.deviceId === selectedDeviceId);
const groups = useSelector((state) => state.groups.items);
const devices = useSelector((state) => state.devices.items);
const [filteredDevices, setFilteredDevices] = useState([]);
const [filterKeyword, setFilterKeyword] = useState('');
const [filterStatuses, setFilterStatuses] = useState([]);
const [filterGroups, setFilterGroups] = useState([]);
const [filterSort, setFilterSort] = usePersistedState('filterSort', '');
const [filterMap, setFilterMap] = usePersistedState('filterMap', false);
const filterRef = useRef();
const [filterAnchorEl, setFilterAnchorEl] = useState(null);
const [devicesOpen, setDevicesOpen] = useState(false);
const [eventsOpen, setEventsOpen] = useState(false);
const eventHandler = useCallback(() => setEventsOpen(true), [setEventsOpen]);
const eventsAvailable = useSelector((state) => !!state.events.items.length);
const handleClose = () => {
setDevicesOpen(!devicesOpen);
};
const deviceStatusCount = (status) => Object.values(devices).filter((d) => d.status === status).length;
useEffect(() => setDevicesOpen(desktop), [desktop]);
useEffect(() => {
if (!desktop && mapOnSelect && selectedDeviceId) {
setDevicesOpen(false);
}
}, [desktop, mapOnSelect, selectedDeviceId]);
const onClick = useCallback((_, deviceId) => {
dispatch(devicesActions.select(deviceId));
}, [dispatch]);
const deviceGroups = (device) => {
const groupIds = [];
let { groupId } = device;
while (groupId) {
groupIds.push(groupId);
groupId = groups[groupId]?.groupId || 0;
}
return groupIds;
};
useEffect(() => {
const filtered = Object.values(devices)
.filter((device) => !filterStatuses.length || filterStatuses.includes(device.status))
.filter((device) => !filterGroups.length || deviceGroups(device).some((id) => filterGroups.includes(id)))
.filter((device) => {
const keyword = filterKeyword.toLowerCase();
return [device.name, device.uniqueId, device.phone, device.model, device.contact].some((s) => s && s.toLowerCase().includes(keyword));
});
switch (filterSort) {
case 'name':
filtered.sort((device1, device2) => device1.name.localeCompare(device2.name));
break;
case 'lastUpdate':
filtered.sort((device1, device2) => {
const time1 = device1.lastUpdate ? moment(device1.lastUpdate).valueOf() : 0;
const time2 = device2.lastUpdate ? moment(device2.lastUpdate).valueOf() : 0;
return time2 - time1;
});
break;
default:
break;
}
if (filterSort === 'lastUpdate') {
filtered.sort((device1, device2) => {
const time1 = device1.lastUpdate ? moment(device1.lastUpdate).valueOf() : 0;
const time2 = device2.lastUpdate ? moment(device2.lastUpdate).valueOf() : 0;
return time2 - time1;
});
}
setFilteredDevices(filtered);
setFilteredPositions(filterMap
? filtered.map((device) => positions[device.id]).filter(Boolean)
: Object.values(positions));
}, [devices, positions, filterKeyword, filterStatuses, filterGroups, filterSort, filterMap]);
return (
<div className={classes.root}>
<MapView>
<MapOverlay />
<MapGeofence />
<MapAccuracy positions={filteredPositions} />
{mapLiveRoutes && <MapLiveRoutes />}
<MapPositions positions={filteredPositions} onClick={onClick} selectedPosition={selectedPosition} showStatus />
<MapDefaultCamera />
<MapSelectedDevice />
<PoiMap />
</MapView>
<MapScale />
<MapCurrentLocation />
<MapGeocoder />
{!features.disableEvents && <MapNotification enabled={eventsAvailable} onClick={eventHandler} />}
{desktop && <MapPadding left={parseInt(theme.dimensions.drawerWidthDesktop, 10)} />}
<Button
variant="contained"
color={phone ? 'secondary' : 'primary'}
classes={{ containedPrimary: classes.sidebarToggleBg }}
className={classes.sidebarToggle}
onClick={handleClose}
disableElevation
>
<ListIcon />
<div className={classes.sidebarToggleText}>{t('deviceTitle')}</div>
</Button>
<Paper square elevation={3} className={`${classes.sidebar} ${!devicesOpen && classes.sidebarCollapsed}`}>
<Paper square elevation={3} className={classes.toolbarContainer}>
<Toolbar className={classes.toolbar} disableGutters>
{!desktop && (
<IconButton edge="start" sx={{ mr: 2 }} onClick={handleClose}>
<ArrowBackIcon />
</IconButton>
)}
<OutlinedInput
ref={filterRef}
placeholder={t('sharedSearchDevices')}
value={filterKeyword}
onChange={(event) => setFilterKeyword(event.target.value)}
endAdornment={(
<InputAdornment position="end">
<IconButton size="small" edge="end" onClick={() => setFilterAnchorEl(filterRef.current)}>
<Badge color="info" variant="dot" invisible={!filterStatuses.length && !filterGroups.length}>
<TuneIcon fontSize="small" />
</Badge>
</IconButton>
</InputAdornment>
)}
size="small"
fullWidth
/>
<Popover
open={!!filterAnchorEl}
anchorEl={filterAnchorEl}
onClose={() => setFilterAnchorEl(null)}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
>
<div className={classes.filterPanel}>
<FormControl>
<InputLabel>{t('deviceStatus')}</InputLabel>
<Select
label={t('deviceStatus')}
value={filterStatuses}
onChange={(e) => setFilterStatuses(e.target.value)}
multiple
>
<MenuItem value="online">{`${t('deviceStatusOnline')} (${deviceStatusCount('online')})`}</MenuItem>
<MenuItem value="offline">{`${t('deviceStatusOffline')} (${deviceStatusCount('offline')})`}</MenuItem>
<MenuItem value="unknown">{`${t('deviceStatusUnknown')} (${deviceStatusCount('unknown')})`}</MenuItem>
</Select>
</FormControl>
<FormControl>
<InputLabel>{t('settingsGroups')}</InputLabel>
<Select
label={t('settingsGroups')}
value={filterGroups}
onChange={(e) => setFilterGroups(e.target.value)}
multiple
>
{Object.values(groups).sort((a, b) => a.name.localeCompare(b.name)).map((group) => (
<MenuItem key={group.id} value={group.id}>{group.name}</MenuItem>
))}
</Select>
</FormControl>
<FormControl>
<InputLabel>{t('sharedSortBy')}</InputLabel>
<Select
label={t('sharedSortBy')}
value={filterSort}
onChange={(e) => setFilterSort(e.target.value)}
displayEmpty
>
<MenuItem value="">{'\u00a0'}</MenuItem>
<MenuItem value="name">{t('sharedName')}</MenuItem>
<MenuItem value="lastUpdate">{t('deviceLastUpdate')}</MenuItem>
</Select>
</FormControl>
<FormGroup>
<FormControlLabel
control={<Checkbox checked={filterMap} onChange={(e) => setFilterMap(e.target.checked)} />}
label={t('sharedFilterMap')}
/>
</FormGroup>
</div>
</Popover>
<IconButton onClick={() => navigate('/settings/device')} disabled={deviceReadonly}>
<AddIcon />
</IconButton>
{desktop && (
<IconButton onClick={handleClose}>
<CloseIcon />
</IconButton>
)}
</Toolbar>
</Paper>
<div className={classes.deviceList}>
<DevicesList devices={filteredDevices} />
</div>
</Paper>
{desktop && (
<div className={classes.bottomMenu}>
<BottomMenu />
</div>
)}
{!features.disableEvents && <EventsDrawer open={eventsOpen} onClose={() => setEventsOpen(false)} />}
{selectedDeviceId && (
<StatusCard
deviceId={selectedDeviceId}
position={positions[selectedDeviceId]}
onClose={() => dispatch(devicesActions.select(null))}
desktopPadding={theme.dimensions.drawerWidthDesktop}
/>
)}
</div>
);
};
export default MainPage;
|