safe param access

This commit is contained in:
Alex Holliday committed 2025-01-29 11:42:40 -08:00
1 parent 4c3b348642
commit 80a16ef61a
5 files changed
+69 -80

No files matched your search

@@ -118,8 +118,6 @@ const PieChart = ({ audits }) => {
const [highlightedItem, setHighLightedItem] = useState(null); const [highlightedItem, setHighLightedItem] = useState(null);
const [expand, setExpand] = useState(false); const [expand, setExpand] = useState(false);
if (typeof audits === "undefined") return null;
/** /**
* Retrieves color properties based on the performance value. * Retrieves color properties based on the performance value.
* *
@@ -166,6 +164,8 @@ const PieChart = ({ audits }) => {
*/ */
let performance = 0; let performance = 0;
const getPieData = (audits) => { const getPieData = (audits) => {
if (typeof audits === "undefined") return undefined;
let data = []; let data = [];
let startAngle = 0; let startAngle = 0;
const padding = 3; // padding between arcs const padding = 3; // padding between arcs
@@ -7,84 +7,84 @@ import PropTypes from "prop-types";
const PieChartLegend = ({ audits }) => { const PieChartLegend = ({ audits }) => {
const theme = useTheme(); const theme = useTheme();
if (typeof audits === "undefined") return null;
return ( return (
<LegendBox <LegendBox
icon={<SpeedometerIcon />} icon={<SpeedometerIcon />}
header="Performance metrics" header="Performance metrics"
sx={{ flex: 1 }} sx={{ flex: 1 }}
> >
{Object.keys(audits).map((key) => { {typeof audits !== "undefined" &&
if (key === "_id") return; Object.keys(audits).map((key) => {
if (key === "_id") return;
let audit = audits[key]; let audit = audits[key];
let score = audit.score * 100; let score = audit.score * 100;
let bg = let bg =
score >= 90 score >= 90
? theme.palette.success.main ? theme.palette.success.main
: score >= 50 : score >= 50
? theme.palette.warning.main ? theme.palette.warning.main
: score >= 0 : score >= 0
? theme.palette.error.main ? theme.palette.error.main
: theme.palette.tertiary.main; : theme.palette.tertiary.main;
// Find the position where the number ends and the unit begins // Find the position where the number ends and the unit begins
const match = audit.displayValue.match(/(\d+\.?\d*)\s*([a-zA-Z]+)/); const match = audit.displayValue.match(/(\d+\.?\d*)\s*([a-zA-Z]+)/);
let value; let value;
let unit; let unit;
if (match) { if (match) {
value = match[1]; value = match[1];
match[2] === "s" ? (unit = "seconds") : (unit = match[2]); match[2] === "s" ? (unit = "seconds") : (unit = match[2]);
} else { } else {
value = audit.displayValue; value = audit.displayValue;
} }
return ( return (
<Stack <Stack
flex={1} flex={1}
key={`${key}-box`} key={`${key}-box`}
justifyContent="space-between" justifyContent="space-between"
direction="row" direction="row"
gap={theme.spacing(4)} gap={theme.spacing(4)}
p={theme.spacing(3)} p={theme.spacing(3)}
border={1} border={1}
borderColor={theme.palette.primary.lowContrast} borderColor={theme.palette.primary.lowContrast}
borderRadius={4} borderRadius={4}
> >
<Box> <Box>
<Typography <Typography
fontSize={12} fontSize={12}
fontWeight={500} fontWeight={500}
lineHeight={1} lineHeight={1}
mb={1} mb={1}
textTransform="uppercase" textTransform="uppercase"
> >
{audit.title} {audit.title}
</Typography> </Typography>
<Typography
component="span"
fontSize={14}
fontWeight={500}
color={theme.palette.primary.contrastText}
>
{value}
<Typography <Typography
component="span" component="span"
variant="body2" fontSize={14}
ml={2} fontWeight={500}
color={theme.palette.primary.contrastText}
> >
{unit} {value}
<Typography
component="span"
variant="body2"
ml={2}
>
{unit}
</Typography>
</Typography> </Typography>
</Typography> </Box>
</Box> <Box
<Box width={4}
width={4} backgroundColor={bg}
backgroundColor={bg} borderRadius={4}
borderRadius={4} />
/> </Stack>
</Stack> );
); })}
})}
</LegendBox> </LegendBox>
); );
}; };
@@ -15,10 +15,6 @@ const PageSpeedAreaChart = ({ shouldRender, monitor, metrics, handleMetrics }) =
return <SkeletonLayout />; return <SkeletonLayout />;
} }
if (typeof monitor === "undefined") {
return null;
}
const data = monitor?.checks ? [...monitor.checks].reverse() : []; const data = monitor?.checks ? [...monitor.checks].reverse() : [];
return ( return (
@@ -5,18 +5,14 @@ import { Typography } from "@mui/material";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import PieChartLegend from "../Charts/PieChartLegend"; import PieChartLegend from "../Charts/PieChartLegend";
import SkeletonLayout from "./skeleton"; import SkeletonLayout from "./skeleton";
const PerformanceReport = ({ shouldRender, audits }) => { const PerformanceReport = ({ shouldRender, audits }) => {
console.log("shouldRender", shouldRender);
const theme = useTheme(); const theme = useTheme();
if (!shouldRender) { if (!shouldRender) {
return <SkeletonLayout />; return <SkeletonLayout />;
} }
if (typeof audits === "undefined") {
return null;
}
return ( return (
<ChartBox <ChartBox
icon={<PerformanceIcon />} icon={<PerformanceIcon />}
+1 -4
View File
@@ -1,11 +1,10 @@
// Components // Components
import { Stack, Typography } from "@mui/material"; import { Stack, Typography, Skeleton } from "@mui/material";
import Breadcrumbs from "../../../Components/Breadcrumbs"; import Breadcrumbs from "../../../Components/Breadcrumbs";
import MonitorStatusHeader from "../../../Components/MonitorStatusHeader"; import MonitorStatusHeader from "../../../Components/MonitorStatusHeader";
import PageSpeedStatusBoxes from "./Components/PageSpeedStatusBoxes"; import PageSpeedStatusBoxes from "./Components/PageSpeedStatusBoxes";
import PageSpeedAreaChart from "./Components/PageSpeedAreaChart"; import PageSpeedAreaChart from "./Components/PageSpeedAreaChart";
import PerformanceReport from "./Components/PerformanceReport"; import PerformanceReport from "./Components/PerformanceReport";
import Fallback from "../../../Components/Fallback";
// Utils // Utils
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { useIsAdmin } from "../../../Hooks/useIsAdmin"; import { useIsAdmin } from "../../../Hooks/useIsAdmin";
@@ -31,8 +30,6 @@ const PageSpeedDetails = () => {
monitorId, monitorId,
}); });
console.log(monitor, audits, isLoading);
const [metrics, setMetrics] = useState({ const [metrics, setMetrics] = useState({
accessibility: true, accessibility: true,
bestPractices: true, bestPractices: true,