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),
57 const pointsWithLayers = await Promise.all(
58 ownedPoints.map(async ship => {
61 const pointInfo = await api.getPoint(Number(ship));
62 console.log(3, pointInfo)
65 layer: isL2(pointInfo?.dominion) ? 1 : 2,
69 return { point: ship, layer: 1 };
74 console.log(4, pointsWithLayers)
77 ownedPoints === null &&
78 incomingPoints === null &&
79 managingPoints === null &&
80 votingPoints === null &&
81 spawningPoints === null
84 Just(Result.Error('Failed to read the blockchain.'))
90 ownedPoints: ownedPoints
91 .concat(ownedPointsL1)
94 incomingPoints: incomingPoints
95 .concat(incomingPointsL1)
98 managingPoints: managingPoints
99 .concat(managingPointsL1)
102 votingPoints: votingPoints
103 .concat(votingPointsL1)
106 spawningPoints: spawningPoints
107 .concat(spawningPointsL1)
116 console.error('failed to fetch controlled points', error);
117 _setControlledPoints(Just(Result.Error(error)));
119 }, [contracts, wallet, getPoints, api]);
121 // sync controlled points whenever wallet or contracts changes
123 syncControlledPoints();
124 }, [syncControlledPoints]);
128 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