@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
117 lines • 5.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePlatformUrl = usePlatformUrl;
exports.usePlatformAccounts = usePlatformAccounts;
exports.useListPlatformAccounts = useListPlatformAccounts;
exports.useListPlatformCurrencies = useListPlatformCurrencies;
const react_1 = require("react");
const react_redux_1 = require("react-redux");
const minimatch_1 = require("minimatch");
const api_1 = require("@ledgerhq/cryptoassets/cal-client/state-manager/api");
const converters_1 = require("./converters");
const filters_1 = require("./filters");
const helpers_1 = require("./helpers");
const account_1 = require("../account");
const currencies_1 = require("../currencies");
/**
* TODO: we might want to use "searchParams.append" instead of "searchParams.set"
* to handle duplicated query params (example: "?foo=bar&foo=baz")
*
* We can also use the stringify method of qs (https://github.com/ljharb/qs#stringifying)
*/
function usePlatformUrl(manifest, inputs) {
return (0, react_1.useMemo)(() => {
const url = new URL(manifest.url.toString());
if (inputs) {
for (const key in inputs) {
const value = inputs[key];
if (Object.prototype.hasOwnProperty.call(inputs, key) && value !== undefined) {
url.searchParams.set(key, value);
}
}
}
if (manifest.params) {
url.searchParams.set("params", JSON.stringify(manifest.params));
}
return url;
}, [manifest.url, manifest.params, inputs]);
}
function usePlatformAccounts(walletState, accounts) {
return (0, react_1.useMemo)(() => {
return accounts.map(account => {
const parentAccount = (0, account_1.getParentAccount)(account, accounts);
return (0, converters_1.accountToPlatformAccount)(walletState, account, parentAccount);
});
}, [walletState, accounts]);
}
function useListPlatformAccounts(walletState, accounts) {
const platformAccounts = usePlatformAccounts(walletState, accounts);
return (0, react_1.useCallback)((filters = {}) => {
return (0, filters_1.filterPlatformAccounts)(platformAccounts, filters);
}, [platformAccounts]);
}
function useListPlatformCurrencies(deactivatedCurrencyIds) {
const dispatch = (0, react_redux_1.useDispatch)();
return (0, react_1.useCallback)(async (filters) => {
const filterCurrencyRegexes = filters?.currencies
? filters.currencies.map(filter => (0, minimatch_1.makeRe)(filter))
: null;
// 1. Gather all supported parent currencies
const allCurrencies = (0, currencies_1.listSupportedCurrencies)().reduce((acc, c) => {
if ((0, helpers_1.isPlatformSupportedCurrency)(c) && !deactivatedCurrencyIds.has(c.id))
acc.push((0, converters_1.currencyToPlatformCurrency)(c));
return acc;
}, []);
// 2. Determine which currencies to include based on patterns
let includedCurrencies = allCurrencies;
if (filterCurrencyRegexes) {
includedCurrencies = allCurrencies.filter(c => {
if (filterCurrencyRegexes && !filterCurrencyRegexes.some(regex => c.id.match(regex))) {
return false;
}
return true;
});
}
if (filters?.includeTokens === false) {
return includedCurrencies;
}
// 3. Determine which token families to fetch (only if not already fetched as specific tokens)
const familiesToFetch = new Set();
includedCurrencies.forEach(c => {
if (c.type === "CryptoCurrency")
familiesToFetch.add(c.family);
});
// 4. Fetch tokens for relevant families
const fetchAllPagesForFamily = async (family) => {
const args = { networkFamily: family, pageSize: 1000 };
let hasNextPage = true;
let data;
while (hasNextPage) {
const querySub = dispatch(api_1.endpoints.getTokensData.initiate(args, data ? { direction: "forward" } : undefined));
try {
const result = await querySub;
data = result.data;
hasNextPage = result.hasNextPage;
if (result.error)
throw result.error;
}
finally {
querySub.unsubscribe();
}
}
return (data?.pages ?? []).flatMap(p => p.tokens);
};
const tokensByFamily = await Promise.all([...familiesToFetch].map(f => fetchAllPagesForFamily(f)));
// 5. Combine all results
return tokensByFamily.reduce((acc, tokens) => {
return tokens.reduce((tAcc, t) => {
const pc = (0, converters_1.currencyToPlatformCurrency)(t);
if (!filterCurrencyRegexes || filterCurrencyRegexes.some(r => pc.id.match(r))) {
tAcc.push(pc);
}
return tAcc;
}, acc);
}, includedCurrencies);
}, [deactivatedCurrencyIds, dispatch]);
}
//# sourceMappingURL=react.js.map