Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow users to search supported networks by contract addresses #626

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/SupportedNetworks/Networks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ const SupportedNetworks: React.FC = () => {
<TextField
className={css.searchField}
variant='outlined'
placeholder='Search by network name or chain ID'
placeholder='Search by network name, chain ID, or contract address'
InputProps={{
style: {
color: 'white',
Expand Down
36 changes: 11 additions & 25 deletions components/SupportedNetworks/useNetworksSearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useMemo } from 'react'
import Fuse from 'fuse.js'
import { type Network } from './Networks'

export const useNetworksSearch = (
Expand All @@ -13,31 +12,18 @@ export const useNetworksSearch = (
}),
[resources]
)
const fuse = useMemo(
() =>
new Fuse(sortedByChainId, {
keys: [
{
name: 'name',
weight: 0.99
},
{
name: 'chainId',
weight: 0.5
}
],
distance: 500,
threshold: 0.3,
findAllMatches: true
}),
[sortedByChainId]
)

return useMemo(() => {
if (query.length === 0) {
return sortedByChainId
}
if (query.length === 0) return sortedByChainId

return fuse.search(query).map(result => result.item)
}, [fuse, sortedByChainId, query])
const lowercaseQuery = query.toLowerCase()
return sortedByChainId.filter(
network =>
network.name.toLowerCase().includes(lowercaseQuery) ||
network.chainId.toString().includes(lowercaseQuery) ||
network.smartAccounts?.some(account =>
account.address?.toLowerCase().includes(lowercaseQuery)
)
)
}, [sortedByChainId, query])
}
Loading