import React, { useState, useCallback, useEffect, } from 'react'; import { Paper } from '@mui/material'; import { makeStyles } from '@mui/styles'; import { useTheme } from '@mui/material/styles'; import useMediaQuery from '@mui/material/useMediaQuery'; import { useDispatch, useSelector } from 'react-redux'; import DeviceList from './DeviceList'; import BottomMenu from '../common/components/BottomMenu'; import StatusCard from '../common/components/StatusCard'; import { devicesActions } from '../store'; import usePersistedState from '../common/util/usePersistedState'; import EventsDrawer from './EventsDrawer'; import useFilter from './useFilter'; import MainToolbar from './MainToolbar'; import MainMap from './MainMap'; import { useAttributePreference } from '../common/util/preferences'; const useStyles = makeStyles((theme) => ({ root: { height: '100%', }, sidebar: { display: 'flex', flexDirection: 'column', [theme.breakpoints.up('md')]: { position: 'fixed', left: 0, top: 0, height: `calc(100% - ${theme.spacing(3)})`, width: theme.dimensions.drawerWidthDesktop, margin: theme.spacing(1.5), zIndex: 3, }, [theme.breakpoints.down('md')]: { height: '100%', width: '100%', }, }, header: { zIndex: 6, }, footer: { zIndex: 5, }, middle: { flex: 1, display: 'grid', }, contentMap: { gridArea: '1 / 1', }, contentList: { gridArea: '1 / 1', zIndex: 4, }, })); const MainPage = () => { const classes = useStyles(); const dispatch = useDispatch(); const theme = useTheme(); const desktop = useMediaQuery(theme.breakpoints.up('md')); const mapOnSelect = useAttributePreference('mapOnSelect', true); 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 [filteredDevices, setFilteredDevices] = useState([]); const [filter, setFilter] = useState({ keyword: '', statuses: [], groups: [], }); const [filterSort, setFilterSort] = usePersistedState('filterSort', ''); const [filterMap, setFilterMap] = usePersistedState('filterMap', false); const [devicesOpen, setDevicesOpen] = useState(desktop); const [eventsOpen, setEventsOpen] = useState(false); const onEventsClick = useCallback(() => setEventsOpen(true), [setEventsOpen]); useEffect(() => { if (!desktop && mapOnSelect && selectedDeviceId) { setDevicesOpen(false); } }, [desktop, mapOnSelect, selectedDeviceId]); useFilter(filter, filterSort, filterMap, positions, setFilteredDevices, setFilteredPositions); return (