38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
'use client'
|
|
|
|
import { useQuery } from '@tanstack/react-query'
|
|
import { getCurrentWeather, getForecast, getHistorical } from '@/lib/api'
|
|
import type { CurrentWeatherResponse, ForecastResponse, HistoricalResponse } from '@/lib/types'
|
|
|
|
const STALE_TIME = 5 * 60 * 1000 // 5 minutes
|
|
const REFETCH_INTERVAL = 5 * 60 * 1000 // 5 minutes
|
|
|
|
export function useCurrentWeather(lat?: number, lon?: number) {
|
|
return useQuery<CurrentWeatherResponse>({
|
|
queryKey: ['weather', 'current', lat, lon],
|
|
queryFn: () => getCurrentWeather(lat, lon),
|
|
staleTime: STALE_TIME,
|
|
refetchInterval: REFETCH_INTERVAL,
|
|
refetchOnWindowFocus: true,
|
|
})
|
|
}
|
|
|
|
export function useForecast(lat?: number, lon?: number) {
|
|
return useQuery<ForecastResponse>({
|
|
queryKey: ['weather', 'forecast', lat, lon],
|
|
queryFn: () => getForecast(lat, lon),
|
|
staleTime: STALE_TIME,
|
|
refetchInterval: REFETCH_INTERVAL,
|
|
refetchOnWindowFocus: true,
|
|
})
|
|
}
|
|
|
|
export function useHistorical(date: string, lat?: number, lon?: number) {
|
|
return useQuery<HistoricalResponse>({
|
|
queryKey: ['weather', 'historical', date, lat, lon],
|
|
queryFn: () => getHistorical(date, lat, lon),
|
|
staleTime: STALE_TIME,
|
|
enabled: !!date,
|
|
})
|
|
}
|