import StyledSectionHeader from '~/components/StyledSectionHeader'
import Switch from '~/components/inputs/Switch'
import api from '~/lib/api'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { useNotifications } from '~/context/NotificationContext'
import { useAppAutoUpdateStatus } from '~/hooks/useAppAutoUpdateStatus'
export default function AppAutoUpdateSection() {
const { addNotification } = useNotifications()
const queryClient = useQueryClient()
const { data: status, isLoading } = useAppAutoUpdateStatus()
const enabled = status?.enabled ?? false
const toggleMutation = useMutation({
mutationFn: (value: boolean) => api.updateSetting('appAutoUpdate.enabled', value),
onSuccess: (_data, value) => {
queryClient.invalidateQueries({ queryKey: ['app-auto-update-status'] })
addNotification({
type: 'success',
message: value ? 'App automatic updates enabled.' : 'App automatic updates disabled.',
})
},
onError: () => {
addNotification({ type: 'error', message: 'Failed to update app auto-update setting.' })
},
})
return (
<>
Update window: {status.windowStart}–{status.windowEnd} ( {status.withinWindow ? 'currently inside' : 'currently outside'}); cool-off{' '} {status.cooloffHours}h. {status.lastResult && ( <> {' '} Last run: {status.lastResult} {status.lastAttemptAt ? ` (${new Date(status.lastAttemptAt).toLocaleString()})` : ''} > )}
{status.apps.length === 0 ? (No apps are opted in yet. Enable auto-update on individual apps from the Supply Depot.
) : ({app.friendly_name || app.service_name}
{app.current_version} {app.available_update_version ? ` → ${app.available_update_version}` : ' (up to date)'}
{app.auto_disabled_reason && ({app.auto_disabled_reason}
)}