Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
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
LuckySwap
interface
Commits
744db498
Unverified
Commit
744db498
authored
May 12, 2021
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
do not show duplicate token results, and stop searching as soon as possible
parent
54f59e02
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
19 deletions
+30
-19
filtering.ts
src/components/SearchModal/filtering.ts
+14
-10
Tokens.ts
src/hooks/Tokens.ts
+16
-9
No files found.
src/components/SearchModal/filtering.ts
View file @
744db498
...
...
@@ -3,13 +3,18 @@ import { useMemo } from 'react'
import
{
isAddress
}
from
'
../../utils
'
import
{
Token
}
from
'
@uniswap/sdk-core
'
export
function
filterTokens
<
T
extends
Token
|
TokenInfo
>
(
tokens
:
T
[],
search
:
string
):
T
[]
{
if
(
search
.
length
===
0
)
return
tokens
const
alwaysTrue
=
()
=>
true
/**
* Create a filter function to apply to a token for whether it matches a particular search query
* @param search the search query to apply to the token
*/
export
function
createTokenFilterFunction
<
T
extends
Token
|
TokenInfo
>
(
search
:
string
):
(
tokens
:
T
)
=>
boolean
{
const
searchingAddress
=
isAddress
(
search
)
if
(
searchingAddress
)
{
return
tokens
.
filter
((
token
)
=>
token
.
address
===
searchingAddress
)
const
lower
=
searchingAddress
.
toLowerCase
()
return
(
t
:
T
)
=>
(
'
isToken
'
in
t
?
searchingAddress
===
t
.
address
:
lower
===
t
.
address
.
toLowerCase
())
}
const
lowerSearchParts
=
search
...
...
@@ -17,9 +22,7 @@ export function filterTokens<T extends Token | TokenInfo>(tokens: T[], search: s
.
split
(
/
\s
+/
)
.
filter
((
s
)
=>
s
.
length
>
0
)
if
(
lowerSearchParts
.
length
===
0
)
{
return
tokens
}
if
(
lowerSearchParts
.
length
===
0
)
return
alwaysTrue
const
matchesSearch
=
(
s
:
string
):
boolean
=>
{
const
sParts
=
s
...
...
@@ -30,10 +33,11 @@ export function filterTokens<T extends Token | TokenInfo>(tokens: T[], search: s
return
lowerSearchParts
.
every
((
p
)
=>
p
.
length
===
0
||
sParts
.
some
((
sp
)
=>
sp
.
startsWith
(
p
)
||
sp
.
endsWith
(
p
)))
}
return
tokens
.
filter
((
token
)
=>
{
const
{
symbol
,
name
}
=
token
return
(
symbol
&&
matchesSearch
(
symbol
))
||
(
name
&&
matchesSearch
(
name
))
})
return
({
name
,
symbol
}:
T
):
boolean
=>
Boolean
((
symbol
&&
matchesSearch
(
symbol
))
||
(
name
&&
matchesSearch
(
name
)))
}
export
function
filterTokens
<
T
extends
Token
|
TokenInfo
>
(
tokens
:
T
[],
search
:
string
):
T
[]
{
return
tokens
.
filter
(
createTokenFilterFunction
(
search
))
}
export
function
useSortedTokensByQuery
(
tokens
:
Token
[]
|
undefined
,
searchQuery
:
string
):
Token
[]
{
...
...
src/hooks/Tokens.ts
View file @
744db498
...
...
@@ -2,7 +2,7 @@ import { parseBytes32String } from '@ethersproject/strings'
import
{
Currency
,
currencyEquals
,
ETHER
,
Token
}
from
'
@uniswap/sdk-core
'
import
{
arrayify
}
from
'
ethers/lib/utils
'
import
{
useMemo
}
from
'
react
'
import
{
filterTokens
}
from
'
../components/SearchModal/filtering
'
import
{
createTokenFilterFunction
}
from
'
../components/SearchModal/filtering
'
import
{
useAllLists
,
useCombinedActiveList
,
useInactiveListUrls
}
from
'
../state/lists/hooks
'
import
{
WrappedTokenInfo
}
from
'
../state/lists/wrappedTokenInfo
'
import
{
NEVER_RELOAD
,
useSingleCallResult
}
from
'
../state/multicall/hooks
'
...
...
@@ -61,21 +61,28 @@ export function useSearchInactiveTokenLists(search: string | undefined, minResul
const
lists
=
useAllLists
()
const
inactiveUrls
=
useInactiveListUrls
()
const
{
chainId
}
=
useActiveWeb3React
()
const
activeTokens
=
useAllTokens
()
return
useMemo
(()
=>
{
if
(
!
search
||
search
.
trim
().
length
===
0
)
return
[]
let
result
:
WrappedTokenInfo
[]
=
[]
const
tokenFilter
=
createTokenFilterFunction
(
search
)
const
result
:
WrappedTokenInfo
[]
=
[]
const
addressSet
:
{
[
address
:
string
]:
true
}
=
{}
for
(
const
url
of
inactiveUrls
)
{
const
list
=
lists
[
url
].
current
if
(
!
list
)
continue
const
matching
=
filterTokens
(
list
.
tokens
.
filter
((
token
)
=>
token
.
chainId
===
chainId
),
search
)
result
=
[...
result
,
...
matching
.
map
((
tokenInfo
)
=>
new
WrappedTokenInfo
(
tokenInfo
,
list
))]
if
(
result
.
length
>=
minResults
)
return
result
for
(
const
tokenInfo
of
list
.
tokens
)
{
if
(
tokenInfo
.
chainId
===
chainId
&&
tokenFilter
(
tokenInfo
))
{
const
wrapped
=
new
WrappedTokenInfo
(
tokenInfo
,
list
)
if
(
!
(
wrapped
.
address
in
activeTokens
)
&&
!
addressSet
[
wrapped
.
address
])
{
addressSet
[
wrapped
.
address
]
=
true
result
.
push
(
wrapped
)
if
(
result
.
length
>=
minResults
)
return
result
}
}
}
}
return
result
},
[
chainId
,
inactiveUrls
,
lists
,
minResults
,
search
])
},
[
activeTokens
,
chainId
,
inactiveUrls
,
lists
,
minResults
,
search
])
}
export
function
useIsTokenActive
(
token
:
Token
|
undefined
|
null
):
boolean
{
...
...
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