Skip to content

Commit

Permalink
perf: (transaction-row): Don't create new element for each render
Browse files Browse the repository at this point in the history
  • Loading branch information
memoyil committed Jul 7, 2023
1 parent 09be4f1 commit ddeafa6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
34 changes: 17 additions & 17 deletions apps/web/src/components/Menu/UserMenu/TransactionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TransactionDetails } from 'state/transactions/reducer'
import { pickFarmTransactionTx } from 'state/global/actions'
import { TransactionType, FarmTransactionStatus } from 'state/transactions/actions'
import { getBlockExploreLink } from 'utils'
import { useCallback, useMemo } from 'react'

interface TransactionRowProps {
txn: TransactionDetails
Expand Down Expand Up @@ -38,41 +39,40 @@ const TxnLink = styled.div`
}
`

const renderIcon = (txn: TransactionDetails) => {
const { receipt, nonBscFarm } = txn
if (!txn.receipt || nonBscFarm?.status === FarmTransactionStatus.PENDING) {
return <RefreshIcon spin width="24px" />
}

const isFarmStatusSuccess = nonBscFarm ? nonBscFarm.status === FarmTransactionStatus.SUCCESS : true
return (receipt?.status === 1 && isFarmStatusSuccess) || typeof receipt?.status === 'undefined' ? (
<CheckmarkCircleIcon color="success" width="24px" />
) : (
<BlockIcon color="failure" width="24px" />
)
}

const TransactionRow: React.FC<React.PropsWithChildren<TransactionRowProps>> = ({ txn, chainId, type, onDismiss }) => {
const { t } = useTranslation()
const dispatch = useAppDispatch()
const { receipt, nonBscFarm } = txn || {}
const isFarmStatusSuccess = useMemo(
() => (nonBscFarm ? nonBscFarm.status === FarmTransactionStatus.SUCCESS : true),
[nonBscFarm],
)

const onClickTransaction = () => {
const onClickTransaction = useCallback(() => {
if (type === 'non-bsc-farm') {
onDismiss()
dispatch(pickFarmTransactionTx({ tx: txn.hash, chainId }))
} else {
const url = getBlockExploreLink(txn.hash, 'transaction', chainId)
window.open(url, '_blank', 'noopener noreferrer')
}
}
}, [chainId, dispatch, onDismiss, txn?.hash, type])

if (!txn) {
return null
}

return (
<TxnLink onClick={onClickTransaction}>
<TxnIcon>{renderIcon(txn)}</TxnIcon>
<TxnIcon>
{!txn.receipt || nonBscFarm?.status === FarmTransactionStatus.PENDING ? (
<RefreshIcon spin width="24px" />
) : (receipt?.status === 1 && isFarmStatusSuccess) || typeof receipt?.status === 'undefined' ? (
<CheckmarkCircleIcon color="success" width="24px" />
) : (
<BlockIcon color="failure" width="24px" />
)}
</TxnIcon>
<Summary>
{txn.translatableSummary
? t(txn.translatableSummary.text, txn.translatableSummary.data)
Expand Down
7 changes: 4 additions & 3 deletions apps/web/src/components/Menu/UserMenu/WalletTransactions.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useCallback } from 'react'
import styled from 'styled-components'
import { chains } from 'utils/wagmi'
import { Box, Button, Flex, Text } from '@pancakeswap/uikit'
import { useAppDispatch } from 'state'
import { useAllSortedRecentTransactions } from 'state/transactions/hooks'
import { useTranslation } from '@pancakeswap/localization'
import { clearAllTransactions } from 'state/transactions/actions'
import isEmpty from 'lodash/isEmpty'
import TransactionRow from './TransactionRow'
import { chains } from '../../../utils/wagmi'

const TransactionsContainer = styled(Box)`
max-height: 300px;
Expand All @@ -24,9 +25,9 @@ const WalletTransactions: React.FC<React.PropsWithChildren<WalletTransactionsPro

const hasTransactions = !isEmpty(sortedTransactions)

const handleClearAll = () => {
const handleClearAll = useCallback(() => {
dispatch(clearAllTransactions())
}
}, [dispatch])

return (
<Box minHeight="120px">
Expand Down

0 comments on commit ddeafa6

Please sign in to comment.