@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
86 lines • 3.77 kB
JavaScript
import camelCase from "lodash/fp/camelCase";
import { getCryptoCurrencyById, isCurrencySupported } from "../currencies";
import { useMemo } from "react";
const searchFilter = (query) => ({ name, currencyId }) => {
if (!query)
return true;
// Nb allow for multiple comma separated search terms
const queries = query
.split(",")
.map(t => t.toLowerCase().trim())
.filter(Boolean);
const currency = currencyId ? getCryptoCurrencyById(currencyId) : null;
const terms = `${name} ${currency ? `${currency.name} ${currency.ticker}` : ""}`;
return queries.some(query => terms.toLowerCase().includes(query));
};
/**
* Checks if the app's associated currency is supported in Ledger Live.
*
* It boils down to: if the app has a currencyId, check if the currency is supported.
* The currency can be unsupported if there is a feature flag that disables it.
*/
export function isAppAssociatedCurrencySupported({ app, isFeature, getFeature, }) {
if (["swap", "plugin"].includes(app.type))
return true;
if (!app.currencyId)
return false;
if (!isCurrencySupported(getCryptoCurrencyById(app.currencyId)))
return false;
const currencyFeatureKey = camelCase(`currency_${app.currencyId}`);
if (!isFeature(currencyFeatureKey))
return true; // no associated feature flag, the currency is supported
if (getFeature(currencyFeatureKey)?.enabled === false)
return false;
return true;
}
function typeFilter(filters = ["all"], updateAwareInstalledApps, installQueue = [], isFeature, getFeature) {
return (app) => filters.every(filter => {
switch (filter) {
case "installed":
return installQueue.includes(app.name) || app.name in updateAwareInstalledApps;
case "not_installed":
return !(app.name in updateAwareInstalledApps);
case "updatable":
return app.name in updateAwareInstalledApps && !updateAwareInstalledApps[app.name];
case "supported":
return isAppAssociatedCurrencySupported({ app, isFeature, getFeature });
case "not_supported":
return !isAppAssociatedCurrencySupported({ app, isFeature, getFeature });
default:
return true;
}
});
}
export const sortApps = (apps, _options) => {
const { type, order } = _options;
const asc = order === "asc";
if (type === "default")
return apps;
const getScore = ({ indexOfMarketCap: i }, reverse) => i === -1 ? (reverse ? -10e6 : 10e6) : i;
return [...apps].sort((a1, b1) => {
const [a, b] = asc ? [a1, b1] : [b1, a1];
let diff = 0;
if (type === "marketcap")
diff = getScore(b, asc) - getScore(a, asc);
if (diff === 0)
diff = a.name.localeCompare(b.name);
return diff;
});
};
export const filterApps = (apps, _options) => {
const { query, installedApps, installQueue, type = ["all"] } = _options;
const updateAwareInstalledApps = {};
for (let i = 0; i < installedApps.length; i++) {
updateAwareInstalledApps[installedApps[i].name] = installedApps[i].updated;
}
return apps
.filter(searchFilter(query))
.filter(typeFilter(type, updateAwareInstalledApps, installQueue, _options.isFeature, _options.getFeature));
};
export const sortFilterApps = (apps, _filterOptions, _sortOptions) => {
return sortApps(filterApps(apps, _filterOptions), _sortOptions);
};
export const useSortedFilteredApps = (apps, _filterOptions, _sortOptions) => {
return useMemo(() => sortFilterApps(apps, _filterOptions, _sortOptions), [apps, _filterOptions, _sortOptions]);
};
//# sourceMappingURL=filtering.js.map