Skip to content

Commit

Permalink
Change SearchResults format
Browse files Browse the repository at this point in the history
Don't list all blocks, txs, accounts separately;
use a single list, with type information added.
  • Loading branch information
csillag committed Jun 7, 2023
1 parent 69b2373 commit 635302c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const GlobalSearchResultsView: FC<{ searchResults: SearchResults; tokenPr
tokenPrices,
}) => {
const { t } = useTranslation()
return searchResults.allResults.length === 0 ? (
return searchResults.length === 0 ? (
<NoResults />
) : (
<>
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/SearchResultsPage/ScopedSearchResultsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export const ScopedSearchResultsView: FC<{
const isNotInWantedScope = getInverseFilterForScope(wantedScope)
const isOnWantedNetworkInOtherParatime = (item: HasScope) =>
item.network === wantedScope.network && item.layer !== wantedScope.layer
const hasWantedResults = searchResults.allResults.some(isInWantedScope)
const otherResults = searchResults.allResults.filter(isNotInWantedScope)
const hasWantedResults = searchResults.some(isInWantedScope)
const otherResults = searchResults.filter(isNotInWantedScope)
const hasMainnetResults = otherResults.some(isOnMainnet)
const notificationTheme = themes[hasMainnetResults ? Network.mainnet : Network.testnet]

Expand Down
16 changes: 11 additions & 5 deletions src/app/pages/SearchResultsPage/SearchResultsFiltered.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BlockDetailView } from '../BlockDetailPage'
import { RouteUtils } from '../../utils/route-utils'
import { TransactionDetailView } from '../TransactionDetailPage'
import { AccountDetailsView } from '../AccountDetailsPage'
import { SearchResults } from './hooks'
import { AccountResult, BlockResult, SearchResults, TransactionResult } from './hooks'
import { HasScope } from '../../../oasis-indexer/api'
import { getThemesForNetworks } from '../../../styles/theme'
import { Network } from '../../../types/network'
Expand All @@ -30,7 +30,7 @@ export const SearchResultsFiltered: FC<{
}> = ({ filter, title, networkForTheme, searchResults, tokenPrices }) => {
const { t } = useTranslation()

const numberOfResults = searchResults.allResults.filter(filter).length
const numberOfResults = searchResults.filter(filter).length

if (!numberOfResults) {
return null
Expand All @@ -48,15 +48,19 @@ export const SearchResultsFiltered: FC<{
>
<ResultsGroupByType
title={t('search.results.blocks.title')}
results={searchResults.blocks.filter(filter)}
results={searchResults
.filter(filter)
.filter((item): item is BlockResult => item.resultType === 'block')}
resultComponent={item => <BlockDetailView isLoading={false} block={item} showLayer={true} />}
link={block => RouteUtils.getBlockRoute(block, block.round)}
linkLabel={t('search.results.blocks.viewLink')}
/>

<ResultsGroupByType
title={t('search.results.transactions.title')}
results={searchResults.transactions.filter(filter)}
results={searchResults
.filter(filter)
.filter((item): item is TransactionResult => item.resultType === 'transaction')}
resultComponent={item => (
<TransactionDetailView
isLoading={false}
Expand All @@ -71,7 +75,9 @@ export const SearchResultsFiltered: FC<{

<ResultsGroupByType
title={t('search.results.accounts.title')}
results={searchResults.accounts.filter(filter)}
results={searchResults
.filter(filter)
.filter((item): item is AccountResult => item.resultType === 'account')}
resultComponent={item => (
<AccountDetailsView
isLoading={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { renderWithProviders } from '../../../utils/renderWithProviders'

import {
sapphireParsedBlock,
suggestedParsedAccount,
suggestedParsedAccountResult,
suggestedParsedBlock,
} from '../../../utils/test-fixtures'
import { Network } from '../../../../types/network'
Expand All @@ -22,12 +22,7 @@ describe('SearchResultsView', () => {
it('block should correctly link to transactions', () => {
renderWithProviders(
<SearchResultsFiltered
searchResults={{
blocks: [suggestedParsedBlock, sapphireParsedBlock],
transactions: [],
accounts: [],
allResults: [suggestedParsedBlock, sapphireParsedBlock],
}}
searchResults={[suggestedParsedBlock, sapphireParsedBlock]}
tokenPrices={{
[Network.mainnet]: {
isLoading: false,
Expand Down Expand Up @@ -64,12 +59,7 @@ describe('SearchResultsView', () => {
it('account should correctly link to erc-20 tokens', () => {
renderWithProviders(
<SearchResultsFiltered
searchResults={{
blocks: [],
transactions: [],
accounts: [suggestedParsedAccount],
allResults: [suggestedParsedAccount],
}}
searchResults={[suggestedParsedAccountResult]}
title={'test search'}
filter={() => true}
networkForTheme={Network.mainnet}
Expand Down
30 changes: 18 additions & 12 deletions src/app/pages/SearchResultsPage/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Runtime,
Layer,
isAccountNonEmpty,
HasScope,
} from '../../../oasis-indexer/api'
import { RouteUtils } from '../../utils/route-utils'
import { SearchParams } from '../../components/Search/search-utils'
Expand All @@ -18,13 +19,20 @@ function isDefined<T>(item: T): item is NonNullable<T> {

export type ConditionalResults<T> = { isLoading: boolean; results: T[] }

export type SearchResults = {
blocks: RuntimeBlock[]
transactions: RuntimeTransaction[]
accounts: RuntimeAccount[]
allResults: (RuntimeBlock | RuntimeTransaction | RuntimeAccount)[]
type SearchResultItemCore = HasScope & {
resultType: 'block' | 'transaction' | 'account'
}

export type BlockResult = SearchResultItemCore & RuntimeBlock & { resultType: 'block' }

export type TransactionResult = SearchResultItemCore & RuntimeTransaction & { resultType: 'transaction' }

export type AccountResult = SearchResultItemCore & RuntimeAccount & { resultType: 'account' }

export type SearchResultItem = BlockResult | TransactionResult | AccountResult

export type SearchResults = SearchResultItem[]

export function useBlocksConditionally(blockHeight: string | undefined): ConditionalResults<RuntimeBlock> {
const queries = RouteUtils.getEnabledScopes()
.filter(scope => scope.layer !== Layer.consensus)
Expand Down Expand Up @@ -104,13 +112,11 @@ export const useSearch = (q: SearchParams) => {
...(queries.oasisAccount.results || []),
...(queries.evmBech32Account.results || []),
].filter(isAccountNonEmpty)
const allResults = [...blocks, ...transactions, ...accounts]
const results: SearchResults = {
blocks,
transactions,
accounts,
allResults,
}
const results: SearchResultItem[] = [
...blocks.map((block): BlockResult => ({ ...block, resultType: 'block' })),
...transactions.map((tx): TransactionResult => ({ ...tx, resultType: 'transaction' })),
...accounts.map((account): AccountResult => ({ ...account, resultType: 'account' })),
]
return {
isLoading,
results,
Expand Down
33 changes: 19 additions & 14 deletions src/app/pages/SearchResultsPage/useRedirectIfSingleResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,32 @@ export function useRedirectIfSingleResult(
) {
const navigate = useNavigate()

let shouldRedirect = !isLoading && results.allResults.length === 1
let shouldRedirect = !isLoading && results.length === 1

if (shouldRedirect) {
const result = results.allResults[0]
const result = results[0]
shouldRedirect = scope ? isItemInScope(result, scope) : result.network === Network.mainnet
}

let redirectTo: string | undefined

const block = results.blocks[0]
const tx = results.transactions[0]
const account = results.accounts[0]
if (shouldRedirect) {
if (block) {
redirectTo = RouteUtils.getBlockRoute(block, block.round)
} else if (tx) {
redirectTo = RouteUtils.getTransactionRoute(tx, tx.eth_hash || tx.hash)
} else if (account) {
redirectTo = RouteUtils.getAccountRoute(account, account.address_eth ?? account.address)
} else {
// TODO: typescript should ensure all queries are handled
const item = results[0]
switch (item.resultType) {
case 'block':
redirectTo = RouteUtils.getBlockRoute(item, item.round)
break
case 'transaction':
redirectTo = RouteUtils.getTransactionRoute(item, item.eth_hash || item.hash)
break
case 'account':
redirectTo = RouteUtils.getAccountRoute(item, item.address_eth ?? item.address)
break
default:
// The conversion of any is necessary here, since we have covered all possible subtype,
// and TS is concluding that the only possible remaining type is "never".
// However, if we all more result types in the future and forget to add the appropriate case here,
// we might hit this, hence the warning.
console.log(`Don't know how to redirect to unknown search result type ${(item as any).resultType}`)
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/app/utils/test-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import {
EvmTokenType,
groupAccountTokenBalances,
Layer,
RuntimeAccount,
RuntimeBlock,
} from '../../oasis-indexer/api'
import { EvmTokenType, groupAccountTokenBalances, Layer, RuntimeAccount } from '../../oasis-indexer/api'
import { Ticker } from '../../types/ticker'
import { Network } from '../../types/network'
import { AccountResult, BlockResult } from '../pages/SearchResultsPage/hooks'

export const suggestedParsedBlock: RuntimeBlock = {
export const suggestedParsedBlock: BlockResult = {
round: 1396255,
hash: '42efb4c989d219842aa0c5f7fa11fd24f913612a7235d4564b95caf8aa20fb8b',
timestamp: '2022-05-13T06:39:03Z',
Expand All @@ -17,9 +12,10 @@ export const suggestedParsedBlock: RuntimeBlock = {
gas_used: 1482530,
layer: Layer.emerald,
network: Network.mainnet,
resultType: 'block',
}

export const sapphireParsedBlock: RuntimeBlock = {
export const sapphireParsedBlock: BlockResult = {
round: 143553,
hash: '91cc80baead4779221cadfe756a959273697850f9ef994b0e2b2ac0a178b86ca',
timestamp: '2023-02-17T10:57:09Z',
Expand All @@ -28,6 +24,7 @@ export const sapphireParsedBlock: RuntimeBlock = {
gas_used: 11292,
layer: Layer.sapphire,
network: Network.mainnet,
resultType: 'block',
}

export const suggestedParsedAccount: RuntimeAccount = groupAccountTokenBalances({
Expand Down Expand Up @@ -66,3 +63,8 @@ export const suggestedParsedAccount: RuntimeAccount = groupAccountTokenBalances(
network: Network.mainnet,
ticker: Ticker.ROSE,
})

export const suggestedParsedAccountResult: AccountResult = {
...suggestedParsedAccount,
resultType: 'account',
}

0 comments on commit 635302c

Please sign in to comment.