on d760cbf help sign in
bridge/src/store/lib/useDetailsStore.js
50 lines 1.2 kB history raw
1import { useState, useCallback } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import * as azimuth from 'azimuth-js';
5import { useNetwork } from '../network';
7export default function useDetailsStore() {
8 const { contracts } = useNetwork();
9 const [detailsCache, _setDetailsCache] = useState({});
11 const addToDetails = useCallback(
12 entry =>
13 _setDetailsCache(cache => ({
14 ...cache,
15 ...entry,
16 })),
17 [_setDetailsCache]
18 );
20 // TODO: refactor detailsCache access to use accessor like bithday
21 // Maybe<{}>
22 const getDetails = useCallback(
23 point => (point in detailsCache ? Just(detailsCache[point]) : Nothing()),
24 [detailsCache]
25 );
27 const syncDetails = useCallback(
28 async point => {
29 const _contracts = contracts.getOrElse(null);
30 if (!_contracts) {
31 return;
32 }
34 // fetch point details
35 const details = await azimuth.azimuth.getPoint(_contracts, point);
36 addToDetails({
37 [point]: details,
38 });
39 },
40 [contracts, addToDetails]
41 );
43 return {
44 // TODO: refactor accessors to use getDetails instead of
45 // touching the cache directly
46 pointCache: detailsCache,
47 getDetails,
48 syncDetails,
49 };