import React, { useState, useRef } from 'react'; import { useSelector } from 'react-redux'; import { useNavigate } from 'react-router-dom'; import { Toolbar, IconButton, OutlinedInput, InputAdornment, Popover, FormControl, InputLabel, Select, MenuItem, FormGroup, FormControlLabel, Checkbox, Badge, ListItemButton, ListItemText, } from '@mui/material'; import { makeStyles, useTheme } from '@mui/styles'; import MapIcon from '@mui/icons-material/Map'; import ViewListIcon from '@mui/icons-material/ViewList'; import AddIcon from '@mui/icons-material/Add'; import TuneIcon from '@mui/icons-material/Tune'; import { useTranslation } from '../common/components/LocalizationProvider'; import { useDeviceReadonly } from '../common/util/permissions'; import DeviceRow from './DeviceRow'; const useStyles = makeStyles((theme) => ({ toolbar: { display: 'flex', gap: theme.spacing(1), }, filterPanel: { display: 'flex', flexDirection: 'column', padding: theme.spacing(2), gap: theme.spacing(2), width: theme.dimensions.drawerWidthTablet, }, })); const MainToolbar = ({ filteredDevices, devicesOpen, setDevicesOpen, keyword, setKeyword, filter, setFilter, filterSort, setFilterSort, filterMap, setFilterMap, }) => { const classes = useStyles(); const theme = useTheme(); const navigate = useNavigate(); const t = useTranslation(); const deviceReadonly = useDeviceReadonly(); const groups = useSelector((state) => state.groups.items); const devices = useSelector((state) => state.devices.items); const toolbarRef = useRef(); const inputRef = useRef(); const [filterAnchorEl, setFilterAnchorEl] = useState(null); const [devicesAnchorEl, setDevicesAnchorEl] = useState(null); const deviceStatusCount = (status) => Object.values(devices).filter((d) => d.status === status).length; return ( setDevicesOpen(!devicesOpen)}> {devicesOpen ? : } setKeyword(e.target.value)} onFocus={() => setDevicesAnchorEl(toolbarRef.current)} onBlur={() => setDevicesAnchorEl(null)} endAdornment={( setFilterAnchorEl(inputRef.current)}> )} size="small" fullWidth /> setDevicesAnchorEl(null)} anchorOrigin={{ vertical: 'bottom', horizontal: Number(theme.spacing(2).slice(0, -2)), }} marginThreshold={0} PaperProps={{ style: { width: `calc(${toolbarRef.current?.clientWidth}px - ${theme.spacing(4)})` }, }} elevation={1} disableAutoFocus disableEnforceFocus > {filteredDevices.slice(0, 3).map((_, index) => ( ))} {filteredDevices.length > 3 && ( setDevicesOpen(true)}> )} setFilterAnchorEl(null)} anchorOrigin={{ vertical: 'bottom', horizontal: 'left', }} >
{t('deviceStatus')} {t('settingsGroups')} {t('sharedSortBy')} setFilterMap(e.target.checked)} />} label={t('sharedFilterMap')} />
navigate('/settings/device')} disabled={deviceReadonly}>
); }; export default MainToolbar;