Files
Checkmate/client/src/Components/Image/index.jsx
T

72 lines
1.4 KiB
React
Raw Normal View History

2025-02-04 13:06:42 -08:00
import { Box } from "@mui/material";
import PropTypes from "prop-types";
const isValidBase64Image = (data) => {
return /^[A-Za-z0-9+/=]+$/.test(data);
};
2025-02-04 13:06:42 -08:00
const Image = ({
shouldRender = true,
2025-02-04 13:06:42 -08:00
src,
alt,
width = "auto",
height = "auto",
minWidth = "auto",
minHeight = "auto",
2025-02-04 13:06:42 -08:00
maxWidth = "auto",
maxHeight = "auto",
base64,
placeholder,
2025-02-05 14:59:08 -08:00
sx,
2025-02-04 13:06:42 -08:00
}) => {
if (shouldRender === false) {
return null;
}
if (typeof src !== "undefined" && typeof base64 !== "undefined") {
console.warn("base64 takes precedence over src and overwrites it");
}
if (typeof base64 !== "undefined" && isValidBase64Image(base64)) {
src = `data:image/png;base64,${base64}`;
}
if (
typeof src === "undefined" &&
typeof base64 === "undefined" &&
typeof placeholder !== "undefined"
) {
src = placeholder;
}
2025-02-04 13:06:42 -08:00
return (
<Box
component="img"
src={src}
alt={alt}
minWidth={minWidth}
minHeight={minHeight}
2025-02-04 13:06:42 -08:00
maxWidth={maxWidth}
maxHeight={maxHeight}
width={width}
height={height}
2025-02-05 14:59:08 -08:00
sx={{ ...sx }}
2025-02-04 13:06:42 -08:00
/>
);
};
Image.propTypes = {
shouldRender: PropTypes.bool,
2025-02-04 13:06:42 -08:00
src: PropTypes.string,
alt: PropTypes.string.isRequired,
width: PropTypes.string,
height: PropTypes.string,
minWidth: PropTypes.string,
minHeight: PropTypes.string,
maxWidth: PropTypes.string,
maxHeight: PropTypes.string,
base64: PropTypes.string,
2025-02-05 14:59:08 -08:00
sx: PropTypes.object,
2025-02-04 13:06:42 -08:00
};
export default Image;