2024-10-25 11:00:29 -03:00
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
import { useTheme } from "@mui/material";
|
|
|
|
|
import { BaseLabel } from "../Label";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @component
|
|
|
|
|
* @param {Object} props
|
2024-11-18 01:41:30 -03:00
|
|
|
* @param {number} props.status - The http status for the label
|
2024-10-25 11:00:29 -03:00
|
|
|
* @param {Styles} props.customStyles - CSS Styles passed from parent component
|
|
|
|
|
* @returns {JSX.Element}
|
|
|
|
|
* @example
|
|
|
|
|
* // Render a http status label
|
2024-10-25 11:21:39 -03:00
|
|
|
* <HttpStatusLabel status={404} />
|
2024-10-25 11:00:29 -03:00
|
|
|
*/
|
|
|
|
|
|
2024-11-18 01:41:30 -03:00
|
|
|
const DEFAULT_CODE = 9999; // Default code for unknown status
|
|
|
|
|
|
|
|
|
|
const handleStatusCode = (status) => {
|
2025-01-21 15:31:40 -08:00
|
|
|
if (status) {
|
2024-11-18 01:41:30 -03:00
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
return DEFAULT_CODE;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-25 11:00:29 -03:00
|
|
|
const getRoundedStatusCode = (status) => {
|
2024-12-02 11:13:23 +08:00
|
|
|
return Math.floor(status / 100) * 100;
|
2024-10-25 11:00:29 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const HttpStatusLabel = ({ status, customStyles }) => {
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const colors = {
|
|
|
|
|
400: {
|
2025-01-12 12:28:13 -05:00
|
|
|
color: theme.palette.warning.main,
|
|
|
|
|
borderColor: theme.palette.warning.lowContrast,
|
2024-10-25 11:00:29 -03:00
|
|
|
},
|
|
|
|
|
500: {
|
2025-01-12 12:28:13 -05:00
|
|
|
color: theme.palette.error.main,
|
|
|
|
|
borderColor: theme.palette.error.lowContrast,
|
2024-10-25 11:00:29 -03:00
|
|
|
},
|
2024-11-18 01:41:30 -03:00
|
|
|
default: {
|
2025-01-12 12:28:13 -05:00
|
|
|
color: theme.palette.primary.contrastText,
|
2025-01-14 19:19:36 -05:00
|
|
|
borderColor: theme.palette.primary.contrastText,
|
2024-10-25 11:00:29 -03:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-02 11:13:23 +08:00
|
|
|
const statusCode = handleStatusCode(status);
|
2024-11-18 01:41:30 -03:00
|
|
|
|
2025-01-12 12:28:13 -05:00
|
|
|
const { borderColor, color } =
|
2024-11-18 01:41:30 -03:00
|
|
|
colors[getRoundedStatusCode(statusCode)] || colors.default;
|
2024-10-25 11:00:29 -03:00
|
|
|
return (
|
|
|
|
|
<BaseLabel
|
2024-11-18 01:41:30 -03:00
|
|
|
label={String(statusCode)}
|
2024-10-25 11:00:29 -03:00
|
|
|
styles={{
|
2025-01-12 12:28:13 -05:00
|
|
|
color: color,
|
2024-10-25 11:00:29 -03:00
|
|
|
borderColor: borderColor,
|
|
|
|
|
...customStyles,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
HttpStatusLabel.propTypes = {
|
2024-12-02 11:13:23 +08:00
|
|
|
status: PropTypes.number,
|
2024-10-25 11:00:29 -03:00
|
|
|
customStyles: PropTypes.object,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { HttpStatusLabel };
|