Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #421 from juan-cortes/sortFilterForManagerv2
Browse files Browse the repository at this point in the history
Sorting and filtering utils for the managerv2 for mobile and desktop
  • Loading branch information
gre authored Dec 17, 2019
2 parents 13a0433 + 0bce193 commit 1d97ece
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
107 changes: 103 additions & 4 deletions src/apps/filtering.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,109 @@
// @flow
import type { App } from "../types/manager";
import type { InstalledItem } from "./types";
import { getCryptoCurrencyById, isCurrencySupported } from "../currencies";
import { useMemo } from "react";

export type SortOptions = *;
export type SortOptions = {
type: "name" | "marketcap" | "default",
order: "asc" | "desc"
};

export const sortApps = (apps: App[], _options: SortOptions): App[] => apps;
export type FilterOptions = {
query?: string,
installedApps: InstalledItem[],
type: "all" | "installed" | "not_installed" | "supported" | "updatable"
};

export type FilterOptions = *;
type UpdateAwareInstalledApps = {
[string]: boolean // NB [AppName]: isUpdated
};

export const filterApps = (apps: App[], _options: FilterOptions): App[] => apps;
const searchFilter = (query?: string) => ({ name, currencyId }) => {
if (!query) return true;
const currency = currencyId ? getCryptoCurrencyById(currencyId) : null;
const terms = `${name} ${
currency ? `${currency.name} ${currency.ticker}` : ""
}`;
return terms.toLowerCase().includes(query.toLowerCase().trim());
};

const typeFilter = (
type = "all",
updateAwareInstalledApps: UpdateAwareInstalledApps
) => app => {
switch (type) {
case "installed":
return updateAwareInstalledApps.hasOwnProperty(app.name);
case "not_installed":
return !updateAwareInstalledApps.hasOwnProperty(app.name);
case "updatable":
return (
updateAwareInstalledApps.hasOwnProperty(app.name) &&
!updateAwareInstalledApps[app.name]
);
case "supported":
return (
app.currencyId &&
isCurrencySupported(getCryptoCurrencyById(app.currencyId))
);
default:
return true;
}
};

export const sortApps = (apps: App[], _options: SortOptions): App[] => {
const { type, order } = _options;
const asc = order === "asc";
if (type === "default") return apps;

const getScore = ({ indexOfMarketCap: i }: App, reverse: boolean) =>
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: App[], _options: FilterOptions): App[] => {
const { query, installedApps, type = "all" } = _options;
const updateAwareInstalledApps: 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));
};

export const sortFilterApps = (
apps: App[],
_filterOptions: FilterOptions,
_sortOptions: SortOptions
): App[] => {
return sortApps(filterApps(apps, _filterOptions), _sortOptions);
};

export const useSortedFilteredApps = (
apps: App[],
_filterOptions: FilterOptions,
_sortOptions: SortOptions
) => {
const { query, installedApps, type: filterType } = _filterOptions;
const { type: sortType, order } = _sortOptions;

return useMemo(
() =>
sortFilterApps(
apps,
{ query, installedApps, type: filterType },
{ type: sortType, order }
),
[apps, query, installedApps, filterType, sortType, order]
);
};
7 changes: 6 additions & 1 deletion src/apps/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getDependencies, getDependents } from "./polyfill";
import { findCryptoCurrency } from "../currencies";
import type { ListAppsResult, AppOp, Exec, InstalledItem } from "./types";
import type { App, DeviceInfo, FinalFirmware } from "../types/manager";
import { tickersByMarketCap } from "../countervalues/mock";

export const deviceInfo155 = {
version: "1.5.5",
Expand Down Expand Up @@ -76,6 +77,10 @@ export function mockListAppsResult(
.map((name, i) => {
const dependencies = getDependencies(name);
const currency = findCryptoCurrency(c => c.managerAppName === name);
const indexOfMarketCap = currency
? tickersByMarketCap.indexOf(currency.ticker)
: -1;

return {
id: i,
app: i,
Expand All @@ -99,7 +104,7 @@ export function mockListAppsResult(
dateModified: "",
compatibleWallets: [],
currencyId: currency ? currency.id : null,
indexOfMarketCap: 0
indexOfMarketCap
};
});
const appByName = {};
Expand Down
2 changes: 1 addition & 1 deletion src/countervalues/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const fetchExchangesForPairImplementation = async () => {
];
};

const tickersByMarketCap = [
export const tickersByMarketCap = [
"BTC",
"ETH",
"XRP",
Expand Down

0 comments on commit 1d97ece

Please sign in to comment.