// Components import { Stack, Typography, Button, Box } from "@mui/material"; import ConfigBox from "../../../Components/ConfigBox"; import Checkbox from "../../../Components/Inputs/Checkbox"; import TextInput from "../../../Components/Inputs/TextInput"; import VisuallyHiddenInput from "./Components/VisuallyHiddenInput"; import Image from "../../../Components/Image"; import LogoPlaceholder from "../../../assets/Images/logo_placeholder.svg"; import Breadcrumbs from "../../../Components/Breadcrumbs"; // Utils import { useTheme } from "@emotion/react"; import { useState, useEffect } from "react"; import { useParams } from "react-router-dom"; import { useStatusPageFetch } from "../../StatusPage/Status/Hooks/useStatusPageFetch"; import { useCreateStatusPage } from "../../StatusPage/Create/Hooks/useCreateStatusPage"; import { statusPageValidation } from "../../../Validation/validation"; import { buildErrors } from "../../../Validation/error"; import { createToast } from "../../../Utils/toastUtils"; import { useNavigate } from "react-router-dom"; const CreateStatus = () => { const theme = useTheme(); const { monitorId, url } = useParams(); const navigate = useNavigate(); const isCreate = typeof url === "undefined"; const [createStatusPage, isLoading, networkError] = useCreateStatusPage(isCreate); const [statusPage, statusPageMonitors, statusPageIsLoading, statusPageNetworkError] = useStatusPageFetch(isCreate, url); const BREADCRUMBS = [ { name: "distributed uptime", path: "/distributed-uptime" }, { name: "details", path: `/distributed-uptime/${monitorId}` }, { name: isCreate ? "create status page" : "edit status page", path: `` }, ]; // Local state const [form, setForm] = useState({ type: "distributed", isPublished: false, url: url ?? Math.floor(Math.random() * 1000000).toFixed(0), logo: undefined, companyName: "", monitors: [monitorId], }); const [errors, setErrors] = useState({}); const handleFormChange = (e) => { const { name, value, checked, type } = e.target; // Check for errors const { error } = statusPageValidation.validate( { [name]: value }, { abortEarly: false } ); setErrors((prev) => buildErrors(prev, name, error)); if (type === "checkbox") { setForm({ ...form, [name]: checked }); return; } setForm({ ...form, [name]: value }); }; const handleImageUpload = (e) => { const img = e.target?.files?.[0]; setForm((prev) => ({ ...prev, logo: img, })); }; const handleSubmit = async () => { let logoToSubmit = undefined; // Handle image if (typeof form.logo !== "undefined") { logoToSubmit = { src: URL.createObjectURL(form.logo), name: form.logo.name, type: form.logo.type, size: form.logo.size, }; } const formToSubmit = { ...form }; if (typeof logoToSubmit !== "undefined") { formToSubmit.logo = logoToSubmit; } // Validate const { error } = statusPageValidation.validate(formToSubmit, { abortEarly: false }); if (typeof error === "undefined") { const success = await createStatusPage({ form: formToSubmit }); if (success) { const verb = isCreate ? "created" : "updated"; createToast({ body: `Status page ${verb} successfully` }); navigate(`/status/distributed/${form.url}`); } return; } const newErrors = {}; error?.details?.forEach((err) => { newErrors[err.path[0]] = err.message; }); setErrors((prev) => ({ ...prev, ...newErrors })); }; // If we are configuring, populate fields useEffect(() => { if (isCreate) return; if (typeof statusPage === "undefined") { return; } let newLogo = undefined; if (statusPage.logo) { newLogo = { src: `data:${statusPage.logo.contentType};base64,${statusPage.logo.data}`, name: "logo", type: statusPage.logo.contentType, size: null, }; } setForm((prev) => { return { ...prev, companyName: statusPage?.companyName, isPublished: statusPage?.isPublished, timezone: statusPage?.timezone, monitors: statusPageMonitors.map((monitor) => monitor._id), color: statusPage?.color, logo: newLogo, }; }); }, [isCreate, statusPage, statusPageMonitors]); return ( Create your{" "} status page Access If your status page is ready, you can mark it as published. Basic Information Define company name and the subdomain that your status page points to. Logo Upload a logo for your status page Logo ); }; export default CreateStatus;