import { useState, useEffect } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { Card } from '../components/ui/Card' import { Button } from '../components/ui/Button' import { Input } from '../components/ui/Input' import { toast } from '../utils/toast' import { getSettings, updateSetting } from '../api/settings' import client from '../api/client' import { Loader2, Server, RefreshCw, Save, Activity } from 'lucide-react' interface HealthResponse { status: string service: string version: string git_commit: string build_time: string } interface UpdateInfo { current_version: string latest_version: string update_available: boolean release_url?: string } export default function SystemSettings() { const queryClient = useQueryClient() const [caddyAdminAPI, setCaddyAdminAPI] = useState('http://localhost:2019') // Fetch Settings const { data: settings } = useQuery({ queryKey: ['settings'], queryFn: getSettings, }) // Update local state when settings load useEffect(() => { if (settings) { if (settings['caddy.admin_api']) setCaddyAdminAPI(settings['caddy.admin_api']) } }, [settings]) // Fetch Health/System Status const { data: health, isLoading: isLoadingHealth } = useQuery({ queryKey: ['health'], queryFn: async (): Promise => { const response = await client.get('/health') return response.data }, }) // Check for Updates const { data: updateInfo, refetch: checkUpdates, isFetching: isCheckingUpdates, } = useQuery({ queryKey: ['updates'], queryFn: async (): Promise => { const response = await client.get('/system/updates') return response.data }, enabled: false, // Manual trigger }) const saveSettingsMutation = useMutation({ mutationFn: async () => { await updateSetting('caddy.admin_api', caddyAdminAPI, 'caddy', 'string') }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['settings'] }) toast.success('System settings saved') }, onError: (error: any) => { toast.error(`Failed to save settings: ${error.message}`) }, }) return (

System Settings

{/* General Configuration */}

General Configuration

setCaddyAdminAPI(e.target.value)} placeholder="http://localhost:2019" />

URL to the Caddy admin API (usually on port 2019)

{/* System Status */}

System Status

{isLoadingHealth ? (
) : health ? (

Service

{health.service}

Status

{health.status}

Version

{health.version}

Build Time

{health.build_time || 'N/A'}

Git Commit

{health.git_commit || 'N/A'}

) : (

Unable to fetch system status

)}
{/* Update Check */}

Software Updates

{updateInfo && (

Current Version

{updateInfo.current_version}

Latest Version

{updateInfo.latest_version}

{updateInfo.update_available && (

A new version is available!

{updateInfo.release_url && ( View Release Notes )}
)} {!updateInfo.update_available && (

✓ You are running the latest version

)}
)}
) }