Feat: Uptime Monitor Filters

This commit is contained in:
Br0wnHammer committed 2025-04-02 15:00:00 +05:30
1 parent 4ffb6832e2
commit bda7e4706b
5 files changed
+212 -152

No files matched your search

+60 -90
View File
@@ -1,108 +1,78 @@
import {
Checkbox,
FormControl,
InputLabel,
ListItemText,
MenuItem,
Select,
} from "@mui/material";
import { Checkbox, FormControl, ListItemText, MenuItem, Select } from "@mui/material";
import { useTheme } from "@emotion/react";
import PropTypes from "prop-types";
import AddCircleOutlineIcon from "@mui/icons-material/AddCircleOutline";
/**
* A reusable filter header component that displays a dropdown menu with selectable options.
*
* @component
* @param {Object} props - The component props.
* @param {string} props.header - The header text to display when no options are selected.
* @param {Array} props.options - An array of options to display in the dropdown menu. Each option should have a `value` and `label`.
* @param {Array} [props.value] - The currently selected values.
* @param {Function} props.onChange - The callback function to handle changes in the selected values.
* @param {boolean} [props.multiple=true] - Whether multiple options can be selected.
* @returns {JSX.Element} The rendered FilterHeader component.
*/
const FilterHeader = ({ header, options, value, onChange, multiple = true }) => {
const theme = useTheme();
const selectStyles = {
"& .MuiOutlinedInput-input": {
color: theme.palette.primary.contrastText,
},
"& .MuiOutlinedInput-notchedOutline": {
borderColor: theme.palette.primary.lowContrast,
borderRadius: theme.shape.borderRadius,
},
"& .MuiSelect-icon": {
color: theme.palette.primary.contrastText,
},
"&:hover": {
backgroundColor: theme.palette.primary.main,
},
"&:hover .MuiOutlinedInput-notchedOutline": {
borderColor: theme.palette.primary.lowContrast,
},
};
const menuItemStyles = {
"&:hover": {
backgroundColor: theme.palette.secondary.main,
},
};
return (
<div>
<FormControl
sx={{ m: theme.spacing(2), minWidth: 120 }}
size="small"
<FormControl
sx={{ m: theme.spacing(2), minWidth: "10%" }}
size="small"
>
<Select
multiple={multiple}
IconComponent={(props) => (
<AddCircleOutlineIcon
{...props}
sx={{ fontSize: "medium" }}
/>
)}
displayEmpty
value={value ?? []}
onChange={onChange}
renderValue={(selected) => {
if (!selected?.length) {
return header;
}
return selected
.map((value) => options.find((option) => option.value === value)?.label)
.filter(Boolean)
.join(", ");
}}
>
<InputLabel
sx={{
color: theme.palette.primary.contrastText,
"&.Mui-focused": {
display: "none",
},
}}
>
{header}
</InputLabel>
<Select
multiple={multiple}
IconComponent={(props) => (
<AddCircleOutlineIcon
{...props}
sx={{ fontSize: "medium" }}
{options.map((option) => (
<MenuItem
key={option.value}
value={option.value}
>
<Checkbox
checked={value?.includes(option.value)}
size="small"
/>
)}
value={value}
onChange={onChange}
renderValue={(selected) => selected.join(", ")}
sx={selectStyles}
>
{options.map((option) => (
<MenuItem
key={option}
value={option}
sx={menuItemStyles}
>
<Checkbox
checked={value.includes(option)}
size="small"
/>
<ListItemText
primary={
option === "http"
? "HTTP(S)"
: option === "ping"
? "Ping"
: option === "docker"
? "Docker"
: option === "port"
? "Port"
: option
}
/>
</MenuItem>
))}
</Select>
</FormControl>
</div>
<ListItemText primary={option.label} />
</MenuItem>
))}
</Select>
</FormControl>
);
};
FilterHeader.propTypes = {
header: PropTypes.string,
options: PropTypes.arrayOf(PropTypes.string),
header: PropTypes.string.isRequired,
options: PropTypes.arrayOf(
PropTypes.shape({
value: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
})
).isRequired,
value: PropTypes.arrayOf(PropTypes.string),
onChange: PropTypes.func,
onChange: PropTypes.func.isRequired,
multiple: PropTypes.bool,
};