refactor: extracting condition for showing create monitor button
This commit is contained in:
1 parent
32b0c2039e
commit
63a4223d9c
1 file changed
+166
-167
@@ -4,13 +4,7 @@ import { useSelector, useDispatch } from "react-redux";
|
|||||||
import { getUptimeMonitorsByTeamId } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
import { getUptimeMonitorsByTeamId } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import {
|
import { Box, Button, CircularProgress, Stack, Typography } from "@mui/material";
|
||||||
Box,
|
|
||||||
Button,
|
|
||||||
CircularProgress,
|
|
||||||
Stack,
|
|
||||||
Typography,
|
|
||||||
} from "@mui/material";
|
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import SkeletonLayout from "./skeleton";
|
import SkeletonLayout from "./skeleton";
|
||||||
import Fallback from "./fallback";
|
import Fallback from "./fallback";
|
||||||
@@ -22,173 +16,178 @@ import Search from "../../../Components/Inputs/Search";
|
|||||||
import useDebounce from "../../../Utils/debounce";
|
import useDebounce from "../../../Utils/debounce";
|
||||||
|
|
||||||
const Monitors = ({ isAdmin }) => {
|
const Monitors = ({ isAdmin }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const monitorState = useSelector((state) => state.uptimeMonitors);
|
const monitorState = useSelector((state) => state.uptimeMonitors);
|
||||||
const authState = useSelector((state) => state.auth);
|
const authState = useSelector((state) => state.auth);
|
||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
const [isSearching, setIsSearching] = useState(false);
|
const [isSearching, setIsSearching] = useState(false);
|
||||||
const dispatch = useDispatch({});
|
const dispatch = useDispatch({});
|
||||||
const debouncedFilter = useDebounce(search, 500);
|
const debouncedFilter = useDebounce(search, 500);
|
||||||
|
|
||||||
const handleSearch = (value) => {
|
const handleSearch = (value) => {
|
||||||
setIsSearching(true);
|
setIsSearching(true);
|
||||||
setSearch(value);
|
setSearch(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
dispatch(getUptimeMonitorsByTeamId(authState.authToken));
|
dispatch(getUptimeMonitorsByTeamId(authState.authToken));
|
||||||
}, [authState.authToken, dispatch]);
|
}, [authState.authToken, dispatch]);
|
||||||
|
|
||||||
const loading =
|
console.log(monitorState);
|
||||||
monitorState?.isLoading &&
|
const loading = true;
|
||||||
monitorState?.monitorsSummary?.monitors?.length === 0;
|
/* monitorState?.isLoading &&
|
||||||
|
monitorState?.monitorsSummary?.monitors?.length === 0; */
|
||||||
|
const canCreateMonitor =
|
||||||
|
isAdmin && monitorState?.monitorsSummary?.monitors?.length !== 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack className="monitors" gap={theme.spacing(8)}>
|
<Stack
|
||||||
{loading ? (
|
className="monitors"
|
||||||
<SkeletonLayout />
|
gap={theme.spacing(8)}
|
||||||
) : (
|
>
|
||||||
<>
|
<Box>
|
||||||
<Box>
|
<Breadcrumbs list={[{ name: `monitors`, path: "/monitors" }]} />
|
||||||
<Breadcrumbs list={[{ name: `monitors`, path: "/monitors" }]} />
|
<Stack
|
||||||
<Stack
|
direction="row"
|
||||||
direction="row"
|
justifyContent="space-between"
|
||||||
justifyContent="space-between"
|
alignItems="center"
|
||||||
alignItems="center"
|
mt={theme.spacing(5)}
|
||||||
mt={theme.spacing(5)}
|
>
|
||||||
>
|
<Greeting type="uptime" />
|
||||||
<Greeting type="uptime" />
|
{canCreateMonitor && (
|
||||||
{isAdmin &&
|
<Button
|
||||||
monitorState?.monitorsSummary?.monitors?.length !== 0 && (
|
variant="contained"
|
||||||
<Button
|
color="primary"
|
||||||
variant="contained"
|
onClick={() => {
|
||||||
color="primary"
|
navigate("/monitors/create");
|
||||||
onClick={() => {
|
}}
|
||||||
navigate("/monitors/create");
|
sx={{ fontWeight: 500 }}
|
||||||
}}
|
>
|
||||||
sx={{ fontWeight: 500 }}
|
Create monitor
|
||||||
>
|
</Button>
|
||||||
Create monitor
|
)}
|
||||||
</Button>
|
</Stack>
|
||||||
)}
|
</Box>
|
||||||
</Stack>
|
{loading ? (
|
||||||
</Box>
|
<SkeletonLayout />
|
||||||
{isAdmin && monitorState?.monitorsSummary?.monitors?.length === 0 && (
|
) : (
|
||||||
<Fallback isAdmin={isAdmin} />
|
<>
|
||||||
)}
|
{isAdmin && monitorState?.monitorsSummary?.monitors?.length === 0 && (
|
||||||
|
<Fallback isAdmin={isAdmin} />
|
||||||
|
)}
|
||||||
|
|
||||||
{monitorState?.monitorsSummary?.monitors?.length !== 0 && (
|
{monitorState?.monitorsSummary?.monitors?.length !== 0 && (
|
||||||
<>
|
<>
|
||||||
<Stack
|
<Stack
|
||||||
gap={theme.spacing(8)}
|
gap={theme.spacing(8)}
|
||||||
direction="row"
|
direction="row"
|
||||||
justifyContent="space-between"
|
justifyContent="space-between"
|
||||||
>
|
>
|
||||||
<StatusBox
|
<StatusBox
|
||||||
title="up"
|
title="up"
|
||||||
value={monitorState?.monitorsSummary?.monitorCounts?.up ?? 0}
|
value={monitorState?.monitorsSummary?.monitorCounts?.up ?? 0}
|
||||||
/>
|
/>
|
||||||
<StatusBox
|
<StatusBox
|
||||||
title="down"
|
title="down"
|
||||||
value={
|
value={monitorState?.monitorsSummary?.monitorCounts?.down ?? 0}
|
||||||
monitorState?.monitorsSummary?.monitorCounts?.down ?? 0
|
/>
|
||||||
}
|
<StatusBox
|
||||||
/>
|
title="paused"
|
||||||
<StatusBox
|
value={monitorState?.monitorsSummary?.monitorCounts?.paused ?? 0}
|
||||||
title="paused"
|
/>
|
||||||
value={
|
</Stack>
|
||||||
monitorState?.monitorsSummary?.monitorCounts?.paused ?? 0
|
<Box
|
||||||
}
|
flex={1}
|
||||||
/>
|
px={theme.spacing(10)}
|
||||||
</Stack>
|
py={theme.spacing(8)}
|
||||||
<Box
|
border={1}
|
||||||
flex={1}
|
borderColor={theme.palette.border.light}
|
||||||
px={theme.spacing(10)}
|
borderRadius={theme.shape.borderRadius}
|
||||||
py={theme.spacing(8)}
|
backgroundColor={theme.palette.background.main}
|
||||||
border={1}
|
>
|
||||||
borderColor={theme.palette.border.light}
|
<Stack
|
||||||
borderRadius={theme.shape.borderRadius}
|
direction="row"
|
||||||
backgroundColor={theme.palette.background.main}
|
alignItems="center"
|
||||||
>
|
mb={theme.spacing(8)}
|
||||||
<Stack
|
>
|
||||||
direction="row"
|
<Typography
|
||||||
alignItems="center"
|
component="h2"
|
||||||
mb={theme.spacing(8)}
|
variant="h2"
|
||||||
>
|
fontWeight={500}
|
||||||
<Typography
|
letterSpacing={-0.2}
|
||||||
component="h2"
|
>
|
||||||
variant="h2"
|
Actively monitoring
|
||||||
fontWeight={500}
|
</Typography>
|
||||||
letterSpacing={-0.2}
|
<Box
|
||||||
>
|
className="current-monitors-counter"
|
||||||
Actively monitoring
|
color={theme.palette.text.primary}
|
||||||
</Typography>
|
border={1}
|
||||||
<Box
|
borderColor={theme.palette.border.light}
|
||||||
className="current-monitors-counter"
|
backgroundColor={theme.palette.background.accent}
|
||||||
color={theme.palette.text.primary}
|
>
|
||||||
border={1}
|
{monitorState?.monitorsSummary?.monitorCounts?.total || 0}
|
||||||
borderColor={theme.palette.border.light}
|
</Box>
|
||||||
backgroundColor={theme.palette.background.accent}
|
<Box
|
||||||
>
|
width="25%"
|
||||||
{monitorState?.monitorsSummary?.monitorCounts?.total || 0}
|
minWidth={150}
|
||||||
</Box>
|
ml="auto"
|
||||||
<Box width="25%" minWidth={150} ml="auto">
|
>
|
||||||
<Search
|
<Search
|
||||||
options={monitorState?.monitorsSummary?.monitors ?? []}
|
options={monitorState?.monitorsSummary?.monitors ?? []}
|
||||||
filteredBy="name"
|
filteredBy="name"
|
||||||
inputValue={search}
|
inputValue={search}
|
||||||
handleInputChange={handleSearch}
|
handleInputChange={handleSearch}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
<Box position="relative">
|
<Box position="relative">
|
||||||
{isSearching && (
|
{isSearching && (
|
||||||
<>
|
<>
|
||||||
<Box
|
<Box
|
||||||
width="100%"
|
width="100%"
|
||||||
height="100%"
|
height="100%"
|
||||||
position="absolute"
|
position="absolute"
|
||||||
sx={{
|
sx={{
|
||||||
backgroundColor: theme.palette.background.main,
|
backgroundColor: theme.palette.background.main,
|
||||||
opacity: 0.8,
|
opacity: 0.8,
|
||||||
zIndex: 100,
|
zIndex: 100,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Box
|
<Box
|
||||||
height="100%"
|
height="100%"
|
||||||
position="absolute"
|
position="absolute"
|
||||||
top="20%"
|
top="20%"
|
||||||
left="50%"
|
left="50%"
|
||||||
sx={{
|
sx={{
|
||||||
transform: "translateX(-50%)",
|
transform: "translateX(-50%)",
|
||||||
zIndex: 101,
|
zIndex: 101,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<CircularProgress
|
<CircularProgress
|
||||||
sx={{
|
sx={{
|
||||||
color: theme.palette.other.icon,
|
color: theme.palette.other.icon,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<MonitorTable
|
<MonitorTable
|
||||||
isAdmin={isAdmin}
|
isAdmin={isAdmin}
|
||||||
filter={debouncedFilter}
|
filter={debouncedFilter}
|
||||||
setLoading={setIsSearching}
|
setLoading={setIsSearching}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
Monitors.propTypes = {
|
Monitors.propTypes = {
|
||||||
isAdmin: PropTypes.bool,
|
isAdmin: PropTypes.bool,
|
||||||
};
|
};
|
||||||
export default Monitors;
|
export default Monitors;
|
||||||
Reference in new issue
Block a user