'use client' import { useEffect, useState, useCallback } from 'react' import Link from 'next/link' interface NightModeConfig { enabled: boolean shutdown_time: string startup_time: string last_action: string | null last_action_time: string | null } interface NightModeStatus { config: NightModeConfig current_time: string next_action: string | null next_action_time: string | null time_until_next_action: string | null services_status: Record } export function NightModeWidget() { const [status, setStatus] = useState(null) const [loading, setLoading] = useState(true) const [actionLoading, setActionLoading] = useState(null) const [error, setError] = useState(null) const fetchData = useCallback(async () => { try { const response = await fetch('/api/admin/night-mode') if (response.ok) { setStatus(await response.json()) setError(null) } else { setError('Nicht erreichbar') } } catch { setError('Verbindung fehlgeschlagen') } finally { setLoading(false) } }, []) useEffect(() => { fetchData() const interval = setInterval(fetchData, 30000) return () => clearInterval(interval) }, [fetchData]) const toggleEnabled = async () => { if (!status) return setActionLoading('toggle') try { const response = await fetch('/api/admin/night-mode', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...status.config, enabled: !status.config.enabled }), }) if (response.ok) { fetchData() } } catch { // ignore } finally { setActionLoading(null) } } const executeAction = async (action: 'start' | 'stop') => { setActionLoading(action) try { const response = await fetch('/api/admin/night-mode/execute', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action }), }) if (response.ok) { fetchData() } } catch { // ignore } finally { setActionLoading(null) } } const runningCount = Object.values(status?.services_status || {}).filter( s => s.toLowerCase() === 'running' || s.toLowerCase().includes('up') ).length const totalCount = Object.keys(status?.services_status || {}).length if (loading) { return (
) } if (error) { return (

Nachtabschaltung

{error}

Details
) } return (
{/* Header */}

Nachtabschaltung

{status?.config.enabled ? `${status.config.shutdown_time} - ${status.config.startup_time}` : 'Deaktiviert'}

Einstellungen
{/* Content */}
{/* Status Row */}
{/* Toggle */} {/* Countdown */} {status?.config.enabled && status.time_until_next_action && (
{status.next_action === 'shutdown' ? '⏸' : '▶'} {status.time_until_next_action}
)}
{/* Service Count */}
{runningCount} /{totalCount} aktiv
{/* Action Buttons */}
) }