import PropTypes from "prop-types"; import { useTheme } from "@mui/material"; import { BaseLabel } from "../Label"; /** * @component * @param {Object} props * @param {number} 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 * */ const DEFAULT_CODE = 9999; // Default code for unknown status const handleStatusCode = (status) => { if (status) { return status; } return DEFAULT_CODE; }; const getRoundedStatusCode = (status) => { return Math.floor(status / 100) * 100; }; const HttpStatusLabel = ({ status, customStyles }) => { const theme = useTheme(); const colors = { 400: { color: theme.palette.warning.main, borderColor: theme.palette.warning.lowContrast, }, 500: { color: theme.palette.error.main, borderColor: theme.palette.error.lowContrast, }, default: { color: theme.palette.primary.contrastText, borderColor: theme.palette.primary.contrastText, }, }; const statusCode = handleStatusCode(status); const { borderColor, color } = colors[getRoundedStatusCode(statusCode)] || colors.default; return ( ); }; HttpStatusLabel.propTypes = { status: PropTypes.number, customStyles: PropTypes.object, }; export { HttpStatusLabel };