This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from juan-cortes/sortFilterForManagerv2
Sorting and filtering utils for the managerv2 for mobile and desktop
- Loading branch information
Showing
3 changed files
with
110 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters