Commit a75e239f authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: keep search history synced with trending (#4854)

parent a663482d
......@@ -106,7 +106,7 @@ interface SearchBarDropdownProps {
export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput, isLoading }: SearchBarDropdownProps) => {
const [hoveredIndex, setHoveredIndex] = useState<number | undefined>(0)
const searchHistory = useSearchHistory((state: { history: (FungibleToken | GenieCollection)[] }) => state.history)
const { history: searchHistory, updateItem: updateSearchHistory } = useSearchHistory()
const shortenedHistory = useMemo(() => searchHistory.slice(0, 2), [searchHistory])
const { pathname } = useLocation()
const isNFTPage = pathname.includes('/nfts')
......@@ -146,9 +146,11 @@ export const SearchBarDropdown = ({ toggleOpen, tokens, collections, hasInput, i
refetchOnReconnect: false,
}
)
useEffect(() => {
trendingTokenResults?.forEach(updateSearchHistory)
}, [trendingTokenResults, updateSearchHistory])
const trendingTokensLength = phase1Flag === NftVariant.Enabled ? (isTokenPage ? 3 : 2) : 4
const trendingTokens = useMemo(
() =>
trendingTokenResults
......
......@@ -5,6 +5,7 @@ import { devtools, persist } from 'zustand/middleware'
interface SearchHistoryProps {
history: (FungibleToken | GenieCollection)[]
addItem: (item: FungibleToken | GenieCollection) => void
updateItem: (update: FungibleToken | GenieCollection) => void
}
export const useSearchHistory = create<SearchHistoryProps>()(
......@@ -18,6 +19,16 @@ export const useSearchHistory = create<SearchHistoryProps>()(
return { history: historyCopy }
})
},
updateItem: (update: FungibleToken | GenieCollection) => {
set(({ history }) => {
const index = history.findIndex((item) => item.address === update.address)
if (index === -1) return { history }
const historyCopy = [...history]
historyCopy[index] = update
return { history: historyCopy }
})
},
})),
{ name: 'useSearchHistory' }
)
......
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