import { IconShield } from "@tabler/icons-react"; import { Field, useFormikContext } from "formik"; import Select, { type ActionMeta, components, type OptionProps } from "react-select"; import type { Certificate } from "src/api/backend"; import { useCertificates } from "src/hooks"; import { DateTimeFormat, intl, T } from "src/locale"; interface CertOption { readonly value: number | "new"; readonly label: string; readonly subLabel: string; readonly icon: React.ReactNode; } const Option = (props: OptionProps) => { return (
{props.data.icon} {props.data.label}
{props.data.subLabel}
); }; interface Props { id?: string; name?: string; label?: string; required?: boolean; allowNew?: boolean; forHttp?: boolean; // the sslForced, http2Support, hstsEnabled, hstsSubdomains fields } export function SSLCertificateField({ name = "certificateId", label = "ssl-certificate", id = "certificateId", required, allowNew, forHttp = true, }: Props) { const { isLoading, isError, error, data } = useCertificates(); const { values, setFieldValue } = useFormikContext(); const v: any = values || {}; const handleChange = (newValue: any, _actionMeta: ActionMeta) => { setFieldValue(name, newValue?.value); const { sslForced, http2Support, hstsEnabled, hstsSubdomains, dnsChallenge, dnsProvider, dnsProviderCredentials, propagationSeconds, } = v; if (forHttp && !newValue?.value) { sslForced && setFieldValue("sslForced", false); http2Support && setFieldValue("http2Support", false); hstsEnabled && setFieldValue("hstsEnabled", false); hstsSubdomains && setFieldValue("hstsSubdomains", false); } if (newValue?.value !== "new") { dnsChallenge && setFieldValue("dnsChallenge", undefined); dnsProvider && setFieldValue("dnsProvider", undefined); dnsProviderCredentials && setFieldValue("dnsProviderCredentials", undefined); propagationSeconds && setFieldValue("propagationSeconds", undefined); } }; const options: CertOption[] = data?.map((cert: Certificate) => ({ value: cert.id, label: cert.niceName, subLabel: `${cert.provider === "letsencrypt" ? intl.formatMessage({ id: "lets-encrypt" }) : cert.provider} — ${intl.formatMessage({ id: "expires.on" }, { date: cert.expiresOn ? DateTimeFormat(cert.expiresOn) : "N/A" })}`, icon: , })) || []; // Prepend the Add New option if (allowNew) { options?.unshift({ value: "new", label: intl.formatMessage({ id: "certificates.request.title" }), subLabel: intl.formatMessage({ id: "certificates.request.subtitle" }), icon: , }); } // Prepend the None option if (!required) { options?.unshift({ value: 0, label: intl.formatMessage({ id: "certificate.none.title" }), subLabel: forHttp ? intl.formatMessage({ id: "certificate.none.subtitle.for-http" }) : intl.formatMessage({ id: "certificate.none.subtitle" }), icon: , }); } return ( {({ field, form }: any) => (
{isLoading ?
: null} {isError ?
{`${error}`}
: null} {!isLoading && !isError ? (