11import { Runtime , useGetRuntimeRoflApps } from 'oasis-nexus/api'
22import { TableLayout } from '../../components/TableLayoutButton'
3- import { useEffect , useState } from 'react'
3+ import { useCallback , useEffect , useState } from 'react'
44import { useScreenSize } from '../../hooks/useScreensize'
55import { useSearchParamsPagination } from '../../components/Table/useSearchParamsPagination'
66import { NUMBER_OF_ITEMS_ON_SEPARATE_PAGE } from '../../../config'
@@ -10,7 +10,8 @@ import { useTranslation } from 'react-i18next'
1010import { useTypedSearchParam } from '../../hooks/useTypedSearchParam'
1111import { textSearch } from '../../components/Search/search-utils'
1212import { 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
1516const 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+
3033export 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
5661export 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}
0 commit comments