refactor: created current monitoring component and changed isLoading initial to true because of glitch

This commit is contained in:
Caio Cabral committed 2024-10-18 17:29:11 -04:00
1 parent 5adca16b55
commit 704348d1f9
5 files changed
+119 -86

No files matched your search

+3 -10
View File
@@ -1,12 +1,5 @@
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { import { Box, ListItem, Autocomplete, TextField, Stack, Typography } from "@mui/material";
Box,
ListItem,
Autocomplete,
TextField,
Stack,
Typography,
} from "@mui/material";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import SearchIcon from "../../../assets/icons/search.svg?react"; import SearchIcon from "../../../assets/icons/search.svg?react";
@@ -45,6 +38,7 @@ const SearchAdornment = () => {
); );
}; };
//TODO keep search state inside of component
const Search = ({ const Search = ({
id, id,
options, options,
@@ -141,8 +135,7 @@ const Search = ({
: {} : {}
} }
> >
{option[filteredBy] + {option[filteredBy] + (secondaryLabel ? ` (${option[secondaryLabel]})` : "")}
(secondaryLabel ? ` (${option[secondaryLabel]})` : "")}
</ListItem> </ListItem>
); );
}} }}
@@ -0,0 +1,79 @@
import { useTheme } from "@emotion/react";
import { Box, Stack, Typography } from "@mui/material";
import Search from "../../../../Components/Inputs/Search";
import MemoizedMonitorTable from "../MonitorTable";
import { useState } from "react";
import useDebounce from "../../../../Utils/debounce";
import PropTypes from "prop-types";
const CurrentMonitoring = ({ totalMonitors, monitors, isAdmin }) => {
const theme = useTheme();
const [search, setSearch] = useState("");
const [isSearching, setIsSearching] = useState(false);
const debouncedFilter = useDebounce(search, 500);
const handleSearch = (value) => {
setIsSearching(true);
setSearch(value);
};
return (
<Box
flex={1}
px={theme.spacing(10)}
py={theme.spacing(8)}
border={1}
borderColor={theme.palette.border.light}
borderRadius={theme.shape.borderRadius}
backgroundColor={theme.palette.background.main}
>
<Stack
direction="row"
alignItems="center"
mb={theme.spacing(8)}
>
<Typography
component="h2"
variant="h2"
fontWeight={500}
letterSpacing={-0.2}
>
Actively monitoring
</Typography>
<Box
className="current-monitors-counter"
color={theme.palette.text.primary}
border={1}
borderColor={theme.palette.border.light}
backgroundColor={theme.palette.background.accent}
>
{totalMonitors}
</Box>
<Box
width="25%"
minWidth={150}
ml="auto"
>
<Search
options={monitors}
filteredBy="name"
inputValue={search}
handleInputChange={handleSearch}
/>
</Box>
</Stack>
<MemoizedMonitorTable
isAdmin={isAdmin}
filter={debouncedFilter}
setIsSearching={setIsSearching}
isSearching={isSearching}
/>
</Box>
);
};
CurrentMonitoring.propTypes = {
totalMonitors: PropTypes.number,
monitors: PropTypes.array,
isAdmin: PropTypes.bool,
};
export { CurrentMonitoring };
@@ -109,7 +109,7 @@ TablePaginationActions.propTypes = {
onPageChange: PropTypes.func.isRequired, onPageChange: PropTypes.func.isRequired,
}; };
const MonitorTable = ({ isAdmin, filter, setLoading, isSearching }) => { const MonitorTable = ({ isAdmin, filter, setIsSearching, isSearching }) => {
const theme = useTheme(); const theme = useTheme();
const navigate = useNavigate(); const navigate = useNavigate();
const dispatch = useDispatch(); const dispatch = useDispatch();
@@ -162,15 +162,25 @@ const MonitorTable = ({ isAdmin, filter, setLoading, isSearching }) => {
}); });
setMonitors(res?.data?.data?.monitors ?? []); setMonitors(res?.data?.data?.monitors ?? []);
setMonitorCount(res?.data?.data?.monitorCount ?? 0); setMonitorCount(res?.data?.data?.monitorCount ?? 0);
setLoading(false);
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
} finally {
setIsSearching(false);
} }
}, [authState, page, rowsPerPage, filter, sort, setLoading]); }, [authState, page, rowsPerPage, filter, sort, setIsSearching]);
useEffect(() => { useEffect(() => {
fetchPage(); fetchPage();
}, [updateTrigger, authState, page, rowsPerPage, filter, sort, setLoading, fetchPage]); }, [
updateTrigger,
authState,
page,
rowsPerPage,
filter,
sort,
setIsSearching,
fetchPage,
]);
// Listen for changes in filter, if new value reset the page // Listen for changes in filter, if new value reset the page
useEffect(() => { useEffect(() => {
@@ -459,7 +469,7 @@ const MonitorTable = ({ isAdmin, filter, setLoading, isSearching }) => {
MonitorTable.propTypes = { MonitorTable.propTypes = {
isAdmin: PropTypes.bool, isAdmin: PropTypes.bool,
filter: PropTypes.string, filter: PropTypes.string,
setLoading: PropTypes.func, setIsSearching: PropTypes.func,
isSearching: PropTypes.bool, isSearching: PropTypes.bool,
}; };
+8 -64
View File
@@ -1,19 +1,17 @@
import "./index.css"; import "./index.css";
import { useEffect, useState } from "react"; import { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux"; 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 { Box, Button, Stack, Typography } from "@mui/material"; import { Box, Button, Stack } 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";
import StatusBox from "./StatusBox"; import StatusBox from "./StatusBox";
import Breadcrumbs from "../../../Components/Breadcrumbs"; import Breadcrumbs from "../../../Components/Breadcrumbs";
import Greeting from "../../../Utils/greeting"; import Greeting from "../../../Utils/greeting";
import MonitorTable from "./MonitorTable"; import { CurrentMonitoring } from "./CurrentMonitoring";
import Search from "../../../Components/Inputs/Search";
import useDebounce from "../../../Utils/debounce";
const Monitors = ({ isAdmin }) => { const Monitors = ({ isAdmin }) => {
const theme = useTheme(); const theme = useTheme();
@@ -22,20 +20,12 @@ const Monitors = ({ isAdmin }) => {
const authState = useSelector((state) => state.auth); const authState = useSelector((state) => state.auth);
const dispatch = useDispatch({}); const dispatch = useDispatch({});
//TODO create components, and lower these states.
const [search, setSearch] = useState("");
const [isSearching, setIsSearching] = useState(false);
const debouncedFilter = useDebounce(search, 500);
const handleSearch = (value) => {
setIsSearching(true);
setSearch(value);
};
useEffect(() => { useEffect(() => {
dispatch(getUptimeMonitorsByTeamId(authState.authToken)); dispatch(getUptimeMonitorsByTeamId(authState.authToken));
}, [authState.authToken, dispatch]); }, [authState.authToken, dispatch]);
//TODO bring fetching to this component, like on pageSpeed
const loading = monitorState?.isLoading; const loading = monitorState?.isLoading;
const totalMonitors = monitorState?.monitorsSummary?.monitorCounts?.total; const totalMonitors = monitorState?.monitorsSummary?.monitorCounts?.total;
@@ -100,57 +90,11 @@ const Monitors = ({ isAdmin }) => {
value={monitorState?.monitorsSummary?.monitorCounts?.paused ?? 0} value={monitorState?.monitorsSummary?.monitorCounts?.paused ?? 0}
/> />
</Stack> </Stack>
<Box <CurrentMonitoring
flex={1}
px={theme.spacing(10)}
py={theme.spacing(8)}
border={1}
borderColor={theme.palette.border.light}
borderRadius={theme.shape.borderRadius}
backgroundColor={theme.palette.background.main}
>
<Stack
direction="row"
alignItems="center"
mb={theme.spacing(8)}
>
<Typography
component="h2"
variant="h2"
fontWeight={500}
letterSpacing={-0.2}
>
Actively monitoring
</Typography>
<Box
className="current-monitors-counter"
color={theme.palette.text.primary}
border={1}
borderColor={theme.palette.border.light}
backgroundColor={theme.palette.background.accent}
>
{totalMonitors}
</Box>
<Box
width="25%"
minWidth={150}
ml="auto"
>
<Search
options={monitorState.monitorsSummary.monitors}
filteredBy="name"
inputValue={search}
handleInputChange={handleSearch}
/>
</Box>
</Stack>
<MonitorTable
isAdmin={isAdmin} isAdmin={isAdmin}
filter={debouncedFilter} monitors={monitorState.monitorsSummary.monitors}
setLoading={setIsSearching} totalMonitors={totalMonitors}
isSearching={isSearching}
/> />
</Box>
</> </>
)} )}
</> </>
+14 -7
View File
@@ -19,7 +19,7 @@ const PageSpeed = ({ isAdmin }) => {
const navigate = useNavigate(); const navigate = useNavigate();
const { user, authToken } = useSelector((state) => state.auth); const { user, authToken } = useSelector((state) => state.auth);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(true);
const [monitors, setMonitors] = useState([]); const [monitors, setMonitors] = useState([]);
useEffect(() => { useEffect(() => {
dispatch(getPageSpeedByTeamId(authToken)); dispatch(getPageSpeedByTeamId(authToken));
@@ -59,6 +59,7 @@ const PageSpeed = ({ isAdmin }) => {
// will show skeletons only on initial load // will show skeletons only on initial load
// since monitor state is being added to redux persist, there's no reason to display skeletons on every render // since monitor state is being added to redux persist, there's no reason to display skeletons on every render
let isActuallyLoading = isLoading && monitors?.length === 0; let isActuallyLoading = isLoading && monitors?.length === 0;
console.log({ isActuallyLoading });
return ( return (
<Box <Box
className="page-speed" className="page-speed"
@@ -74,10 +75,6 @@ const PageSpeed = ({ isAdmin }) => {
}, },
}} }}
> >
{isActuallyLoading ? (
<SkeletonLayout />
) : monitors?.length !== 0 ? (
<Box>
<Box mb={theme.spacing(12)}> <Box mb={theme.spacing(12)}>
<Breadcrumbs list={[{ name: `pagespeed`, path: "/pagespeed" }]} /> <Breadcrumbs list={[{ name: `pagespeed`, path: "/pagespeed" }]} />
<Stack <Stack
@@ -99,9 +96,19 @@ const PageSpeed = ({ isAdmin }) => {
)} )}
</Stack> </Stack>
</Box> </Box>
<Grid container spacing={theme.spacing(12)}> {isActuallyLoading ? (
<SkeletonLayout />
) : monitors?.length !== 0 ? (
<Box>
<Grid
container
spacing={theme.spacing(12)}
>
{monitors?.map((monitor) => ( {monitors?.map((monitor) => (
<Card monitor={monitor} key={monitor._id} /> <Card
monitor={monitor}
key={monitor._id}
/>
))} ))}
</Grid> </Grid>
</Box> </Box>