import { IconSettings } from "@tabler/icons-react"; import cn from "classnames"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; import { Field, Form } from "formik"; import { type ReactNode, useState } from "react"; import { Alert } from "react-bootstrap"; import Modal from "react-bootstrap/Modal"; import { AccessField, Button, DomainNamesField, HasPermission, Loading, LocationsFields, NginxConfigField, SSLCertificateField, SSLOptionsFields, } from "src/components"; import { FormModal } from "src/components/Form"; import { useProxyHost, useSetProxyHost, useUser } from "src/hooks"; import { T } from "src/locale"; import { MANAGE, PROXY_HOSTS } from "src/modules/Permissions"; import { validateNumber, validateString } from "src/modules/Validations"; import { showObjectSuccess } from "src/notifications"; const showProxyHostModal = (id: number | "new") => { EasyModal.show(ProxyHostModal, { id }); }; interface Props extends InnerModalProps { id: number | "new"; } const ProxyHostModal = EasyModal.create(({ id, visible, remove }: Props) => { const { data: currentUser, isLoading: userIsLoading, error: userError } = useUser("me"); const { data, isLoading, error } = useProxyHost(id); const { mutate: setProxyHost } = useSetProxyHost(); const [errorMsg, setErrorMsg] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); const isAdmin = currentUser?.roles?.includes("admin"); const onSubmit = async (values: any, { setSubmitting }: any) => { if (isSubmitting) return; setIsSubmitting(true); setErrorMsg(null); const { ...payload } = { id: id === "new" ? undefined : id, ...values, }; payload.locations = (values.locations || []).filter((location: any) => location.path?.trim() !== ""); if (!isAdmin) { delete payload.advanced_config; delete payload.advancedConfig; // Location advanced configs are sent as-is. Non-admins can't edit them (the editor is // hidden), so each one is still the admin-written value and moves with its location // when the path is renamed; the backend matches them by value. Looking them up by // path here would silently drop the config from a renamed location. } setProxyHost(payload, { onError: (err: any) => setErrorMsg(), onSuccess: () => { showObjectSuccess("proxy-host", "saved"); remove(); }, onSettled: () => { setIsSubmitting(false); setSubmitting(false); }, }); }; return ( <> {(isLoading || userIsLoading || ((error || userError) && (!data || !currentUser))) && ( {!isLoading && (error || userError) && ( {error?.message || userError?.message || "Unknown error"} )} {(isLoading || userIsLoading) && } )} {!isLoading && !userIsLoading && data && currentUser && ( {({ onHide }) => (
setErrorMsg(null)} dismissible> {errorMsg}
{({ field, form }: any) => (
{form.errors.forwardScheme ? (
{form.errors.forwardScheme && form.touched.forwardScheme ? form.errors.forwardScheme : null}
) : null}
)}
{({ field, form }: any) => (
{form.errors.forwardHost ? (
{form.errors.forwardHost && form.touched.forwardHost ? form.errors.forwardHost : null}
) : null}
)}
{({ field, form }: any) => (
{form.errors.forwardPort ? (
{form.errors.forwardPort && form.touched.forwardPort ? form.errors.forwardPort : null}
) : null}
)}

{isAdmin && (
)}
)}
)} ); }); export { showProxyHostModal };