File size: 1,651 Bytes
0e11366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useCallback, useEffect, useState } from "react";
import type { UpdateItem } from "../lib/types";
import { UPDATES_LOCAL_URL, UPDATES_GLOBAL_URL } from "../lib/constants";
import { toQuery } from "../lib/utils";

export function useUpdates(selectedLL: [number, number] | null) {
  const [activeTab, setActiveTab] = useState<"local" | "global">("local");
  const [localUpdates, setLocal] = useState<UpdateItem[]>([]);
  const [globalUpdates, setGlobal] = useState<UpdateItem[]>([]);
  const [loadingLocal, setLLoad] = useState(false);
  const [loadingGlobal, setGLoad] = useState(false);

  const loadLocal = useCallback(async (ll: [number, number]) => {
    setLLoad(true);
    try {
      const url =
        UPDATES_LOCAL_URL +
        toQuery({
          lat: ll[0],
          lon: ll[1],
          radius_miles: 25,
          max_age_hours: 48,
          limit: 100,
        });
      const j = await fetch(url).then((r) => r.json());
      setLocal(j.updates || []);
    } catch {
      setLocal([]);
    } finally {
      setLLoad(false);
    }
  }, []);

  const loadGlobal = useCallback(async () => {
    setGLoad(true);
    try {
      const j = await fetch(UPDATES_GLOBAL_URL + "?limit=200").then((r) =>
        r.json()
      );
      setGlobal(j.updates || []);
    } catch {
      setGlobal([]);
    } finally {
      setGLoad(false);
    }
  }, []);

  useEffect(() => {
    loadGlobal();
  }, [loadGlobal]);
  useEffect(() => {
    if (selectedLL) loadLocal(selectedLL);
  }, [selectedLL, loadLocal]);

  return {
    activeTab,
    setActiveTab,
    localUpdates,
    globalUpdates,
    loadingLocal,
    loadingGlobal,
  };
}