import { Box, Grid, Stack, Typography } from "@mui/material"; import { useEffect } from "react"; import { useTheme } from "@emotion/react"; import { formatDate, formatDurationRounded } from "../../Utils/timeUtils"; import { StatusLabel } from "../../Components/Label"; import { useDispatch, useSelector } from "react-redux"; import { getPageSpeedByUserId } from "../../Features/PageSpeedMonitor/pageSpeedMonitorSlice"; import PageSpeedIcon from "../../assets/icons/page-speed.svg?react"; import Fallback from "../../Components/Fallback"; import "./index.css"; const Card = ({ data }) => { const theme = useTheme(); /** * Helper function to get duration since last check or the last date checked * @param {Array} checks Array of check objects. * @param {boolean} duration Whether the function should return the duration since last checked or the date itself * @returns {number} Timestamp of the most recent check. */ const getLastChecked = (checks, duration = true) => { if (!checks || checks.length === 0) { return 0; // Handle case when no checks are available } // Data is sorted newest -> oldest, so newest check is the most recent if (!duration) { return new Date(checks[0].createdAt); } return new Date() - new Date(checks[0].createdAt); }; return ( {data.name} {data.url.replace(/^https?:\/\//, "")} Last checked:{" "} {formatDate(getLastChecked(data.checks, false))}{" "} ({formatDurationRounded(getLastChecked(data.checks))} ago) ); }; const PageSpeed = () => { const theme = useTheme(); const dispatch = useDispatch(); const { authToken } = useSelector((state) => state.auth); const { monitors } = useSelector((state) => state.pageSpeedMonitors); useEffect(() => { dispatch(getPageSpeedByUserId(authToken)); }, []); return ( {monitors ? ( All page speed monitors Click on one of the monitors to get more site speed information. {monitors?.map((monitor) => ( ))} ) : ( )} ); }; export default PageSpeed;