import { fetchService } from "@/lib/nezha-api" import { NezhaServer, ServiceData } from "@/types/nezha-api" import { ExclamationTriangleIcon } from "@heroicons/react/20/solid" import { useQuery } from "@tanstack/react-query" import { useTranslation } from "react-i18next" import { CycleTransferStatsCard } from "./CycleTransferStats" import ServiceTrackerClient from "./ServiceTrackerClient" import { Loader } from "./loading/Loader" export function ServiceTracker({ serverList }: { serverList: NezhaServer[] }) { const { t } = useTranslation() const { data: serviceData, isLoading } = useQuery({ queryKey: ["service"], queryFn: () => fetchService(), refetchOnMount: true, refetchOnWindowFocus: true, refetchInterval: 10000, }) const processServiceData = (serviceData: ServiceData) => { const days = serviceData.up.map((up, index) => { const totalChecks = up + serviceData.down[index] const dailyUptime = totalChecks > 0 ? (up / totalChecks) * 100 : 0 return { completed: up > serviceData.down[index], date: new Date(Date.now() - (29 - index) * 24 * 60 * 60 * 1000), uptime: dailyUptime, delay: serviceData.delay[index] || 0, } }) const totalUp = serviceData.up.reduce((a, b) => a + b, 0) const totalChecks = serviceData.up.reduce((a, b) => a + b, 0) + serviceData.down.reduce((a, b) => a + b, 0) const uptime = (totalUp / totalChecks) * 100 const avgDelay = serviceData.delay.length > 0 ? serviceData.delay.reduce((a, b) => a + b, 0) / serviceData.delay.length : 0 return { days, uptime, avgDelay } } if (isLoading) { return (