Skip to content

Commit 4e74873

Browse files
committed
Rofl app list: support opening single search result on enter
1 parent e8453aa commit 4e74873

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/app/pages/RoflAppsPage/hook.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Runtime, useGetRuntimeRoflApps } from 'oasis-nexus/api'
22
import { TableLayout } from '../../components/TableLayoutButton'
3-
import { useEffect, useState } from 'react'
3+
import { useCallback, useEffect, useState } from 'react'
44
import { useScreenSize } from '../../hooks/useScreensize'
55
import { useSearchParamsPagination } from '../../components/Table/useSearchParamsPagination'
66
import { NUMBER_OF_ITEMS_ON_SEPARATE_PAGE } from '../../../config'
@@ -10,7 +10,8 @@ import { useTranslation } from 'react-i18next'
1010
import { useTypedSearchParam } from '../../hooks/useTypedSearchParam'
1111
import { textSearch } from '../../components/Search/search-utils'
1212
import { getHighlightPattern } from '../../components/Search/search-utils'
13-
import { useSearchParams } from 'react-router-dom'
13+
import { useNavigate, useSearchParams } from 'react-router-dom'
14+
import { encodeURIComponentPretty, RouteUtils } from '../../utils/route-utils'
1415

1516
const limit = NUMBER_OF_ITEMS_ON_SEPARATE_PAGE
1617

@@ -27,10 +28,14 @@ export const useTableViewMode = () => {
2728
return { tableView, setTableView }
2829
}
2930

31+
const ROFL_SEARCH_ARG_NAME = 'name'
32+
3033
export const useROFLAppFiltering = () => {
3134
const setSearchParams = useSearchParams()[1]
3235
const { t } = useTranslation()
33-
const [wantedNameInput, setWantedNameInput] = useTypedSearchParam('name', '', { deleteParams: ['page'] })
36+
const [wantedNameInput, setWantedNameInput] = useTypedSearchParam(ROFL_SEARCH_ARG_NAME, '', {
37+
deleteParams: ['page'],
38+
})
3439
const search = textSearch.roflAppName(wantedNameInput, t)
3540
const { result: wantedNamePattern, warning: nameError } = search
3641
const highlightPattern = getHighlightPattern(search)
@@ -55,8 +60,9 @@ export const useROFLAppFiltering = () => {
5560

5661
export const useRoflApps = (network: Network, layer: Runtime) => {
5762
const { tableView } = useTableViewMode()
63+
const navigate = useNavigate()
5864
const pagination = useSearchParamsPagination('page')
59-
const { wantedNamePattern, hasFilters } = useROFLAppFiltering()
65+
const { wantedNameInput, wantedNamePattern, hasFilters } = useROFLAppFiltering()
6066
const offset = (pagination.selectedPage - 1) * limit
6167
const roflAppsQuery = useGetRuntimeRoflApps(network, layer, {
6268
name: wantedNamePattern,
@@ -80,6 +86,15 @@ export const useRoflApps = (network: Network, layer: Runtime) => {
8086
const hasNoResultsOnSelectedPage = isFetched && !isOnFirstPage && !hasData
8187
const hasNoResultsBecauseOfFilters = !isLoading && !hasData && isOnFirstPage && hasFilters
8288

89+
const jumpToSingleResult = useCallback(() => {
90+
// If we only have a single result
91+
if (hasData && isOnFirstPage && roflApps?.length === 1) {
92+
// Then let's jump to it
93+
const path = `${RouteUtils.getRoflAppRoute(network, roflApps![0].id)}?q=${encodeURIComponentPretty(wantedNameInput)}`
94+
navigate(path)
95+
}
96+
}, [hasData, isOnFirstPage, roflApps, navigate, network, wantedNameInput])
97+
8398
return {
8499
isLoading,
85100
limit,
@@ -88,5 +103,6 @@ export const useRoflApps = (network: Network, layer: Runtime) => {
88103
tablePagination,
89104
hasNoResultsOnSelectedPage,
90105
hasNoResultsBecauseOfFilters,
106+
jumpToSingleResult,
91107
}
92108
}

src/app/pages/RoflAppsPage/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ export const RoflAppsPage: FC = () => {
8080
if (!paraTimesConfig[layer]?.offerRoflTxTypes) throw AppErrors.UnsupportedLayer
8181

8282
const { wantedNameInput, setWantedNameInput, nameError } = useROFLAppFiltering()
83-
const { pagination, isLoading, hasNoResultsBecauseOfFilters } = useRoflApps(network, layer)
83+
const { pagination, isLoading, hasNoResultsBecauseOfFilters, jumpToSingleResult } = useRoflApps(
84+
network,
85+
layer,
86+
)
8487

8588
const searchBar = (
8689
<TableSearchBar
@@ -90,6 +93,7 @@ export const RoflAppsPage: FC = () => {
9093
warning={nameError}
9194
fullWidth={isMobile}
9295
autoFocus={!isMobile}
96+
onEnter={jumpToSingleResult}
9397
/>
9498
)
9599

0 commit comments

Comments
 (0)