on master help sign in
bridge/src/store/lib/useResidentsStore.ts
114 lines 3.1 kB history raw
1import { useState, useCallback } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
4import { azimuth } from 'azimuth-js';
6import { useNetwork } from 'store/network';
8import * as need from 'lib/need';
9import { isPlanet } from 'lib/utils/point';
11const emptyCacheEntry = {
12 residentCount: Nothing(),
13 requestCount: Nothing(),
14 residents: Nothing(),
15 requests: Nothing(),
16};
18// any because wrapped in Just / Nothing
19interface ResidentDetail {
20 residentCount: any;
21 requestCount: any;
22 requests: any;
23 residents: any;
26export default function useResidents() {
27 const [residentCache, _setResidentCache] = useState<
28 Record<number, ResidentDetail>
29 >({});
31 const addToResidentCache = useCallback(
32 (point, entry) =>
33 _setResidentCache(cache => ({ ...cache, [point]: entry })),
34 [_setResidentCache]
35 );
37 const getResidents = useCallback(
38 point => residentCache[point] || emptyCacheEntry,
39 [residentCache]
40 );
42 const { contracts }: any = useNetwork();
43 const _contracts = need.contracts(contracts);
45 const syncResidentCount = useCallback(
46 async point => {
47 // Do nothing if we've already fetched in full
48 // or we aren't a sponsor
49 const pointSize = azimuth.getPointSize(point);
50 if (Just.hasInstance(getResidents(point).residents)) {
51 return;
52 }
53 if (pointSize === azimuth.PointSize.Planet) {
54 addToResidentCache(point, {
55 residentCount: Just(0),
56 requestCount: Just(0),
57 requests: Just([]),
58 residents: Just([]),
59 });
60 return;
61 }
63 const isGalaxy = pointSize === azimuth.PointSize.Galaxy;
64 const [residentCount, requestCount] = await Promise.all([
65 azimuth.getSponsoringCount(_contracts, point),
66 azimuth.getEscapeRequestsCount(_contracts, point),
67 ]);
69 // Galaxies sponsor themselves
70 const parsedCount = Number(residentCount);
71 const _residentCount = isGalaxy ? parsedCount - 1 : parsedCount;
73 addToResidentCache(point, {
74 residentCount: Just(_residentCount),
75 requestCount: Just(Number(requestCount)),
76 residents: Nothing(),
77 requests: Nothing(),
78 });
79 },
80 [addToResidentCache, _contracts, getResidents]
81 );
83 const syncResidents = useCallback(
84 async point => {
85 if (isPlanet(point)) {
86 addToResidentCache(point, {
87 residentCount: Just(0),
88 requestCount: Just(0),
89 requests: Just([]),
90 residents: Just([]),
91 });
93 return;
94 }
95 const [residents, requests] = await Promise.all([
96 azimuth.getSponsoring(_contracts, point),
97 azimuth.getEscapeRequests(_contracts, point),
98 ]);
100 // Galaxies sponsor themselves
101 const _residents = residents.filter((r: number) => r !== point);
103 addToResidentCache(point, {
104 residentCount: Just(_residents.length),
105 requestCount: Just(requests.length),
106 residents: Just(_residents),
107 requests: Just(requests),
108 });
109 },
110 [addToResidentCache, _contracts]
111 );
113 return { getResidents, syncResidentCount, syncResidents };