import { Alert, Box, Button, Chip, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, Stack, Table, TableBody, TableCell, TableRow, TextField, Typography } from '@mui/material' import { useCallback, useEffect, useState, type FormEvent } from 'react' import ConfirmDialog from './ConfirmDialog.tsx' import { COMPONENTS_SHAPE, request, type Component } from './api.ts' /** Seconds between two reads while a component is being installed. */ const POLL_SECONDS = 3 /** The name and what each component is. */ const LABELS: Record = { vpn: ['VPN', 'IKEv2, OpenVPN and WireGuard access for users, on the Remote Access page.'], router: ['Router', 'Routing between the external and the internal interface, and site-to-site IPsec.'], dns: ['Name server', 'PowerDNS, the primary name server of the server\'s domains, on the DNS page.'], webdav: ['WebDAV', 'The folders of the web sites over WebDAV, with rclone behind Caddy.'], } /** The chip of a component's installation state. */ const STATE_COLORS: Record = { 'not installed': 'default', installing: 'info', installed: 'success', failed: 'error', } /** An IPv4 address with its prefix length, as local.int_addr takes it. */ const ADDRESS_PREFIX = /^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$/ /** An IPv4 address. */ const ADDRESS = /^(\d{1,3}\.){3}\d{1,3}$/ /** * The installable components of the server from /components, the VPN, the router, the name server and WebDAV, each * with its switch and whether its packages are installed. Enable switches a component on, and the server installs its * packages, which the panel follows every POLL_SECONDS seconds; Disable, after a confirmation, switches it off and * keeps its packages. The router is offered only while the server has an interface for its internal side, and its * Enable asks for that interface and, in manual mode, its address and gateway. onChange tells of every change made. */ export default function ComponentsPanel(props: { onChange?: () => void }) { const [components, setComponents] = useState(null) const [error, setError] = useState(null) const [busy, setBusy] = useState(false) const [disabling, setDisabling] = useState(null) const [router, setRouter] = useState(null) const read = useCallback(async () => { await request<{ components: Component[] }>('GET', '/components', COMPONENTS_SHAPE).then( (found) => setComponents(found.components), (caught) => setError(`The server did not give its components: ${(caught as Error).message}`), ) }, []) useEffect(() => { void read() }, [read]) // A component switched on is followed while its packages are being installed or are still missing. const pending = components?.some((item) => item.state === 'installing' || (item.enabled && item.state === 'not installed')) useEffect(() => { if (!pending) { return } const timer = window.setInterval(() => void read(), POLL_SECONDS * 1000) return () => window.clearInterval(timer) }, [pending, read]) /** Sends the switch of a component, with the router's interface settings, and reads the answer. */ async function change(item: Component, body: Record) { setBusy(true) setError(null) try { setComponents((await request<{ components: Component[] }>('PUT', `/components/${item.name}`, COMPONENTS_SHAPE, body)).components) props.onChange?.() } finally { setBusy(false) } } async function enable(item: Component) { if (item.name === 'router') { setRouter(item) return } try { await change(item, { enabled: true }) } catch (caught) { setError((caught as Error).message) } } return ( {error && setError(null)}>{error}} {components === null ? ( The components are not read yet. ) : ( {components.map((item) => { const [label, description] = LABELS[item.name] const unavailable = item.name === 'router' && item.available === false return ( {label} {description} {unavailable && ( It needs a second network interface; the server has only one. )} {item.enabled && item.state === 'failed' && ( The installation failed; Enable tries again, and the Logs tab has its output. )} {item.enabled && item.state !== 'failed' ? ( ) : ( )} ) })}
)} {disabling && ( setDisabling(null)} onConfirm={() => { const item = disabling setDisabling(null) change(item, { enabled: false }).catch((caught: Error) => setError(caught.message)) }} /> )} {router && ( setRouter(null)} onSave={async (values) => { await change(router, { enabled: true, ...values }) setRouter(null) }} /> )}
) } /** * A dialog that switches the router on: the internal interface, chosen from the server's interfaces but the external * one, and in manual mode its address with the prefix length and the gateway of the internal network; in auto mode the * server detects them. onSave sends them; the dialog shows the server's message when it fails. */ function RouterDialog(props: { component: Component onSave: (values: { internal_interface: string; int_addr: string; int_gateway_ip: string }) => Promise onCancel: () => void }) { const choices = props.component.interfaces ?? [] const mode = props.component.mode ?? 'manual' const [internal, setInternal] = useState(props.component.internal_interface && choices.includes(props.component.internal_interface) ? props.component.internal_interface : choices[0] ?? '') const [address, setAddress] = useState(props.component.int_addr ?? '') const [gateway, setGateway] = useState(props.component.int_gateway_ip ?? '') const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const manual = mode === 'manual' const addressProblem = manual && !ADDRESS_PREFIX.test(address) ? 'An IPv4 address with its prefix length, such as 10.1.1.4/24.' : '' const gatewayProblem = manual && !ADDRESS.test(gateway) ? 'An IPv4 address.' : '' const blocked = (mode !== 'auto' && internal === '') || addressProblem !== '' || gatewayProblem !== '' async function submit(event: FormEvent) { event.preventDefault() if (blocked) { return } setSaving(true) setError(null) try { await props.onSave({ internal_interface: mode === 'auto' ? '' : internal, int_addr: manual ? address : '', int_gateway_ip: manual ? gateway : '' }) } catch (caught) { setError((caught as Error).message) setSaving(false) } } return ( reason !== 'backdropClick' && props.onCancel()} maxWidth="xs" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> Enable the router {error && {error}} {mode === 'auto' ? ( In auto mode the server detects the internal interface, its address and its gateway. ) : ( setInternal(event.target.value)}> {choices.map((name) => {name})} )} {manual && ( <> setAddress(event.target.value.trim())} /> setGateway(event.target.value.trim())} /> )} ) }