import { useQuery } from "@tanstack/react-query"; import { UptimeData, Service } from "@/types/service.types"; import { uptimeService } from "@/services/uptimeService"; import { format, parseISO } from "date-fns"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Badge } from "@/components/ui/badge"; import { useTheme } from "@/contexts/ThemeContext"; import { Check, X, AlertTriangle, Pause } from "lucide-react"; interface ServiceUptimeHistoryProps { serviceId: string; serviceType: string; // Add service type to determine collection startDate?: Date; endDate?: Date; } export function ServiceUptimeHistory({ serviceId, serviceType, startDate = new Date(Date.now() - 24 * 60 * 60 * 1000), endDate = new Date() }: ServiceUptimeHistoryProps) { const { theme } = useTheme(); const { data: uptimeHistory, isLoading, error } = useQuery({ queryKey: ['uptimeHistory', serviceId, serviceType, startDate?.toISOString(), endDate?.toISOString()], queryFn: () => { // console.log(`ServiceUptimeHistory: Fetching for service ${serviceId} of type ${serviceType}`); return uptimeService.getUptimeHistory(serviceId, 200, startDate, endDate, serviceType); }, enabled: !!serviceId && !!serviceType, refetchInterval: 5000, // Refresh UI every 5 seconds }); if (isLoading) { return (
Loading uptime history...
Error loading uptime history.
No uptime history available for the selected time period.