File size: 3,087 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { useEffect, useMemo, useState } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { STATES_ALBERS_TOPO, fetchTopo, toFeats } from '../../lib/dotMatrix'
import { STATE_FIPS_TO_CODE } from '../../utils/stateFips'
import './MapSkeleton.css'

/** The national maps' box (the us-atlas Albers frame, 975 × 610). */
export const NATIONAL_MAP_ASPECT = 975 / 610

/**
 * A state's map box (width over height) from the national state shapes (the same cached query the national maps use), so a
 * state map's skeleton has the state's own shape box; about square until the shapes are known.
 */
export function useStateAspect(stateCode: string | null | undefined): number {
  const { data: topo } = useQuery({
    queryKey: ['us-atlas-states-albers'],
    queryFn: () => fetchTopo(STATES_ALBERS_TOPO),
    enabled: Boolean(stateCode),
    staleTime: 1000 * 60 * 60,
  })
  return useMemo(() => {
    if (!topo || !stateCode) return 1
    const f = toFeats(topo, 'states').find((x) => STATE_FIPS_TO_CODE[x.id] === stateCode)
    if (!f) return 1
    const w = f.bbox[2] - f.bbox[0]
    const h = f.bbox[3] - f.bbox[1]
    return h > 0 ? Math.min(3, Math.max(0.5, w / h)) : 1
  }, [topo, stateCode])
}

interface Props {
  /** Width over height of the map that will replace it, so nothing around it moves when it arrives. */
  aspect: number
  /** What a screen reader hears while it loads. */
  label?: string
  /** After this long, a calm "taking longer" line with Try again. */
  slowAfterMs?: number
  /** The load failed: the calm message and Try again at once. */
  error?: boolean
  /** Try again; by default every query on the page that is showing refetches. */
  onRetry?: () => void
}

/**
 * The box a story or home map holds while its shapes and counts load (#1328): its final size, a soft shimmer, and nothing
 * else: no numbers, caption, legend or card until the data they describe has arrived. A slow (8 s) or failed load says so
 * calmly with Try again, never a broken map.
 */
export default function MapSkeleton({ aspect, label = 'Loading the map', slowAfterMs = 8000, error = false, onRetry }: Props) {
  const qc = useQueryClient()
  const [slow, setSlow] = useState(false)
  useEffect(() => {
    if (error) return
    const t = setTimeout(() => setSlow(true), slowAfterMs)
    return () => clearTimeout(t)
  }, [error, slowAfterMs])
  const retry = () => {
    setSlow(false)
    if (onRetry) onRetry()
    else void qc.refetchQueries({ type: 'active' })
  }
  const stuck = error || slow
  return (
    <div className={`map-skeleton${stuck ? ' is-stuck' : ''}`} style={{ aspectRatio: String(aspect) }} role="status" aria-live="polite">
      {stuck ? (
        <div className="map-skeleton-msg">
          <p>{error ? 'The map could not be loaded right now.' : 'The map is taking longer than usual.'}</p>
          <button type="button" className="map-skeleton-retry" onClick={retry}>
            Try again
          </button>
        </div>
      ) : (
        <span className="sr-only">{label}…</span>
      )}
    </div>
  )
}