Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
c7cb8410
Commit
c7cb8410
authored
May 17, 2023
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
search: add simple match item to suggest
parent
de3956e2
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
107 additions
and
15 deletions
+107
-15
search.ts
types/api/search.ts
+4
-4
SearchBar.pw.tsx
ui/snippets/searchBar/SearchBar.pw.tsx
+36
-0
SearchBar.tsx
ui/snippets/searchBar/SearchBar.tsx
+2
-2
SearchBarSuggest.tsx
ui/snippets/searchBar/SearchBarSuggest.tsx
+61
-8
useSearchQuery.tsx
ui/snippets/searchBar/useSearchQuery.tsx
+4
-1
No files found.
types/api/search.ts
View file @
c7cb8410
...
...
@@ -13,20 +13,20 @@ export interface SearchResultAddressOrContract {
type
:
'
address
'
|
'
contract
'
;
name
:
string
|
null
;
address
:
string
;
url
:
string
;
url
?:
string
;
// not used by the frontend, we build the url ourselves
}
export
interface
SearchResultBlock
{
type
:
'
block
'
;
block_number
:
number
;
block_number
:
number
|
string
;
block_hash
:
string
;
url
:
string
;
url
?:
string
;
// not used by the frontend, we build the url ourselves
}
export
interface
SearchResultTx
{
type
:
'
transaction
'
;
tx_hash
:
string
;
url
:
string
;
url
?:
string
;
// not used by the frontend, we build the url ourselves
}
export
type
SearchResultItem
=
SearchResultToken
|
SearchResultAddressOrContract
|
SearchResultBlock
|
SearchResultTx
;
...
...
ui/snippets/searchBar/SearchBar.pw.tsx
View file @
c7cb8410
...
...
@@ -132,3 +132,39 @@ test('search by tx hash +@mobile', async({ mount, page }) => {
await
expect
(
page
).
toHaveScreenshot
({
clip
:
{
x
:
0
,
y
:
0
,
width
:
1200
,
height
:
300
}
});
});
test
(
'
search with simple match
'
,
async
({
mount
,
page
})
=>
{
const
API_URL
=
buildApiUrl
(
'
search
'
)
+
`?q=
${
searchMock
.
tx1
.
tx_hash
}
`
;
const
API_CHECK_REDIRECT_URL
=
buildApiUrl
(
'
search_check_redirect
'
)
+
`?q=
${
searchMock
.
tx1
.
tx_hash
}
`
;
await
page
.
route
(
API_URL
,
(
route
)
=>
route
.
fulfill
({
status
:
200
,
body
:
JSON
.
stringify
({
items
:
[
searchMock
.
tx1
,
],
}),
}));
await
page
.
route
(
API_CHECK_REDIRECT_URL
,
(
route
)
=>
route
.
fulfill
({
status
:
200
,
body
:
JSON
.
stringify
({
parameter
:
searchMock
.
tx1
.
tx_hash
,
redirect
:
true
,
type
:
'
transaction
'
,
}),
}));
await
mount
(
<
TestApp
>
<
SearchBar
/>
</
TestApp
>,
);
await
page
.
getByPlaceholder
(
/search/i
).
type
(
searchMock
.
tx1
.
tx_hash
);
await
page
.
waitForResponse
(
API_URL
);
const
resultText
=
page
.
getByText
(
'
Found 1 matching result
'
);
expect
(
resultText
).
toBeTruthy
();
const
links
=
page
.
getByText
(
searchMock
.
tx1
.
tx_hash
);
await
expect
(
links
).
toHaveCount
(
1
);
});
ui/snippets/searchBar/SearchBar.tsx
View file @
c7cb8410
...
...
@@ -21,7 +21,7 @@ const SearchBar = ({ isHomepage }: Props) => {
const
menuWidth
=
React
.
useRef
<
number
>
(
0
);
const
isMobile
=
useIsMobile
();
const
{
searchTerm
,
handleSearchTermChange
,
query
}
=
useSearchQuery
();
const
{
searchTerm
,
handleSearchTermChange
,
query
,
redirectCheckQuery
}
=
useSearchQuery
();
const
handleSubmit
=
React
.
useCallback
((
event
:
FormEvent
<
HTMLFormElement
>
)
=>
{
event
.
preventDefault
();
...
...
@@ -98,7 +98,7 @@ const SearchBar = ({ isHomepage }: Props) => {
</
PopoverTrigger
>
<
PopoverContent
w=
{
`${ menuWidth.current }px`
}
maxH=
{
{
base
:
'
300px
'
,
lg
:
'
500px
'
}
}
overflowY=
"scroll"
ref=
{
menuRef
}
>
<
PopoverBody
py=
{
6
}
>
<
SearchBarSuggest
query=
{
query
}
searchTerm=
{
searchTerm
}
/>
<
SearchBarSuggest
query=
{
query
}
redirectCheckQuery=
{
redirectCheckQuery
}
searchTerm=
{
searchTerm
}
/>
</
PopoverBody
>
</
PopoverContent
>
</
Popover
>
...
...
ui/snippets/searchBar/SearchBarSuggest.tsx
View file @
c7cb8410
import
{
Text
}
from
'
@chakra-ui/react
'
;
import
type
{
UseQueryResult
}
from
'
@tanstack/react-query
'
;
import
_uniqBy
from
'
lodash/uniqBy
'
;
import
React
from
'
react
'
;
import
type
{
SearchRe
sult
}
from
'
types/api/search
'
;
import
type
{
SearchRe
directResult
,
SearchResult
,
SearchResultItem
}
from
'
types/api/search
'
;
import
useIsMobile
from
'
lib/hooks/useIsMobile
'
;
import
TextAd
from
'
ui/shared/ad/TextAd
'
;
...
...
@@ -11,32 +12,84 @@ import type { Props as PaginationProps } from 'ui/shared/Pagination';
import
SearchBarSuggestItem
from
'
./SearchBarSuggestItem
'
;
const
getUniqueIdentifier
=
(
item
:
SearchResultItem
)
=>
{
switch
(
item
.
type
)
{
case
'
contract
'
:
case
'
address
'
:
{
return
item
.
address
;
}
case
'
transaction
'
:
{
return
item
.
tx_hash
;
}
case
'
block
'
:
{
return
item
.
block_hash
||
item
.
block_number
;
}
case
'
token
'
:
{
return
item
.
address
;
}
}
};
interface
Props
{
query
:
UseQueryResult
<
SearchResult
>
&
{
pagination
:
PaginationProps
;
};
redirectCheckQuery
:
UseQueryResult
<
SearchRedirectResult
>
;
searchTerm
:
string
;
}
const
SearchBarSuggest
=
({
query
,
searchTerm
}:
Props
)
=>
{
const
SearchBarSuggest
=
({
query
,
redirectCheckQuery
,
searchTerm
}:
Props
)
=>
{
const
isMobile
=
useIsMobile
();
const
simpleMatch
:
SearchResultItem
|
undefined
=
React
.
useMemo
(()
=>
{
if
(
!
redirectCheckQuery
.
data
||
!
redirectCheckQuery
.
data
.
redirect
||
!
redirectCheckQuery
.
data
.
parameter
)
{
return
;
}
switch
(
redirectCheckQuery
.
data
?.
type
)
{
case
'
address
'
:
{
return
{
type
:
'
address
'
,
name
:
''
,
address
:
redirectCheckQuery
.
data
.
parameter
,
};
}
case
'
transaction
'
:
{
return
{
type
:
'
transaction
'
,
tx_hash
:
redirectCheckQuery
.
data
.
parameter
,
};
}
}
},
[
redirectCheckQuery
.
data
]);
const
items
=
React
.
useMemo
(()
=>
{
return
_uniqBy
(
[
simpleMatch
,
...(
query
.
data
?.
items
||
[]),
].
filter
(
Boolean
),
getUniqueIdentifier
,
);
},
[
query
.
data
?.
items
,
simpleMatch
]);
const
content
=
(()
=>
{
if
(
query
.
isLoading
)
{
return
<
ContentLoader
text=
"We are searching, please wait... "
/>;
if
(
query
.
isLoading
&&
!
simpleMatch
)
{
return
<
ContentLoader
text=
"We are searching, please wait... "
fontSize=
"sm"
/>;
}
if
(
query
.
isError
)
{
if
(
query
.
isError
&&
!
simpleMatch
)
{
return
<
Text
>
Something went wrong. Try refreshing the page or come back later.
</
Text
>;
}
const
num
=
query
.
data
.
next_page_params
?
'
50+
'
:
query
.
data
.
items
.
length
;
const
resultText
=
query
.
data
.
items
.
length
>
1
||
query
.
pagination
.
page
>
1
?
'
results
'
:
'
result
'
;
const
num
=
query
.
data
?.
next_page_params
?
'
50+
'
:
items
.
length
;
const
resultText
=
items
.
length
>
1
||
query
.
pagination
.
page
>
1
?
'
results
'
:
'
result
'
;
return
(
<>
<
Text
fontWeight=
{
500
}
fontSize=
"sm"
>
Found
<
Text
fontWeight=
{
700
}
as=
"span"
>
{
num
}
</
Text
>
matching
{
resultText
}
</
Text
>
{
query
.
data
.
items
.
map
((
item
,
index
)
=>
<
SearchBarSuggestItem
key=
{
index
}
data=
{
item
}
isMobile=
{
isMobile
}
searchTerm=
{
searchTerm
}
/>)
}
{
items
.
map
((
item
,
index
)
=>
<
SearchBarSuggestItem
key=
{
index
}
data=
{
item
}
isMobile=
{
isMobile
}
searchTerm=
{
searchTerm
}
/>)
}
{
query
.
isLoading
&&
<
ContentLoader
text=
"We are still searching, please wait... "
fontSize=
"sm"
mt=
{
5
}
/>
}
</>
);
})();
...
...
ui/snippets/searchBar/useSearchQuery.tsx
View file @
c7cb8410
...
...
@@ -24,7 +24,10 @@ export default function useSearchQuery(isSearchPage = false) {
const
redirectCheckQuery
=
useApiQuery
(
'
search_check_redirect
'
,
{
queryParams
:
{
q
:
q
.
current
},
queryOptions
:
{
enabled
:
isSearchPage
&&
Boolean
(
q
)
},
// on search result page we check redirect only once on mount
// on pages with regular search bar we check redirect on every search term change
// in order to prepend its result to suggest list since this resource is much faster than regular search
queryOptions
:
{
enabled
:
Boolean
(
isSearchPage
?
q
:
debouncedSearchTerm
)
},
});
useUpdateValueEffect
(()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment