Commit b1b9da1b authored by Charles Bachmeier's avatar Charles Bachmeier Committed by GitHub

feat: enable NFT search results (#4413)

* enable NFT search results

* update usememo deps

* simplify flag trending token logic

* respond to comments

* lowercase bool

* rename flag

* improve truncation
Co-authored-by: default avatarCharlie <charlie@uniswap.org>
parent ffe67092
import { NftVariant, useNftFlag } from 'featureFlags/flags/nft'
import useDebounce from 'hooks/useDebounce' import useDebounce from 'hooks/useDebounce'
import { useOnClickOutside } from 'hooks/useOnClickOutside' import { useOnClickOutside } from 'hooks/useOnClickOutside'
import { useWindowSize } from 'hooks/useWindowSize' import { useWindowSize } from 'hooks/useWindowSize'
...@@ -8,10 +9,11 @@ import { Overlay } from 'nft/components/modals/Overlay' ...@@ -8,10 +9,11 @@ import { Overlay } from 'nft/components/modals/Overlay'
import { subheadSmall } from 'nft/css/common.css' import { subheadSmall } from 'nft/css/common.css'
import { breakpoints } from 'nft/css/sprinkles.css' import { breakpoints } from 'nft/css/sprinkles.css'
import { useSearchHistory } from 'nft/hooks' import { useSearchHistory } from 'nft/hooks'
// import { fetchSearchCollections, fetchTrendingCollections } from 'nft/queries' import { fetchSearchCollections, fetchTrendingCollections } from 'nft/queries'
import { fetchSearchTokens } from 'nft/queries/genie/SearchTokensFetcher' import { fetchSearchTokens } from 'nft/queries/genie/SearchTokensFetcher'
import { fetchTrendingTokens } from 'nft/queries/genie/TrendingTokensFetcher' import { fetchTrendingTokens } from 'nft/queries/genie/TrendingTokensFetcher'
import { FungibleToken, GenieCollection, TrendingCollection } from 'nft/types' import { FungibleToken, GenieCollection, TimePeriod, TrendingCollection } from 'nft/types'
import { formatEthPrice } from 'nft/utils/currency'
import { ChangeEvent, useEffect, useMemo, useReducer, useRef, useState } from 'react' import { ChangeEvent, useEffect, useMemo, useReducer, useRef, useState } from 'react'
import { useQuery } from 'react-query' import { useQuery } from 'react-query'
import { useLocation } from 'react-router-dom' import { useLocation } from 'react-router-dom'
...@@ -94,6 +96,7 @@ export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput }: ...@@ -94,6 +96,7 @@ export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput }:
const { pathname } = useLocation() const { pathname } = useLocation()
const isNFTPage = pathname.includes('/nfts') const isNFTPage = pathname.includes('/nfts')
const isTokenPage = pathname.includes('/tokens') const isTokenPage = pathname.includes('/tokens')
const phase1Flag = useNftFlag()
const tokenSearchResults = const tokenSearchResults =
tokens.length > 0 ? ( tokens.length > 0 ? (
...@@ -110,19 +113,45 @@ export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput }: ...@@ -110,19 +113,45 @@ export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput }:
) )
const collectionSearchResults = const collectionSearchResults =
collections.length > 0 ? ( phase1Flag === NftVariant.Enabled ? (
<SearchBarDropdownSection collections.length > 0 ? (
hoveredIndex={hoveredIndex} <SearchBarDropdownSection
startingIndex={isNFTPage ? 0 : tokens.length} hoveredIndex={hoveredIndex}
setHoveredIndex={setHoveredIndex} startingIndex={isNFTPage ? 0 : tokens.length}
toggleOpen={toggleOpen} setHoveredIndex={setHoveredIndex}
suggestions={collections} toggleOpen={toggleOpen}
header={'NFT Collections'} suggestions={collections}
/> header={'NFT Collections'}
/>
) : (
<Box className={styles.notFoundContainer}>No NFT collections found.</Box>
)
) : null ) : null
// TODO Trending NFT Results implmented here const { data: trendingCollectionResults } = useQuery(['trendingCollections', 'eth', 'twenty_four_hours'], () =>
const trendingCollections = [] as TrendingCollection[] fetchTrendingCollections({ volumeType: 'eth', timePeriod: 'ONE_DAY' as TimePeriod, size: 3 })
)
const trendingCollections = useMemo(() => {
return trendingCollectionResults
?.map((collection) => {
return {
...collection,
collectionAddress: collection.address,
floorPrice: formatEthPrice(collection.floor.toString()),
stats: {
total_supply: collection.totalSupply,
one_day_change: collection.floorChange,
},
}
})
.slice(0, isNFTPage ? 3 : 2)
}, [isNFTPage, trendingCollectionResults])
const showTrendingCollections: boolean = useMemo(
() => (trendingCollections?.length ?? 0) > 0 && !isTokenPage && phase1Flag === NftVariant.Enabled,
[trendingCollections?.length, isTokenPage, phase1Flag]
)
const { data: trendingTokenResults } = useQuery([], () => fetchTrendingTokens(4), { const { data: trendingTokenResults } = useQuery([], () => fetchTrendingTokens(4), {
refetchOnWindowFocus: false, refetchOnWindowFocus: false,
...@@ -130,11 +159,11 @@ export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput }: ...@@ -130,11 +159,11 @@ export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput }:
refetchOnReconnect: false, refetchOnReconnect: false,
}) })
const trendingTokensLength = phase1Flag === NftVariant.Enabled ? (isTokenPage ? 3 : 2) : 4
const trendingTokens = useMemo(() => { const trendingTokens = useMemo(() => {
// TODO reimplement this logic with NFT search return trendingTokenResults?.slice(0, trendingTokensLength)
// return trendingTokenResults?.slice(0, isTokenPage ? 3 : 2) }, [trendingTokenResults, trendingTokensLength])
return trendingTokenResults?.slice(0, 4)
}, [trendingTokenResults])
const totalSuggestions = hasInput const totalSuggestions = hasInput
? tokens.length + collections.length ? tokens.length + collections.length
...@@ -210,14 +239,14 @@ export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput }: ...@@ -210,14 +239,14 @@ export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput }:
headerIcon={<TrendingArrow />} headerIcon={<TrendingArrow />}
/> />
)} )}
{(trendingCollections?.length ?? 0) > 0 && !isTokenPage && ( {showTrendingCollections && (
<SearchBarDropdownSection <SearchBarDropdownSection
hoveredIndex={hoveredIndex} hoveredIndex={hoveredIndex}
startingIndex={searchHistory.length + (isNFTPage ? 0 : trendingTokens?.length ?? 0)} startingIndex={searchHistory.length + (isNFTPage ? 0 : trendingTokens?.length ?? 0)}
setHoveredIndex={setHoveredIndex} setHoveredIndex={setHoveredIndex}
toggleOpen={toggleOpen} toggleOpen={toggleOpen}
suggestions={trendingCollections as unknown as GenieCollection[]} suggestions={trendingCollections as unknown as GenieCollection[]}
header={'Trending NFT collections'} header={'Popular NFT collections'}
headerIcon={<TrendingArrow />} headerIcon={<TrendingArrow />}
/> />
)} )}
...@@ -238,15 +267,22 @@ export const SearchBar = () => { ...@@ -238,15 +267,22 @@ export const SearchBar = () => {
const searchRef = useRef<HTMLDivElement>(null) const searchRef = useRef<HTMLDivElement>(null)
const { pathname } = useLocation() const { pathname } = useLocation()
const { width: windowWidth } = useWindowSize() const { width: windowWidth } = useWindowSize()
const phase1Flag = useNftFlag()
useOnClickOutside(searchRef, () => { useOnClickOutside(searchRef, () => {
isOpen && toggleOpen() isOpen && toggleOpen()
}) })
// TODO NFT Search Results implmented here const { data: collections, isLoading: collectionsAreLoading } = useQuery(
// eslint-disable-next-line react-hooks/exhaustive-deps ['searchCollections', debouncedSearchValue],
const collections = [] as GenieCollection[] () => fetchSearchCollections(debouncedSearchValue),
const collectionsAreLoading = false {
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false,
}
)
const { data: tokens, isLoading: tokensAreLoading } = useQuery( const { data: tokens, isLoading: tokensAreLoading } = useQuery(
['searchTokens', debouncedSearchValue], ['searchTokens', debouncedSearchValue],
() => fetchSearchTokens(debouncedSearchValue), () => fetchSearchTokens(debouncedSearchValue),
...@@ -299,7 +335,7 @@ export const SearchBar = () => { ...@@ -299,7 +335,7 @@ export const SearchBar = () => {
borderTopRightRadius={isOpen && !isMobile ? '12' : undefined} borderTopRightRadius={isOpen && !isMobile ? '12' : undefined}
borderTopLeftRadius={isOpen && !isMobile ? '12' : undefined} borderTopLeftRadius={isOpen && !isMobile ? '12' : undefined}
display={{ mobile: isOpen ? 'flex' : 'none', desktopXl: 'flex' }} display={{ mobile: isOpen ? 'flex' : 'none', desktopXl: 'flex' }}
justifyContent={isOpen ? 'flex-start' : 'center'} justifyContent={isOpen || phase1Flag === NftVariant.Enabled ? 'flex-start' : 'center'}
background={isOpen ? 'white' : 'lightGrayContainer'} background={isOpen ? 'white' : 'lightGrayContainer'}
onFocus={() => !isOpen && toggleOpen()} onFocus={() => !isOpen && toggleOpen()}
onClick={() => !isOpen && toggleOpen()} onClick={() => !isOpen && toggleOpen()}
...@@ -312,8 +348,8 @@ export const SearchBar = () => { ...@@ -312,8 +348,8 @@ export const SearchBar = () => {
</Box> </Box>
<Box <Box
as="input" as="input"
placeholder="Search tokens" placeholder={`Search tokens${phase1Flag === NftVariant.Enabled ? ' and NFT collections' : ''}`}
width={isOpen ? 'full' : '120'} width={isOpen || phase1Flag === NftVariant.Enabled ? 'full' : '120'}
onChange={(event: ChangeEvent<HTMLInputElement>) => { onChange={(event: ChangeEvent<HTMLInputElement>) => {
!isOpen && toggleOpen() !isOpen && toggleOpen()
setSearchValue(event.target.value) setSearchValue(event.target.value)
......
...@@ -58,7 +58,7 @@ export const CollectionRow = ({ collection, isHovered, setHoveredIndex, toggleOp ...@@ -58,7 +58,7 @@ export const CollectionRow = ({ collection, isHovered, setHoveredIndex, toggleOp
className={styles.suggestionRow} className={styles.suggestionRow}
style={{ background: isHovered ? vars.color.lightGrayButton : 'none' }} style={{ background: isHovered ? vars.color.lightGrayButton : 'none' }}
> >
<Row style={{ width: '68%' }}> <Row style={{ width: '60%' }}>
{!brokenImage && collection.imageUrl ? ( {!brokenImage && collection.imageUrl ? (
<Box <Box
as="img" as="img"
...@@ -137,7 +137,7 @@ export const TokenRow = ({ token, isHovered, setHoveredIndex, toggleOpen, index ...@@ -137,7 +137,7 @@ export const TokenRow = ({ token, isHovered, setHoveredIndex, toggleOpen, index
className={styles.suggestionRow} className={styles.suggestionRow}
style={{ background: isHovered ? vars.color.lightGrayButton : 'none' }} style={{ background: isHovered ? vars.color.lightGrayButton : 'none' }}
> >
<Row> <Row style={{ width: '65%' }}>
{!brokenImage && token.logoURI ? ( {!brokenImage && token.logoURI ? (
<Box <Box
as="img" as="img"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment