-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
176 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/widget/src/components/TokenList/VirtualizedTokenList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { TokenAmount } from '@lifi/sdk'; | ||
import { List } from '@mui/material'; | ||
import { useVirtualizer } from '@tanstack/react-virtual'; | ||
import { FC, useEffect } from 'react'; | ||
import { TokenListItem, TokenListItemSkeleton } from './TokenListItem'; | ||
import { VirtualizedTokenListProps } from './types'; | ||
import { skeletonKey } from './utils'; | ||
|
||
export const VirtualizedTokenList: FC<VirtualizedTokenListProps> = ({ | ||
tokens, | ||
scrollElementRef, | ||
onClick, | ||
chainId, | ||
isBalanceLoading, | ||
showBalance, | ||
}) => { | ||
const { getVirtualItems, getTotalSize, scrollToIndex } = useVirtualizer({ | ||
count: tokens.length, | ||
getScrollElement: () => scrollElementRef.current, | ||
overscan: 3, | ||
paddingEnd: 12, | ||
estimateSize: () => 64, | ||
getItemKey: (index) => tokens[index].address ?? index, | ||
}); | ||
|
||
useEffect(() => { | ||
scrollToIndex(0, { align: 'start', smoothScroll: false }); | ||
}, [scrollToIndex, chainId]); | ||
|
||
return ( | ||
<List style={{ height: getTotalSize() }} disablePadding> | ||
{getVirtualItems().map((item) => { | ||
const token = tokens[item.index] as TokenAmount; | ||
if (token.name.includes(skeletonKey)) { | ||
return ( | ||
<TokenListItemSkeleton | ||
key={item.key} | ||
size={item.size} | ||
start={item.start} | ||
/> | ||
); | ||
} | ||
return ( | ||
<TokenListItem | ||
key={item.key} | ||
onClick={onClick} | ||
size={item.size} | ||
start={item.start} | ||
token={token} | ||
isBalanceLoading={isBalanceLoading} | ||
showBalance={showBalance} | ||
/> | ||
); | ||
})} | ||
</List> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ChainId, Token } from '@lifi/sdk'; | ||
import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { LiFi } from '../config/lifi'; | ||
|
||
export const useTokenSearch = ( | ||
token: string, | ||
chainId: number, | ||
enabled?: boolean, | ||
) => { | ||
const queryClient = useQueryClient(); | ||
const { data, isLoading, isFetching, isFetched } = useQuery( | ||
['token-search', chainId, token], | ||
async ({ queryKey: [, chainId, token], signal }) => { | ||
const data = await LiFi.getToken(chainId as ChainId, token as string, { | ||
signal, | ||
}); | ||
if (data) { | ||
queryClient.setQueryData(['tokens', chainId], (tokens?: Token[]) => { | ||
if (!tokens?.some((token) => token.address === data.address)) { | ||
tokens?.push(data); | ||
} | ||
return tokens; | ||
}); | ||
} | ||
return data; | ||
}, | ||
{ | ||
enabled, | ||
retry: false, | ||
}, | ||
); | ||
return { | ||
token: data, | ||
isLoading, | ||
isFetching, | ||
isFetched, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.