forked from LedgerHQ/ledger-live-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiltering.js
132 lines (119 loc) · 3.58 KB
/
filtering.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// @flow
import type { App } from "../types/manager";
import type { InstalledItem } from "./types";
import { getCryptoCurrencyById, isCurrencySupported } from "../currencies";
import { useMemo } from "react";
export type SortOptions = {
type: "name" | "marketcap" | "default",
order: "asc" | "desc"
};
export type AppType =
| "all"
| "installed"
| "not_installed"
| "supported"
| "not_supported"
| "updatable";
export type FilterOptions = {
query?: string,
installQueue?: string[],
installedApps: InstalledItem[],
type: AppType[]
};
type UpdateAwareInstalledApps = {
[string]: boolean // NB [AppName]: isUpdated
};
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 = (
filters: AppType[] = ["all"],
updateAwareInstalledApps: UpdateAwareInstalledApps,
installQueue: string[] = []
) => 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 (
app.currencyId &&
isCurrencySupported(getCryptoCurrencyById(app.currencyId))
);
case "not_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, installQueue, 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, installQueue));
};
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,
installQueue
} = _filterOptions;
const { type: sortType, order } = _sortOptions;
return useMemo(
() =>
sortFilterApps(
apps,
{ query, installedApps, type: filterType, installQueue },
{ type: sortType, order }
),
[apps, query, installedApps, filterType, installQueue, sortType, order]
);
};