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(),
22function _usePointCache() {
23 const { contracts, web3 } = useNetwork();
24 const [pointCache, _setPointCache] = useState({});
25 const [birthdayCache, _setBirthdayCache] = useState({});
26 const [invitesCache, _setInvitesCache] = useState({});
28 const addToPointCache = useCallback(
29 entry => _setPointCache(cache => ({ ...cache, ...entry })),
33 const addToBirthdayCache = useCallback(
34 entry => _setBirthdayCache(cache => ({ ...cache, ...entry })),
38 const addToInvitesCache = useCallback(
39 entry => _setInvitesCache(cache => ({ ...cache, ...entry })),
43 // TODO: refactor pointCache access to use accessor like bithday
44 const getBirthday = useCallback(
45 point => birthdayCache[point] || Maybe.Nothing(),
48 const getInvites = useCallback(
49 point => invitesCache[point] || kEmptyInvites,
53 const fetchPoint = useCallback(
55 const _contracts = contracts.getOrElse(null);
56 const _web3 = web3.getOrElse(null);
57 if (!_contracts || !_web3) {
61 // fetch point details
62 const details = await azimuth.azimuth.getPoint(_contracts, point);
63 addToPointCache({ [point]: details });
66 const count = await azimuth.delegatedSending.getTotalUsableInvites(
72 availableInvites: Maybe.Just(count),
73 // TODO: look up sent/accepted on-chain
74 sentInvites: Maybe.Just(6),
75 acceptedInvites: Maybe.Just(5),
79 // fetch birthday (if not already known — will not change after being set)
80 if (Maybe.Nothing.hasInstance(getBirthday(point))) {
81 const birthBlock = await azimuth.azimuth.getActivationBlock(
87 const block = await _web3.eth.getBlock(birthBlock);
89 [point]: Maybe.Just(new Date(block.timestamp * 1000)),
113export function PointCacheProvider({ children }) {
114 const pointCache = _usePointCache();
117 <PointCacheContext.Provider value={pointCache}>
119 </PointCacheContext.Provider>
124export function usePointCache() {
125 return useContext(PointCacheContext);
129export const withPointCache = Component =>
130 forwardRef((props, ref) => (
131 <PointCacheContext.Consumer>
132 {pointCache => <Component ref={ref} {...pointCache} {...props} />}
133 </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