Files
Checkmate/Client/src/Pages/PageSpeed/index.jsx
T

209 lines
5.9 KiB
React
Raw Normal View History

2024-08-07 17:52:46 -04:00
import { Box, Grid, Skeleton, Stack, Typography } from "@mui/material";
2024-07-30 13:40:31 -04:00
import { useEffect } from "react";
2024-07-28 16:51:47 -04:00
import { useTheme } from "@emotion/react";
import { formatDate, formatDurationRounded } from "../../Utils/timeUtils";
import { StatusLabel } from "../../Components/Label";
2024-07-30 13:40:31 -04:00
import { useDispatch, useSelector } from "react-redux";
2024-08-27 12:07:30 -07:00
import { getPageSpeedByTeamId } from "../../Features/PageSpeedMonitor/pageSpeedMonitorSlice";
2024-07-30 13:40:31 -04:00
import PageSpeedIcon from "../../assets/icons/page-speed.svg?react";
import Fallback from "../../Components/Fallback";
import "./index.css";
import Button from "../../Components/Button";
import { useNavigate } from "react-router";
2024-08-03 12:04:40 -04:00
import { getLastChecked } from "../../Utils/monitorUtils";
2024-08-20 10:55:54 -07:00
import PropTypes from "prop-types";
2024-07-28 16:51:47 -04:00
const Card = ({ data }) => {
const theme = useTheme();
2024-08-02 18:17:52 -04:00
const navigate = useNavigate();
2024-07-28 16:51:47 -04:00
return (
2024-08-02 18:17:52 -04:00
<Grid
item
lg={6}
flexGrow={1}
2024-08-26 17:22:21 -04:00
sx={{
"&:hover > .MuiStack-root": {
backgroundColor: "var(--primary-bg-accent)",
},
}}
2024-08-02 18:17:52 -04:00
>
<Stack
direction="row"
gap={theme.gap.medium}
p={theme.gap.ml}
onClick={() => navigate(`/pagespeed/${data._id}`)}
sx={{ cursor: "pointer" }}
>
2024-07-28 16:51:47 -04:00
<PageSpeedIcon style={{ width: theme.gap.ml, height: theme.gap.ml }} />
<Box flex={1}>
<Stack direction="row" justifyContent="space-between">
<Typography component="h2" mb={theme.gap.xs}>
{data.name}
</Typography>
<StatusLabel
2024-07-30 15:44:41 -04:00
status={data.status ? "up" : "cannot resolve"}
text={data.status ? "Live (collecting data)" : "Inactive"}
2024-07-28 16:51:47 -04:00
/>
</Stack>
2024-07-30 15:44:41 -04:00
<Typography>{data.url.replace(/^https?:\/\//, "")}</Typography>
2024-07-28 16:51:47 -04:00
<Typography mt={theme.gap.large}>
<Typography component="span" fontWeight={600}>
Last checked:{" "}
</Typography>
{formatDate(getLastChecked(data.checks, false))}{" "}
<Typography component="span" fontStyle="italic">
({formatDurationRounded(getLastChecked(data.checks))} ago)
</Typography>
</Typography>
</Box>
</Stack>
</Grid>
);
};
2024-08-20 10:55:54 -07:00
Card.propTypes = {
data: PropTypes.object.isRequired,
};
2024-08-07 19:49:08 -04:00
/**
* Renders a skeleton layout.
*
* @returns {JSX.Element}
*/
const SkeletonLayout = () => {
const theme = useTheme();
return (
<Stack gap={theme.gap.xs}>
<Stack
direction="row"
justifyContent="space-between"
mb={theme.gap.large}
>
<Box width="80%">
<Skeleton variant="rounded" width="25%" height={24} />
<Skeleton
variant="rounded"
width="50%"
height={19.5}
sx={{ mt: theme.gap.xs }}
/>
</Box>
<Skeleton
variant="rounded"
width="20%"
height={34}
sx={{ alignSelf: "flex-end" }}
/>
</Stack>
<Stack direction="row" flexWrap="wrap" gap={theme.gap.large}>
<Skeleton
variant="rounded"
width="100%"
height={120}
sx={{ flex: "35%" }}
/>
<Skeleton
variant="rounded"
width="100%"
height={120}
sx={{ flex: "35%" }}
/>
<Skeleton
variant="rounded"
width="100%"
height={120}
sx={{ flex: "35%" }}
/>
<Skeleton
variant="rounded"
width="100%"
height={120}
sx={{ flex: "35%" }}
/>
</Stack>
</Stack>
);
};
const PageSpeed = ({ isAdmin }) => {
2024-07-28 16:51:47 -04:00
const theme = useTheme();
2024-07-30 13:40:31 -04:00
const dispatch = useDispatch();
const navigate = useNavigate();
2024-07-28 16:51:47 -04:00
2024-07-30 13:40:31 -04:00
const { authToken } = useSelector((state) => state.auth);
2024-08-07 17:52:46 -04:00
const { monitors, isLoading } = useSelector(
(state) => state.pageSpeedMonitors
);
2024-07-30 13:40:31 -04:00
useEffect(() => {
2024-08-27 12:07:30 -07:00
dispatch(getPageSpeedByTeamId(authToken));
2024-08-20 10:55:54 -07:00
}, [authToken, dispatch]);
2024-07-28 16:51:47 -04:00
// 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
let isActuallyLoading = isLoading && monitors.length === 0;
2024-07-28 16:51:47 -04:00
return (
2024-08-27 20:11:59 -04:00
<Box
className="page-speed"
pt={theme.gap.xl}
sx={{
':has(> [class*="fallback__"])': {
position: "relative",
border: 1,
borderColor: theme.palette.border.light,
borderRadius: theme.shape.borderRadius,
borderStyle: "dashed",
backgroundColor: theme.palette.background.main,
overflow: "hidden",
},
}}
>
{isActuallyLoading ? (
2024-08-07 19:49:08 -04:00
<SkeletonLayout />
2024-08-07 17:52:46 -04:00
) : monitors?.length !== 0 ? (
2024-07-28 16:51:47 -04:00
<Stack gap={theme.gap.xs}>
<Stack
direction="row"
justifyContent="space-between"
mb={theme.gap.large}
>
<Box>
<Typography component="h1">All page speed monitors</Typography>
<Typography mt={theme.gap.xs}>
Click on one of the monitors to get more site speed information.
</Typography>
</Box>
<Button
level="primary"
label="Create new"
2024-08-02 18:08:36 -04:00
onClick={() => navigate("/pagespeed/create")}
/>
</Stack>
2024-07-28 16:51:47 -04:00
<Grid container spacing={theme.gap.large}>
2024-07-30 13:40:31 -04:00
{monitors?.map((monitor) => (
2024-07-28 16:51:47 -04:00
<Card data={monitor} key={`monitor-${monitor._id}`} />
))}
</Grid>
</Stack>
) : (
<Fallback
title="page speed"
checks={[
"Report on the user experience of a page",
"Help analyze webpage speed",
"Give suggestions on how the page can be improved",
]}
2024-08-02 18:08:36 -04:00
link="/pagespeed/create"
isAdmin={isAdmin}
2024-07-28 16:51:47 -04:00
/>
)}
</Box>
);
};
PageSpeed.propTypes = {
isAdmin: PropTypes.bool,
};
2024-07-28 16:51:47 -04:00
export default PageSpeed;