@hypernetlabs/web-ui
Version:
Web ui react components for hypernet protocol
181 lines • 7.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useBalances = void 0;
const contexts_1 = require("../contexts");
const react_1 = require("react");
const react_alert_1 = require("react-alert");
var EActionTypes;
(function (EActionTypes) {
EActionTypes["FETCHING"] = "FETCHING";
EActionTypes["FETCHED"] = "FETCHED";
EActionTypes["ERROR"] = "ERROR";
EActionTypes["TOKEN_SELECTED"] = "TOKEN_SELECTED";
EActionTypes["STATE_CHANNEL_CHANGED"] = "STATE_CHANNEL_CHANGED";
})(EActionTypes || (EActionTypes = {}));
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function useBalances() {
const { coreProxy, UIData } = (0, contexts_1.useStoreContext)();
const { setLoading } = (0, contexts_1.useLayoutContext)();
const alert = (0, react_alert_1.useAlert)();
const initialState = {
error: false,
loading: true,
balances: [],
balancesByChannelAddress: [],
balancesByChannelAddresses: new Map(),
preferredPaymentToken: undefined,
};
const [state, dispatch] = (0, react_1.useReducer)((state, action) => {
switch (action.type) {
case EActionTypes.FETCHING:
return {
...state,
error: false,
loading: true,
};
case EActionTypes.FETCHED:
return {
...state,
error: false,
loading: false,
balances: prepareBalances(action.payload),
balancesByChannelAddresses: prepareBalancesByChannelAddresses(action.payload),
};
case EActionTypes.STATE_CHANNEL_CHANGED:
return {
...state,
error: false,
loading: false,
balancesByChannelAddress: prepareBalancesByChannelAddress(action.payload),
};
case EActionTypes.TOKEN_SELECTED:
return {
...state,
error: false,
loading: false,
preferredPaymentToken: action.payload,
};
case EActionTypes.ERROR:
return {
...state,
error: true,
loading: false,
};
default:
return state;
}
}, initialState);
// useEffect(() => {
// console.log("STATE CHANNEL CHANGED !")
// }, [UIData.getSelectedStateChannel().channelAddress]);
(0, react_1.useEffect)(() => {
let cancelRequest = false;
const fetchData = async () => {
if (cancelRequest)
return;
dispatch({ type: EActionTypes.FETCHING });
setLoading(true);
coreProxy === null || coreProxy === void 0 ? void 0 : coreProxy.payments.getBalances().map((_balances) => {
setLoading(false);
dispatch({
type: EActionTypes.FETCHED,
payload: _balances,
});
const selectedStateChannel = UIData.getSelectedStateChannel();
if (selectedStateChannel == null ||
selectedStateChannel.channelAddress == null) {
coreProxy.payments
.getActiveStateChannels()
.map((activeStateChannels) => {
dispatch({
type: EActionTypes.STATE_CHANNEL_CHANGED,
payload: {
balances: _balances,
activeStateChannel: activeStateChannels[0],
},
});
});
}
else {
dispatch({
type: EActionTypes.STATE_CHANNEL_CHANGED,
payload: {
balances: _balances,
activeStateChannel: selectedStateChannel,
},
});
}
}).mapErr((error) => {
setLoading(false);
alert.error(error.message || "An error has happened while pulling balances");
dispatch({ type: EActionTypes.ERROR, payload: error.message });
});
};
fetchData();
coreProxy === null || coreProxy === void 0 ? void 0 : coreProxy.onBalancesChanged.subscribe({
next: (balance) => {
if (cancelRequest)
return;
dispatch({
type: EActionTypes.FETCHED,
payload: balance,
});
const selectedStateChannel = UIData.getSelectedStateChannel();
dispatch({
type: EActionTypes.STATE_CHANNEL_CHANGED,
payload: {
balances: balance,
activeStateChannel: selectedStateChannel,
},
});
},
});
UIData.onSelectedStateChannelChanged.subscribe({
next: (activeStateChannel) => {
if (cancelRequest)
return;
coreProxy === null || coreProxy === void 0 ? void 0 : coreProxy.payments.getBalances().map((balances) => {
setLoading(false);
dispatch({
type: EActionTypes.FETCHED,
payload: balances,
});
dispatch({
type: EActionTypes.STATE_CHANNEL_CHANGED,
payload: {
balances,
activeStateChannel,
},
});
});
},
});
return function cleanup() {
cancelRequest = true;
};
}, []);
function prepareBalances(balance) {
return balance.assets.reduce((acc, assetBalance) => {
acc.push(assetBalance);
return acc;
}, []);
}
function prepareBalancesByChannelAddresses(balance) {
return balance.assets.reduce((acc, assetBalance) => {
acc.set(assetBalance.channelAddress, [
...(acc.get(assetBalance.channelAddress) || []),
assetBalance,
]);
return acc;
}, new Map());
}
function prepareBalancesByChannelAddress(balancesWithStateChannel) {
return balancesWithStateChannel.balances.assets.filter((assetBalance) => assetBalance.channelAddress ===
balancesWithStateChannel.activeStateChannel.channelAddress);
}
return {
...state,
};
}
exports.useBalances = useBalances;
//# sourceMappingURL=useBalances.js.map