8import Maybe from 'folktale/maybe';
9import * as azimuth from 'azimuth-js';
11import { useNetwork } from './network';
13export const PointCacheContext = createContext(null);
15// the default value of a point's invites
16const kEmptyInvites = {
17 availableInvites: Maybe.Nothing(),
18 sentInvites: Maybe.Nothing(),
19 acceptedInvites: Maybe.Nothing(),
22// TODO(shrugs): this hook is too big -> refactor into individual hooks for
23// details, birthday, invites, etc
24function _usePointCache() {
25 const { contracts, web3 } = useNetwork();
26 const [pointCache, _setPointCache] = useState({});
27 const [birthdayCache, _setBirthdayCache] = useState({});
28 const [invitesCache, _setInvitesCache] = useState({});
30 const addToPointCache = useCallback(
31 entry => _setPointCache(cache => ({ ...cache, ...entry })),
35 const addToBirthdayCache = useCallback(
36 entry => _setBirthdayCache(cache => ({ ...cache, ...entry })),
40 const addToInvitesCache = useCallback(
41 entry => _setInvitesCache(cache => ({ ...cache, ...entry })),
45 // TODO: refactor pointCache access to use accessor like bithday
46 const getDetails = useCallback(
47 point => pointCache[point] || Maybe.Nothing(),
50 const getBirthday = useCallback(
51 point => birthdayCache[point] || Maybe.Nothing(),
54 const getInvites = useCallback(
55 point => invitesCache[point] || kEmptyInvites,
59 const syncDetails = useCallback(
61 const _contracts = contracts.getOrElse(null);
66 // fetch point details
67 const details = await azimuth.azimuth.getPoint(_contracts, point);
72 [contracts, addToPointCache]
75 const syncBirthday = useCallback(
77 const _contracts = contracts.getOrElse(null);
78 const _web3 = web3.getOrElse(null);
79 if (!_contracts || !web3) {
83 // fetch birthday if not already known—will not change after being set
84 if (Maybe.Nothing.hasInstance(getBirthday(point))) {
85 const birthBlock = await azimuth.azimuth.getActivationBlock(
91 const block = await _web3.eth.getBlock(birthBlock);
93 [point]: Maybe.Just(new Date(block.timestamp * 1000)),
98 [contracts, web3, addToBirthdayCache, getBirthday]
101 const syncInvites = useCallback(
103 const _contracts = contracts.getOrElse(null);
108 const availableInvites = await azimuth.delegatedSending.getTotalUsableInvites(
113 const invitedPoints = await azimuth.delegatedSending.getInvited(
117 const invitedPointDetails = await Promise.all(
118 invitedPoints.map(async invitedPoint => {
119 console.log('invitedPoint', invitedPoint, typeof invitedPoint);
120 const active = await azimuth.azimuth.isActive(
125 point: Number(invitedPoint),
130 const sentInvites = invitedPointDetails.length;
131 const acceptedInvites = invitedPointDetails.filter(i => i.active).length;
135 availableInvites: Maybe.Just(availableInvites),
136 sentInvites: Maybe.Just(sentInvites),
137 acceptedInvites: Maybe.Just(acceptedInvites),
141 [contracts, addToInvitesCache]
144 // sync all of the on-chain info required for a point that the user owns
145 const syncOwnedPoint = useCallback(
153 [syncDetails, syncInvites, syncBirthday]
166export function PointCacheProvider({ children }) {
167 const pointCache = _usePointCache();
170 <PointCacheContext.Provider value={pointCache}>
172 </PointCacheContext.Provider>
177export function usePointCache() {
178 return useContext(PointCacheContext);
182export const withPointCache = Component =>
183 forwardRef((props, ref) => (
184 <PointCacheContext.Consumer>
185 {pointCache => <Component ref={ref} {...pointCache} {...props} />}
186 </PointCacheContext.Consumer>
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