import { Alert, Button, Checkbox, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, MenuItem, Stack, Switch, TextField, Typography, } from '@mui/material' import { useState, type FormEvent } from 'react' import { TARGET_LABELS } from './backup.ts' import { BACKUP_KEEPS, BACKUP_SCHEDULES, BACKUP_TARGETS, RCLONE_MODES, type Backup, type BackupKeep, type BackupSettings } from './api.ts' /** The labels of the retention settings. */ const KEEP_LABELS: Record = { keep_hourly: 'Hourly', keep_daily: 'Daily', keep_weekly: 'Weekly', keep_monthly: 'Monthly', keep_yearly: 'Yearly', } /** The labels of the rclone modes. */ const MODE_LABELS: Record = { sync: 'Sync: the cloud mirrors the archives, retention applies to both', copy: 'Copy: nothing is deleted in the cloud', } /** A PBS certificate fingerprint: 32 hexadecimal pairs joined by colons. */ const FINGERPRINT = /^([0-9A-Fa-f]{2}:){31}[0-9A-Fa-f]{2}$/ /** * A dialog that edits the server's backup settings: whether backups run, the schedule and the target; for a local * folder, its path, the retention settings that apply to the schedule and the cloud copy with a remote of the server's * rclone.conf; for a Proxmox Backup Server, the repository, the token secret, the fingerprint, the namespace and the * key file; and whether the metrics database is included. An empty token secret keeps the current one. onSave sends * the settings and the secret; the dialog closes when it succeeds and shows the server's message when it fails. */ export default function BackupDialog(props: { backup: Backup onSave: (settings: BackupSettings, password: string) => Promise onCancel: () => void }) { const current = props.backup.settings const [settings, setSettings] = useState(() => { const { pbs_password_set: _set, ...rest } = current return rest }) const [remote, setRemote] = useState(current.rclone_remote.split(':')[0] ?? '') const [remotePath, setRemotePath] = useState(current.rclone_remote.includes(':') ? current.rclone_remote.slice(current.rclone_remote.indexOf(':') + 1) : '') const [password, setPassword] = useState('') const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const change = (values: Partial) => setSettings({ ...settings, ...values }) const local = settings.target === 'local' const keeps = BACKUP_KEEPS.slice(BACKUP_SCHEDULES.indexOf(settings.schedule as (typeof BACKUP_SCHEDULES)[number])) const remotes = [...new Set([...props.backup.rclone.remotes, ...(remote ? [remote] : [])])] const pathProblem = local && !settings.path.startsWith('/') ? 'An absolute path.' : '' const keepProblem = local && !keeps.some((keep) => settings[keep] > 0) ? 'At least one of them above 0.' : '' const repositoryProblem = !local && settings.pbs_repository.trim() === '' ? 'Required.' : '' const passwordProblem = !local && !current.pbs_password_set && password === '' ? 'Required.' : '' const fingerprintProblem = settings.pbs_fingerprint !== '' && !FINGERPRINT.test(settings.pbs_fingerprint) ? '32 hexadecimal pairs joined by colons.' : '' const keyfileProblem = settings.pbs_keyfile !== '' && !settings.pbs_keyfile.startsWith('/') ? 'An absolute path.' : '' const blocked = [pathProblem, keepProblem, repositoryProblem, passwordProblem, fingerprintProblem, keyfileProblem].some((problem) => problem !== '') async function submit(event: FormEvent) { event.preventDefault() if (blocked) { return } setSaving(true) setError(null) try { await props.onSave({ ...settings, rclone_remote: remote ? `${remote}:${remotePath.trim()}` : '' }, password) } catch (caught) { setError((caught as Error).message) setSaving(false) } } return ( reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> Edit backup settings {error && {error}} change({ enabled: event.target.checked })} />} label="Scheduled backups" /> change({ schedule: event.target.value })}> {BACKUP_SCHEDULES.map((item) => {item})} change({ target: event.target.value })}> {BACKUP_TARGETS.map((item) => {TARGET_LABELS[item]})} change({ metrics: event.target.checked })} />} label="Include the metrics database" /> {local ? ( <> change({ path: event.target.value })} /> Archives kept {keeps.map((keep) => ( change({ [keep]: Math.max(0, Math.trunc(Number(event.target.value) || 0)) })} /> ))} {keepProblem && {keepProblem}} Cloud copy setRemote(event.target.value)} > None {remotes.map((item) => {item})} setRemotePath(event.target.value)} /> change({ rclone_mode: event.target.value })}> {RCLONE_MODES.map((item) => {MODE_LABELS[item]})} ) : ( <> change({ pbs_repository: event.target.value.trim() })} /> setPassword(event.target.value)} /> change({ pbs_fingerprint: event.target.value.trim() })} /> change({ pbs_namespace: event.target.value.trim() })} /> change({ pbs_keyfile: event.target.value.trim() })} /> )} ) }