Refact: create a new HttpStatusLabel component
This commit is contained in:
1 parent
1347ccda89
commit
95a0ad0dbd
3 files changed
+78
-35
No files matched your search
@@ -0,0 +1,65 @@
|
|||||||
|
import PropTypes from "prop-types";
|
||||||
|
import { useTheme } from "@mui/material";
|
||||||
|
import { BaseLabel } from "../Label";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @component
|
||||||
|
* @param {Object} props
|
||||||
|
* @param {number | 'N/A'} props.status - The http status for the label
|
||||||
|
* @param {Styles} props.customStyles - CSS Styles passed from parent component
|
||||||
|
* @returns {JSX.Element}
|
||||||
|
* @example
|
||||||
|
* // Render a http status label
|
||||||
|
* <HttpStatusLabel status="404" />
|
||||||
|
*/
|
||||||
|
|
||||||
|
const getRoundedStatusCode = (status) => {
|
||||||
|
if (typeof status === "number" && status >= 100 && status < 600) {
|
||||||
|
return Math.floor(status / 100) * 100;
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
};
|
||||||
|
|
||||||
|
const HttpStatusLabel = ({ status, customStyles }) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
const colors = {
|
||||||
|
400: {
|
||||||
|
dotColor: theme.palette.warning.main,
|
||||||
|
bgColor: theme.palette.warning.bg,
|
||||||
|
borderColor: theme.palette.warning.light,
|
||||||
|
},
|
||||||
|
500: {
|
||||||
|
dotColor: theme.palette.error.main,
|
||||||
|
bgColor: theme.palette.error.bg,
|
||||||
|
borderColor: theme.palette.error.light,
|
||||||
|
},
|
||||||
|
"N/A": {
|
||||||
|
dotColor: theme.palette.unresolved.main,
|
||||||
|
bgColor: theme.palette.unresolved.bg,
|
||||||
|
borderColor: theme.palette.unresolved.light,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const { borderColor, bgColor, dotColor } =
|
||||||
|
colors[getRoundedStatusCode(status)] || colors["N/A"];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<BaseLabel
|
||||||
|
label={String(status)}
|
||||||
|
styles={{
|
||||||
|
color: dotColor,
|
||||||
|
backgroundColor: bgColor,
|
||||||
|
borderColor: borderColor,
|
||||||
|
...customStyles,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: Restrict status type to accept only numeric values after "N/A" status in IncidentTable is resolved
|
||||||
|
HttpStatusLabel.propTypes = {
|
||||||
|
status: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf(["N/A"])]).isRequired,
|
||||||
|
customStyles: PropTypes.object,
|
||||||
|
};
|
||||||
|
|
||||||
|
export { HttpStatusLabel };
|
||||||
@@ -116,14 +116,13 @@ ColoredLabel.propTypes = {
|
|||||||
* @param {Object} props
|
* @param {Object} props
|
||||||
* @param { 'up' | 'down' | 'cannot resolve'} props.status - The status for the label
|
* @param { 'up' | 'down' | 'cannot resolve'} props.status - The status for the label
|
||||||
* @param {string} props.text - The text of the label
|
* @param {string} props.text - The text of the label
|
||||||
* @param {boolean} props.hasDot - Whether to show a dot or not
|
|
||||||
* @returns {JSX.Element}
|
* @returns {JSX.Element}
|
||||||
* @example
|
* @example
|
||||||
* // Render an active label
|
* // Render an active label
|
||||||
* <StatusLabel status="up" text="Active" />
|
* <StatusLabel status="up" text="Active" />
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const StatusLabel = ({ status, text, hasDot = true, customStyles }) => {
|
const StatusLabel = ({ status, text, customStyles }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const colors = {
|
const colors = {
|
||||||
up: {
|
up: {
|
||||||
@@ -176,32 +175,21 @@ const StatusLabel = ({ status, text, hasDot = true, customStyles }) => {
|
|||||||
...customStyles,
|
...customStyles,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{hasDot && (
|
<Box
|
||||||
<Box
|
width={7}
|
||||||
width={7}
|
height={7}
|
||||||
height={7}
|
bgcolor={dotColor}
|
||||||
bgcolor={dotColor}
|
borderRadius="50%"
|
||||||
borderRadius="50%"
|
marginRight="5px"
|
||||||
marginRight="5px"
|
/>
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</BaseLabel>
|
</BaseLabel>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
StatusLabel.propTypes = {
|
StatusLabel.propTypes = {
|
||||||
status: PropTypes.oneOf([
|
status: PropTypes.oneOf(["up", "down", "paused", "pending", "cannot resolve"]),
|
||||||
"up",
|
|
||||||
"down",
|
|
||||||
"paused",
|
|
||||||
"pending",
|
|
||||||
"400",
|
|
||||||
"500",
|
|
||||||
"cannot resolve",
|
|
||||||
]),
|
|
||||||
text: PropTypes.string,
|
text: PropTypes.string,
|
||||||
hasDot: PropTypes.bool,
|
|
||||||
customStyles: PropTypes.object,
|
customStyles: PropTypes.object,
|
||||||
};
|
};
|
||||||
|
|
||||||
export { ColoredLabel, StatusLabel };
|
export { BaseLabel, ColoredLabel, StatusLabel };
|
||||||
@@ -24,6 +24,7 @@ import { useTheme } from "@emotion/react";
|
|||||||
import { formatDateWithTz } from "../../../Utils/timeUtils";
|
import { formatDateWithTz } from "../../../Utils/timeUtils";
|
||||||
import PlaceholderLight from "../../../assets/Images/data_placeholder.svg?react";
|
import PlaceholderLight from "../../../assets/Images/data_placeholder.svg?react";
|
||||||
import PlaceholderDark from "../../../assets/Images/data_placeholder_dark.svg?react";
|
import PlaceholderDark from "../../../assets/Images/data_placeholder_dark.svg?react";
|
||||||
|
import { HttpStatusLabel } from "../../../Components/HttpStatusLabel";
|
||||||
|
|
||||||
const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
|
const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
|
||||||
const uiTimezone = useSelector((state) => state.ui.timezone);
|
const uiTimezone = useSelector((state) => state.ui.timezone);
|
||||||
@@ -182,13 +183,7 @@ const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
|
|||||||
"YYYY-MM-DD HH:mm:ss A",
|
"YYYY-MM-DD HH:mm:ss A",
|
||||||
uiTimezone
|
uiTimezone
|
||||||
);
|
);
|
||||||
const getGenericStatus = (status) => {
|
|
||||||
if (typeof status === "number" && status >= 100 && status < 600) {
|
|
||||||
return Math.floor(status / 100) * 100;
|
|
||||||
}
|
|
||||||
return "cannot resolve";
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TableRow key={check._id}>
|
<TableRow key={check._id}>
|
||||||
<TableCell>{monitors[check.monitorId]?.name}</TableCell>
|
<TableCell>{monitors[check.monitorId]?.name}</TableCell>
|
||||||
@@ -201,12 +196,7 @@ const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
|
|||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>{formattedDate}</TableCell>
|
<TableCell>{formattedDate}</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<StatusLabel
|
<HttpStatusLabel status={check.statusCode || "N/A"} />
|
||||||
status={getGenericStatus(check.statusCode)}
|
|
||||||
text={String(check.statusCode || "N/A")}
|
|
||||||
hasDot={false}
|
|
||||||
customStyles={{ textTransform: "capitalize" }}
|
|
||||||
/>
|
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>{check.message}</TableCell>
|
<TableCell>{check.message}</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
|
|||||||
Reference in new issue
Block a user