@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
60 lines • 2.43 kB
JavaScript
import { useCallback, useMemo } from "react";
import { accountToPlatformAccount, currencyToPlatformCurrency } from "./converters";
import { filterPlatformAccounts, filterPlatformCurrencies, } from "./filters";
import { isPlatformSupportedCurrency } from "./helpers";
import { getParentAccount } from "../account";
import { listCurrencies } from "../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)
*/
export function usePlatformUrl(manifest, inputs) {
return 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]);
}
export function usePlatformAccounts(walletState, accounts) {
return useMemo(() => {
return accounts.map(account => {
const parentAccount = getParentAccount(account, accounts);
return accountToPlatformAccount(walletState, account, parentAccount);
});
}, [walletState, accounts]);
}
export function useListPlatformAccounts(walletState, accounts) {
const platformAccounts = usePlatformAccounts(walletState, accounts);
return useCallback((filters = {}) => {
return filterPlatformAccounts(platformAccounts, filters);
}, [platformAccounts]);
}
export function usePlatformCurrencies() {
return useMemo(() => {
return listCurrencies(true).reduce((filtered, currency) => {
if (isPlatformSupportedCurrency(currency)) {
filtered.push(currencyToPlatformCurrency(currency));
}
return filtered;
}, []);
}, []);
}
export function useListPlatformCurrencies() {
const currencies = usePlatformCurrencies();
return useCallback((filters) => {
return filterPlatformCurrencies(currencies, filters || {});
}, [currencies]);
}
//# sourceMappingURL=react.js.map