import { useState } from 'react' import { useQuery } from '@tanstack/react-query' import { metricsApi } from '@/lib/api' import { TimeRangeSelector, getTimeRange, shouldAutoRefresh } from './TimeRangeSelector' import { HealthChart } from './HealthChart' interface HealthTabProps { tenantId: string deviceId: string active?: boolean } export function HealthTab({ tenantId, deviceId, active = true }: HealthTabProps) { const [timeRange, setTimeRange] = useState('6h') const [customStart, setCustomStart] = useState('') const [customEnd, setCustomEnd] = useState('') const { data, isLoading } = useQuery({ queryKey: ['metrics', 'health', deviceId, timeRange, customStart, customEnd], queryFn: () => { const { start, end } = getTimeRange(timeRange, customStart, customEnd) return metricsApi.health(tenantId, deviceId, start, end) }, refetchInterval: shouldAutoRefresh(timeRange), enabled: active, }) const handleCustomRangeChange = (start: string, end: string) => { setCustomStart(start) setCustomEnd(end) } return (
{isLoading ? (
Loading…
) : !data || data.length === 0 ? (
No health metrics data available for the selected time range.
) : (
)}
) }