~bigbes/lethe

ref: 1be25bac49ceac3bb2923ce40facdf9ba7a9811f lethe/web/src/features/stats/useStats.ts -rw-r--r-- 628 bytes
1be25bac — Eugene Blikh docs: record shipped web tasks and dev tooling updates 24 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { useQuery } from '@tanstack/react-query'
import type { UseQueryResult } from '@tanstack/react-query'
import { apiFetch } from '../../api/client'
import { adaptStats } from '../../api/adapters'
import type { Stats, StatsDTO } from '../../api/adapters'

export function useStats(range: '7d' | '30d' | '90d' | 'all'): UseQueryResult<Stats> {
  return useQuery({
    queryKey: ['stats', range],
    queryFn: async () => {
      const params = new URLSearchParams()
      params.set('range', range)
      const data = await apiFetch<StatsDTO>(`/api/v1/stats?${params.toString()}`)
      return adaptStats(data)
    },
  })
}