File size: 1,580 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { useMemo } from 'react'
import { useQueries } from '@tanstack/react-query'
import { fetchChartData } from '../../api/stance'
import { STORY_CHARTS, type LoadState, type StoryData, type StoryStatus } from '../../utils/dataStory'
import type { ChartData, ChartKey } from '../../utils/stanceGame'

/**
 * Every dataset a story reads, for one scope. The query keys are the ones `useChartData` uses, so the charts and the
 * headlines share one response per dataset. `keys` narrows it (the hub reads only the datasets behind its counts).
 */
export function useStoryData(stateCode: string | undefined, city: string | null, keys: ChartKey[] = STORY_CHARTS) {
  const c = stateCode && city ? city : ''
  const queries = useQueries({
    queries: keys.map((key) => ({
      queryKey: ['stance', 'chart', key, stateCode ?? 'national', c],
      queryFn: () => fetchChartData(key, stateCode, c || undefined),
      staleTime: 5 * 60_000,
      retry: 1,
    })),
  })
  const sig = queries.map((q) => `${q.status}${q.dataUpdatedAt}`).join('|')
  return useMemo(() => {
    const data: StoryData = {}
    const status: StoryStatus = {}
    keys.forEach((k, i) => {
      const q = queries[i]!
      data[k] = q.data as ChartData | undefined
      status[k] = (q.isError ? 'error' : q.isSuccess ? 'ready' : 'loading') satisfies LoadState
    })
    const refetch = (k: ChartKey) => {
      const i = keys.indexOf(k)
      if (i >= 0) void queries[i]!.refetch()
    }
    return { data, status, refetch }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [sig, keys])
}