import Stack from "@mui/material/Stack"; import { TextInput, Button, RadioWithDescription, AutoComplete, } from "@/components/inputs"; import RadioGroup from "@mui/material/RadioGroup"; import { ConfigBox, BasePage } from "@/components/design-elements"; import { Trash2 } from "lucide-react"; import { Typography } from "@mui/material"; import { useEffect, useState, useRef } from "react"; import { useTranslation } from "react-i18next"; import { monitorSchemaInfra } from "@/validation/zod"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm, Controller, useWatch, type SubmitHandler, } from "react-hook-form"; import { useTheme } from "@mui/material/styles"; import { useInitForm } from "@/hooks/forms/UseInitInfraForm"; type FormValues = z.infer; export const InfraForm = ({ mode = "create", initialData, onSubmit, notificationOptions, loading, }: { mode?: "create" | "configure"; initialData?: Partial; onSubmit: SubmitHandler; notificationOptions: any[]; loading: boolean; }) => { const { t } = useTranslation(); const theme = useTheme(); const { defaults } = useInitForm({ initialData: initialData }); const urlInputRef = useRef(null); const [selectedProtocol, setSelectedProtocol] = useState("https"); const { handleSubmit, control, setValue, reset, formState: { errors }, } = useForm({ resolver: zodResolver(monitorSchemaInfra) as any, defaultValues: defaults, mode: "onChange", }); const url = useWatch({ control, name: "url" }); useEffect(() => { reset(defaults); if (defaults.url.startsWith("http://")) { setSelectedProtocol("http"); } }, [initialData, reset, defaults]); useEffect(() => { if (!url) { setValue("url", `${selectedProtocol}://`); } const hasProtocol = /^(http|https):\/\//i.test(url); if (hasProtocol) { const newUrl = url.replace( /^(http|https):\/\//i, `${selectedProtocol}://` ); if (newUrl !== url) setValue("url", newUrl); } else { setValue("url", `${selectedProtocol}://${url}`); } }, [selectedProtocol, setValue, url]); useEffect(() => { const input = urlInputRef.current; if (input) { const len = input.value.length; input.setSelectionRange(len, len); } }, [selectedProtocol]); const notificationChannels = useWatch({ control, name: "notificationChannels", }); return ( { setSelectedProtocol(e.target.value); }} > ( { const input = e.target; setTimeout(() => { input.setSelectionRange( input.value.length, input.value.length ); }, 0); }} type="text" fieldLabel={t("url")} fullWidth error={!!errors.url} helperText={errors.url ? errors.url.message : ""} /> )} /> ( )} /> ( )} /> } /> ( { const target = e.target as HTMLInputElement; field.onChange(target.valueAsNumber); }} /> )} /> } /> ( )} /> ( )} /> ( )} /> ( )} /> } /> ( option.name} value={notificationOptions.filter((o: any) => (field.value || []).includes(o._id) )} onChange={(_, newValue) => { field.onChange(newValue.map((o: any) => o._id)); }} /> )} /> {notificationChannels.map((notificationId) => { const option = notificationOptions.find( (o: any) => o._id === notificationId ); if (!option) return null; return ( {option.name} { const updated = notificationChannels.filter( (id) => id !== notificationId ); setValue("notificationChannels", updated); }} style={{ cursor: "pointer" }} /> ); })} } /> ( )} /> } /> ); };