1import { useState, useEffect, useCallback } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import Result from 'folktale/result';
4import * as azimuth from 'azimuth-js';
6import { useNetwork } from '../network';
7import { useWallet } from 'store/wallet';
8import useRoller from 'lib/useRoller';
9import { isL2 } from 'lib/utils/roller';
11const onlyUnique = (value, index, self) => self.indexOf(value) === index;
13export default function useControlledPointsStore() {
14 const { contracts } = useNetwork();
15 const { wallet } = useWallet();
16 const { api, getPoints } = useRoller();
18 const [controlledPoints, _setControlledPoints] = useState(Nothing());
20 const syncControlledPoints = useCallback(async () => {
21 const _contracts = contracts.getOrElse(null);
22 const _wallet = wallet.getOrElse(null);
23 if (!_contracts || !_wallet) {
27 _setControlledPoints(Nothing());
29 const address = _wallet.address;
43 ] = await Promise.all([
44 getPoints('own', address),
45 getPoints('transfer', address),
46 getPoints('manage', address),
47 getPoints('vote', address),
48 getPoints('spawn', address),
49 azimuth.azimuth.getOwnedPoints(_contracts, address),
50 azimuth.azimuth.getTransferringFor(_contracts, address),
51 azimuth.azimuth.getManagerFor(_contracts, address),
52 azimuth.azimuth.getVotingFor(_contracts, address),
53 azimuth.azimuth.getSpawningFor(_contracts, address),
56 const pointsWithLayers = await Promise.all(
57 ownedPoints.map(async ship => {
59 const pointInfo = await api.getPoint(Number(ship));
62 layer: isL2(pointInfo?.dominion) ? 2 : 1,
66 return { point: ship, layer: 1 };
72 ownedPoints === null &&
73 incomingPoints === null &&
74 managingPoints === null &&
75 votingPoints === null &&
76 spawningPoints === null
79 Just(Result.Error('Failed to read the blockchain.'))
85 ownedPoints: ownedPoints
86 .concat(ownedPointsL1)
89 incomingPoints: incomingPoints
90 .concat(incomingPointsL1)
93 managingPoints: managingPoints
94 .concat(managingPointsL1)
97 votingPoints: votingPoints
98 .concat(votingPointsL1)
101 spawningPoints: spawningPoints
102 .concat(spawningPointsL1)
111 console.error('failed to fetch controlled points', error);
112 _setControlledPoints(Just(Result.Error(error)));
114 }, [contracts, wallet, getPoints, api]);
116 // sync controlled points whenever wallet or contracts changes
118 syncControlledPoints();
119 }, [syncControlledPoints]);
123 syncControlledPoints,
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list