"use client" import { useEffect, useState } from "react" import { Boxes, Info, Loader2, Settings2, CheckCircle2 } from "lucide-react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "./ui/card" import { Badge } from "./ui/badge" import { fetchApi } from "../lib/api-config" interface DetectionResponse { success: boolean enabled?: boolean message?: string purged?: number } export function LxcUpdateDetection() { const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [enabled, setEnabled] = useState(true) const [pending, setPending] = useState(true) const [editMode, setEditMode] = useState(false) const [error, setError] = useState(null) const [saved, setSaved] = useState(false) const [lastPurged, setLastPurged] = useState(null) useEffect(() => { let cancelled = false fetchApi("/api/lxc-updates/detection") .then(data => { if (cancelled) return if (data.success && typeof data.enabled === "boolean") { setEnabled(data.enabled) setPending(data.enabled) } else { setError(data.message || "Failed to load setting") } }) .catch(e => { if (!cancelled) setError(String(e)) }) .finally(() => { if (!cancelled) setLoading(false) }) return () => { cancelled = true } }, []) const hasChanges = pending !== enabled function handleEdit() { setEditMode(true) setError(null) setSaved(false) setLastPurged(null) } function handleCancel() { setPending(enabled) setEditMode(false) setError(null) setLastPurged(null) } async function handleSave() { if (!hasChanges) { setEditMode(false) return } setSaving(true) setError(null) setSaved(false) setLastPurged(null) try { const data = await fetchApi("/api/lxc-updates/detection", { method: "POST", body: JSON.stringify({ enabled: pending }), }) if (!data.success) { setError(data.message || "Failed to save setting") return } setEnabled(pending) setEditMode(false) setSaved(true) setTimeout(() => setSaved(false), 3000) if (!pending && typeof data.purged === "number" && data.purged > 0) { setLastPurged(data.purged) } // Notify the Notifications section so it hides/shows the // lxc_updates_available toggle in real time. if (typeof window !== "undefined") { window.dispatchEvent( new CustomEvent("proxmenux:lxc-detection-changed", { detail: { enabled: pending } }), ) } } catch (e) { setError(String(e)) } finally { setSaving(false) } } return (
{/* Title row — flex-wrap so on narrow screens the badge can drop under the title without dragging the icon along with it. The icon stays on the same baseline as the title text on every breakpoint thanks to `items-center` + leading-tight title. */}
LXC Update Detection {enabled ? ( Active ) : ( Disabled )}
{saved && ( Saved )} {error && !editMode && ( Save failed: {error} )} {editMode ? ( <> ) : ( )}
Periodically check running Debian/Ubuntu/Alpine LXC containers for pending package updates (apt list --upgradable / apk list -u) and surface them on the dashboard. The corresponding notification toggle in Notifications → Services appears only while detection is enabled.
{/* ── Enable/Disable ── single-line label + toggle. The description paragraph was removed because the CardDescription above already covers the behaviour; on mobile that second paragraph forced the icon to top-align and made the toggle wrap awkwardly. */}
Enable LXC update detection
{lastPurged !== null && lastPurged > 0 && (

{lastPurged} LXC entries removed from the registry. Re-enabling detection will repopulate them on the next scan cycle.

)} {error && editMode && (

{error}

)}
) }