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

List performance #675

Merged
merged 5 commits into from
Jun 21, 2023
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
6 changes: 2 additions & 4 deletions src/entries/popup/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ function Tab({
display="flex"
flexDirection="column"
justifyContent="center"
style={{
cursor: 'default',
}}
onClick={onClick}
style={{ cursor: 'default' }}
>
{/* TODO: Convert to <Row> */}
<Box
Expand All @@ -42,7 +41,6 @@ function Tab({
alignItems="center"
style={{ flex: 1, outlineColor: accentColorAsHsl, borderRadius: 6 }}
className="home-tab-wrapper"
onClick={onClick}
tabIndex={tabIndex}
>
<Inset horizontal="2px">
Expand Down
39 changes: 17 additions & 22 deletions src/entries/popup/hooks/useAllTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Address, useNetwork } from 'wagmi';

import { SupportedCurrencyKey } from '~/core/references';
import { shortcuts } from '~/core/references/shortcuts';
import { selectTransactionsByDate } from '~/core/resources/_selectors';
import { useTransactions } from '~/core/resources/transactions/transactions';
import {
currentAddressStore,
Expand Down Expand Up @@ -131,24 +130,6 @@ export function useAllTransactions({
const pendingTransactions: RainbowTransaction[] = getPendingTransactions({
address,
});
const allTransactions = useMemo(
() => [
...pendingTransactions,
...(confirmedTransactions || []),
...(confirmedArbitrumTransactions || []),
...(confirmedBscTransactions || []),
...(confirmedOptimismTransactions || []),
...(confirmedPolygonTransactions || []),
],
[
confirmedArbitrumTransactions,
confirmedBscTransactions,
confirmedOptimismTransactions,
confirmedPolygonTransactions,
confirmedTransactions,
pendingTransactions,
],
);

const isInitialLoading =
confirmedInitialLoading ||
Expand All @@ -160,11 +141,25 @@ export function useAllTransactions({

return useMemo(
() => ({
allTransactions,
allTransactionsByDate: selectTransactionsByDate(allTransactions),
allTransactions: [
...pendingTransactions,
...(confirmedTransactions || []),
...(confirmedArbitrumTransactions || []),
...(confirmedBscTransactions || []),
...(confirmedOptimismTransactions || []),
...(confirmedPolygonTransactions || []),
],
isInitialLoading,
}),
[allTransactions, isInitialLoading],
[
confirmedArbitrumTransactions,
confirmedBscTransactions,
confirmedOptimismTransactions,
confirmedPolygonTransactions,
confirmedTransactions,
pendingTransactions,
isInitialLoading,
],
);
}

Expand Down
51 changes: 14 additions & 37 deletions src/entries/popup/pages/home/Activity.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { useVirtualizer } from '@tanstack/react-virtual';
import React, {
ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { useWindowVirtualizer } from '@tanstack/react-virtual';
import React, { ReactNode, useMemo, useRef } from 'react';
import { useAccount } from 'wagmi';

import { i18n } from '~/core/languages';
import { selectTransactionsByDate } from '~/core/resources/_selectors';
import { useCurrentCurrencyStore } from '~/core/state';
import {
RainbowTransaction,
Expand All @@ -36,44 +30,27 @@ import { Spinner } from '../../components/Spinner/Spinner';
import { useActivityShortcuts } from '../../hooks/useActivityShortcuts';
import { useAllTransactions } from '../../hooks/useAllTransactions';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { TransactionDetailsMenu } from './TransactionDetailsMenu';

const ACTIVITY_DEFAULT_LENGTH = 100;

export function Activity() {
const { address } = useAccount();
const { currentCurrency: currency } = useCurrentCurrencyStore();
const { allTransactionsByDate, isInitialLoading } = useAllTransactions({
const { allTransactions, isInitialLoading } = useAllTransactions({
address,
currency,
});
const [activityLength, setActivityLength] = useState(ACTIVITY_DEFAULT_LENGTH);
const listData = useMemo(() => {
return Object.keys(allTransactionsByDate).reduce((listData, dateKey) => {
return [...listData, dateKey, ...allTransactionsByDate[dateKey]];
}, [] as (string | RainbowTransaction)[]);
}, [allTransactionsByDate]).slice(0, activityLength);

const listData = useMemo(
() => Object.entries(selectTransactionsByDate(allTransactions)).flat(2),
[allTransactions],
);

const containerRef = useRef<HTMLDivElement>(null);
const activityRowVirtualizer = useVirtualizer({
const activityRowVirtualizer = useWindowVirtualizer({
count: listData.length,
getScrollElement: () => containerRef.current,
estimateSize: (i) => (typeof listData[i] === 'string' ? 34 : 52),
overscan: 20,
});
const scrollEndListener = useCallback(() => {
if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
setActivityLength(activityLength + ACTIVITY_DEFAULT_LENGTH);
}
}, [activityLength]);

useEffect(() => {
window.removeEventListener('scroll', scrollEndListener);
window.addEventListener('scroll', scrollEndListener);
return () => {
window.removeEventListener('scroll', scrollEndListener);
};
}, [scrollEndListener]);

useActivityShortcuts();

Expand Down Expand Up @@ -135,18 +112,18 @@ export function Activity() {
}}
>
{activityRowVirtualizer.getVirtualItems().map((virtualItem) => {
const { index, start } = virtualItem;
const { index, key, start, size } = virtualItem;
const rowData = listData[index];
return (
<Box
key={index}
key={key}
data-index={index}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: typeof rowData === 'string' ? 34 : 52,
height: size,
transform: `translateY(${start}px)`,
}}
>
Expand Down
19 changes: 8 additions & 11 deletions src/entries/popup/pages/home/Tokens.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useWindowVirtualizer } from '@tanstack/react-virtual';
import { useMemo, useState } from 'react';

import { i18n } from '~/core/languages';
Expand Down Expand Up @@ -28,7 +29,6 @@ import { CoinbaseIcon } from '../../components/CoinbaseIcon/CoinbaseIcon';
import { WalletIcon } from '../../components/WalletIcon/WalletIcon';
import { useKeyboardShortcut } from '../../hooks/useKeyboardShortcut';
import { useTokensShortcuts } from '../../hooks/useTokensShortcuts';
import { useVirtualizedAssets } from '../../hooks/useVirtualizedAssets';

import { TokenDetailsMenu } from './TokenDetailsMenu';

Expand All @@ -46,8 +46,10 @@ export function Tokens() {
{ address: currentAddress, currency, connectedToHardhat },
{ select: selectUserAssetsList },
);
const { containerRef, assetsRowVirtualizer } = useVirtualizedAssets({
assets,
const assetsRowVirtualizer = useWindowVirtualizer({
count: assets?.length || 0,
estimateSize: () => 52,
overscan: 20,
});

useKeyboardShortcut({
Expand All @@ -73,7 +75,6 @@ export function Tokens() {

return (
<Box
ref={containerRef}
width="full"
style={{
overflow: 'auto',
Expand All @@ -91,17 +92,13 @@ export function Tokens() {
position: 'relative',
}}
>
<Box
style={{
overflow: 'auto',
}}
>
<Box style={{ overflow: 'auto' }}>
{assetsRowVirtualizer.getVirtualItems().map((virtualItem) => {
const { index } = virtualItem;
const { key, index } = virtualItem;
const rowData = assets[index];
return (
<Box
key={index}
key={key}
style={{
position: 'absolute',
top: 0,
Expand Down
12 changes: 10 additions & 2 deletions src/entries/popup/pages/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
useSpring,
useTransform,
} from 'framer-motion';
import { PropsWithChildren, useCallback, useEffect, useState } from 'react';
import {
PropsWithChildren,
useCallback,
useEffect,
useState,
useTransition,
} from 'react';
import { useLocation } from 'react-router-dom';
import { useAccount } from 'wagmi';

Expand Down Expand Up @@ -66,13 +72,15 @@ export function Home() {

const [activeTab, setActiveTab] = useState<Tab>(state?.activeTab || 'tokens');

const [, startTransition] = useTransition();

const onSelectTab = useCallback((tab: Tab) => {
// If we are already in a state where the header is collapsed,
// then ensure we are scrolling to the top when we change tab.
if (window.scrollY > COLLAPSED_HEADER_TOP_OFFSET) {
window.scrollTo({ top: COLLAPSED_HEADER_TOP_OFFSET });
}
setActiveTab(tab);
startTransition(() => setActiveTab(tab));
}, []);

useEffect(() => {
Expand Down