@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
97 lines • 4.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSortedFilteredApps = exports.sortFilterApps = exports.filterApps = exports.sortApps = exports.isAppAssociatedCurrencySupported = void 0;
const camelCase_1 = __importDefault(require("lodash/fp/camelCase"));
const currencies_1 = require("../currencies");
const react_1 = require("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 ? (0, currencies_1.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.
*/
function isAppAssociatedCurrencySupported({ app, isFeature, getFeature, }) {
if (["swap", "plugin"].includes(app.type))
return true;
if (!app.currencyId)
return false;
if (!(0, currencies_1.isCurrencySupported)((0, currencies_1.getCryptoCurrencyById)(app.currencyId)))
return false;
const currencyFeatureKey = (0, camelCase_1.default)(`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;
}
exports.isAppAssociatedCurrencySupported = isAppAssociatedCurrencySupported;
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;
}
});
}
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;
});
};
exports.sortApps = sortApps;
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));
};
exports.filterApps = filterApps;
const sortFilterApps = (apps, _filterOptions, _sortOptions) => {
return (0, exports.sortApps)((0, exports.filterApps)(apps, _filterOptions), _sortOptions);
};
exports.sortFilterApps = sortFilterApps;
const useSortedFilteredApps = (apps, _filterOptions, _sortOptions) => {
return (0, react_1.useMemo)(() => (0, exports.sortFilterApps)(apps, _filterOptions, _sortOptions), [apps, _filterOptions, _sortOptions]);
};
exports.useSortedFilteredApps = useSortedFilteredApps;
//# sourceMappingURL=filtering.js.map