import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, Stack, TextField, Typography } from '@mui/material' import { useState, type FormEvent } from 'react' import { HTTPS_CERTIFICATES, type HttpsCertificateSource, type HttpsChange, type HttpsSettings } from './api.ts' /** The labels of the sources of the server's certificate. */ export const CERTIFICATE_LABELS: Record = { acme: 'ACME', 'self-signed': 'Self-signed', ca: 'Servers Issuing CA of the server\'s PKI', external: 'Another CA: a CSR signed there, or a PFX', } /** * A dialog that edits where the certificate of the server's FQDN comes from, https.certificate: the ACME CA of * AcmeSettingsDialog, a self-signed certificate valid for https.self_signed_days, the Servers Issuing CA, or another * CA's certificate, imported on the Server tab of the Certificates page; self-signed is selected while the first setup * has not chosen one. onSave sends the source and the validity * only; the dialog closes when it succeeds and shows the server's message when it fails. */ export default function HttpsSettingsDialog(props: { current: HttpsSettings onSave: (settings: HttpsChange) => Promise onCancel: () => void }) { const [certificate, setCertificate] = useState(props.current.certificate || 'self-signed') const [days, setDays] = useState(String(props.current.self_signed_days)) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const daysProblem = /^\d{1,4}$/.test(days) && Number(days) >= 1 && Number(days) <= 3650 ? '' : '1 to 3650 days.' const blocked = certificate === 'self-signed' && daysProblem !== '' async function submit(event: FormEvent) { event.preventDefault() if (blocked) { return } setSaving(true) setError(null) try { await props.onSave(certificate === 'self-signed' ? { certificate, self_signed_days: Number(days) } : { certificate }) } catch (caught) { setError((caught as Error).message) setSaving(false) } } return ( reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> HTTPS certificate {error && {error}} setCertificate(event.target.value as HttpsCertificateSource)}> {HTTPS_CERTIFICATES.map((item) => {CERTIFICATE_LABELS[item]})} {certificate === 'acme' && ( Caddy takes the certificate from the ACME CA, which its own settings choose. )} {certificate === 'self-signed' && ( setDays(event.target.value.trim())} /> )} {certificate === 'external' && ( The certificate is imported on this tab, from a CSR the server makes or as a PFX file. The server serves a self-signed certificate until then. )} The server runs its setup again, which applies the change. ) }