-
Notifications
You must be signed in to change notification settings - Fork 7
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 #478 from oasisprotocol/csillag/adjust-global-search
Adjust global search
- Loading branch information
Showing
9 changed files
with
124 additions
and
67 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
63 changes: 51 additions & 12 deletions
63
src/app/pages/SearchResultsPage/GlobalSearchResultsView.tsx
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,34 +1,73 @@ | ||
import { FC } from 'react' | ||
import { FC, useState } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import { getFilterForScope, getKeyForScope, getNameForScope } from '../../../types/searchScope' | ||
import { RouteUtils } from '../../utils/route-utils' | ||
import { SearchResults } from './hooks' | ||
import { NoResults } from './NoResults' | ||
import { NoResultsOnMainnet, NoResultsWhatsoever } from './NoResults' | ||
import { SearchResultsList } from './SearchResultsList' | ||
import { AllTokenPrices } from '../../../coin-gecko/api' | ||
import { | ||
getFilterForNetwork, | ||
getNetworkNames, | ||
isNotMainnet, | ||
isNotOnMainnet, | ||
isOnMainnet, | ||
Network, | ||
} from '../../../types/network' | ||
import { HideMoreResults, ShowMoreResults } from './notifications' | ||
import { getThemesForNetworks } from '../../../styles/theme' | ||
import { orderByLayer } from '../../../types/layers' | ||
import { useRedirectIfSingleResult } from './useRedirectIfSingleResult' | ||
|
||
export const GlobalSearchResultsView: FC<{ searchResults: SearchResults; tokenPrices: AllTokenPrices }> = ({ | ||
searchResults, | ||
tokenPrices, | ||
}) => { | ||
const { t } = useTranslation() | ||
const [othersOpen, setOthersOpen] = useState(false) | ||
useRedirectIfSingleResult(undefined, searchResults) | ||
|
||
return searchResults.length === 0 ? ( | ||
<NoResults /> | ||
) : ( | ||
const themes = getThemesForNetworks() | ||
const networkNames = getNetworkNames(t) | ||
const otherNetworks = RouteUtils.getEnabledNetworks().filter(isNotMainnet) | ||
const notificationTheme = themes[Network.testnet] | ||
const mainnetResults = searchResults.filter(isOnMainnet).sort(orderByLayer) | ||
const otherResults = searchResults.filter(isNotOnMainnet).sort(orderByLayer) | ||
|
||
return ( | ||
<> | ||
{RouteUtils.getEnabledSearchScopes().map(scope => ( | ||
{!mainnetResults.length && (otherResults.length ? <NoResultsOnMainnet /> : <NoResultsWhatsoever />)} | ||
{ | ||
<SearchResultsList | ||
key={getKeyForScope(scope)} | ||
title={getNameForScope(t, scope)} | ||
searchResults={searchResults.filter(getFilterForScope(scope))} | ||
networkForTheme={scope.network} | ||
key={Network.mainnet} | ||
title={networkNames[Network.mainnet]} | ||
searchResults={mainnetResults} | ||
networkForTheme={Network.mainnet} | ||
tokenPrices={tokenPrices} | ||
/> | ||
))} | ||
} | ||
{otherResults.length !== 0 && | ||
(othersOpen ? ( | ||
<> | ||
<HideMoreResults theme={notificationTheme} onHide={() => setOthersOpen(false)} /> | ||
{otherNetworks.map(net => ( | ||
<SearchResultsList | ||
key={net} | ||
title={networkNames[net]} | ||
searchResults={otherResults.filter(getFilterForNetwork(net))} | ||
networkForTheme={net} | ||
tokenPrices={tokenPrices} | ||
/> | ||
))} | ||
</> | ||
) : ( | ||
<ShowMoreResults | ||
theme={notificationTheme} | ||
hasWantedResults={!!mainnetResults.length} | ||
otherResultsCount={otherResults.length} | ||
onShow={() => setOthersOpen(true)} | ||
/> | ||
))} | ||
</> | ||
) | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Here we need to import from the generated code, in order to break | ||
// a cycle of imports which confuse jest | ||
// eslint-disable-next-line no-restricted-imports | ||
import { Layer } from '../oasis-indexer/generated/api' | ||
import { TFunction } from 'i18next' | ||
|
||
export const getLayerNames = (t: TFunction): Record<Layer, string> => ({ | ||
[Layer.emerald]: t('common.emerald'), | ||
[Layer.sapphire]: t('common.sapphire'), | ||
[Layer.cipher]: t('common.cipher'), | ||
[Layer.consensus]: t('common.consensus'), | ||
}) | ||
|
||
interface HasLayer { | ||
layer: Layer | ||
} | ||
|
||
export const getFilterForLayer = (layer: Layer) => (item: HasLayer) => item.layer === layer | ||
|
||
const layerOrder: Record<Layer, number> = { | ||
[Layer.consensus]: 1, | ||
[Layer.sapphire]: 2, | ||
[Layer.emerald]: 3, | ||
[Layer.cipher]: 4, | ||
} | ||
|
||
export const orderByLayer = (itemA: HasLayer, itemB: HasLayer): number => | ||
layerOrder[itemA.layer] - layerOrder[itemB.layer] |
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