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
|
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, Tooltip,
} 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 (
<Toolbar ref={toolbarRef} className={classes.toolbar}>
<IconButton edge="start" onClick={() => setDevicesOpen(!devicesOpen)}>
{devicesOpen ? <MapIcon /> : <ViewListIcon />}
</IconButton>
<OutlinedInput
ref={inputRef}
placeholder={t('sharedSearchDevices')}
value={keyword}
onChange={(e) => setKeyword(e.target.value)}
onFocus={() => setDevicesAnchorEl(toolbarRef.current)}
onBlur={() => setDevicesAnchorEl(null)}
endAdornment={(
<InputAdornment position="end">
<IconButton size="small" edge="end" onClick={() => setFilterAnchorEl(inputRef.current)}>
<Badge color="info" variant="dot" invisible={!filter.statuses.length && !filter.groups.length}>
<TuneIcon fontSize="small" />
</Badge>
</IconButton>
</InputAdornment>
)}
size="small"
fullWidth
/>
<Popover
open={!!devicesAnchorEl && !devicesOpen}
anchorEl={devicesAnchorEl}
onClose={() => setDevicesAnchorEl(null)}
anchorOrigin={{
vertical: 'bottom',
horizontal: Number(theme.spacing(2).slice(0, -2)),
}}
marginThreshold={0}
slotProps={{
paper: {
style: { width: `calc(${toolbarRef.current?.clientWidth}px - ${theme.spacing(4)})` },
},
}}
elevation={1}
disableAutoFocus
disableEnforceFocus
>
{filteredDevices.slice(0, 3).map((_, index) => (
<DeviceRow key={filteredDevices[index].id} data={filteredDevices} index={index} />
))}
{filteredDevices.length > 3 && (
<ListItemButton alignItems="center" onClick={() => setDevicesOpen(true)}>
<ListItemText
primary={t('notificationAlways')}
style={{ textAlign: 'center' }}
/>
</ListItemButton>
)}
</Popover>
<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={filter.statuses}
onChange={(e) => setFilter({ ...filter, statuses: 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={filter.groups}
onChange={(e) => setFilter({ ...filter, groups: 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 edge="end" onClick={() => navigate('/settings/device')} disabled={deviceReadonly}>
<Tooltip open={!deviceReadonly && Object.keys(devices).length === 0} title={t('deviceRegisterFirst')} arrow>
<AddIcon />
</Tooltip>
</IconButton>
</Toolbar>
);
};
export default MainToolbar;
|