diff --git a/client/src/Components/v2/inputs/ImageUpload.tsx b/client/src/Components/v2/inputs/ImageUpload.tsx new file mode 100644 index 000000000..6c9fc1783 --- /dev/null +++ b/client/src/Components/v2/inputs/ImageUpload.tsx @@ -0,0 +1,164 @@ +import { Box, Stack, Typography, IconButton } from "@mui/material"; +import { useTheme } from "@mui/material/styles"; +import { Upload, X } from "lucide-react"; +import { useState, useRef, useCallback } from "react"; +import { useTranslation } from "react-i18next"; + +interface FileObject { + src: string; + name: string; + file: File; +} + +interface ImageUploadProps { + src?: string; + onChange?: (file: FileObject | undefined) => void; + maxSize?: number; + accept?: string[]; + error?: string; +} + +export const ImageUpload = ({ + src, + onChange, + maxSize = 3 * 1024 * 1024, + accept = ["jpg", "jpeg", "png"], + error, +}: ImageUploadProps) => { + const theme = useTheme(); + const { t } = useTranslation(); + const inputRef = useRef(null); + const [isDragging, setIsDragging] = useState(false); + const [localError, setLocalError] = useState(null); + const [preview, setPreview] = useState(null); + + const handleFile = useCallback( + (file: File | undefined) => { + if (!file) return; + const isValidType = accept.some((type) => file.type.includes(type)); + const isValidSize = file.size <= maxSize; + if (!isValidType) { + setLocalError(t("common.errors.invalidFileFormat")); + return; + } + if (!isValidSize) { + setLocalError(t("common.errors.invalidFileSize")); + return; + } + setLocalError(null); + const fileObj: FileObject = { + src: URL.createObjectURL(file), + name: file.name, + file, + }; + setPreview(fileObj); + onChange?.(fileObj); + }, + [maxSize, accept, onChange, t] + ); + + const handleClear = () => { + setPreview(null); + setLocalError(null); + onChange?.(undefined); + if (inputRef.current) inputRef.current.value = ""; + }; + + const displaySrc = src || preview?.src; + const displayError = localError || error; + + return ( + + {displaySrc ? ( + + + + + + + ) : ( + { + e.preventDefault(); + setIsDragging(true); + }} + onDragLeave={() => setIsDragging(false)} + onDrop={(e) => { + e.preventDefault(); + setIsDragging(false); + handleFile(e.dataTransfer.files?.[0]); + }} + alignItems="center" + justifyContent="center" + gap={1} + sx={{ + position: "relative", + width: "100%", + minHeight: 150, + border: "2px dashed", + borderRadius: 1, + borderColor: isDragging ? theme.palette.primary.main : theme.palette.divider, + backgroundColor: isDragging ? theme.palette.action.hover : "transparent", + transition: "0.2s", + cursor: "pointer", + "&:hover": { + borderColor: theme.palette.primary.main, + backgroundColor: theme.palette.action.hover, + }, + }} + > + `.${ext}`).join(",")} + onChange={(e) => handleFile(e.target.files?.[0])} + style={{ position: "absolute", inset: 0, opacity: 0, cursor: "pointer" }} + /> + + + + {t("common.imageUpload.clickToUpload")} + {" "} + {t("common.imageUpload.orDragAndDrop")} + + + {accept.join(", ").toUpperCase()} • {t("common.imageUpload.maxSize")}{" "} + {Math.round(maxSize / 1024 / 1024)}MB + + + )} + {displayError && ( + + {displayError} + + )} + + ); +}; diff --git a/client/src/Components/v2/inputs/index.tsx b/client/src/Components/v2/inputs/index.tsx index 1618daf76..ad7c984bd 100644 --- a/client/src/Components/v2/inputs/index.tsx +++ b/client/src/Components/v2/inputs/index.tsx @@ -12,3 +12,4 @@ export { DialogInput as Dialog } from "./Dialog"; export * from "./Radio"; export * from "./Switch"; export * from "./Slider"; +export * from "./ImageUpload"; diff --git a/client/src/Pages/StatusPage/Create/index.tsx b/client/src/Pages/StatusPage/Create/index.tsx new file mode 100644 index 000000000..25c4d03f8 --- /dev/null +++ b/client/src/Pages/StatusPage/Create/index.tsx @@ -0,0 +1,46 @@ +import { BasePage, ConfigBox } from "@/Components/v2/design-elements"; +import Stack from "@mui/material/Stack"; +import Typography from "@mui/material/Typography"; +import { ImageUpload, SwitchComponent } from "@/Components/v2/inputs"; + +import { useTheme } from "@mui/material/styles"; +import { useTranslation } from "react-i18next"; + +const CreateStatusPage = () => { + const theme = useTheme(); + const { t } = useTranslation(); + return ( + + + field.onChange(e.target.checked)} + /> + + {t("pages.statusPages.form.access.option.published.name")} + + + } + /> + + + + } + /> + + ); +}; + +export default CreateStatusPage; diff --git a/client/src/Pages/StatusPage/Create/index.jsx b/client/src/Pages/StatusPage/Create/old.jsx similarity index 100% rename from client/src/Pages/StatusPage/Create/index.jsx rename to client/src/Pages/StatusPage/Create/old.jsx diff --git a/client/src/Routes/index.jsx b/client/src/Routes/index.jsx index bbba8eac0..0b24ac03e 100644 --- a/client/src/Routes/index.jsx +++ b/client/src/Routes/index.jsx @@ -36,7 +36,7 @@ import Checks from "../Pages/Checks/index"; import Incidents from "../Pages/Incidents/"; // Status pages -import CreateStatus from "../Pages/StatusPage/Create/index.jsx"; +import CreateStatus from "../Pages/StatusPage/Create/"; import StatusPages from "../Pages/StatusPage/StatusPages"; import Status from "../Pages/StatusPage/Status"; @@ -249,7 +249,13 @@ const Routes = () => { } + element={ + <> + + + + + } />