|
1 | 1 | import { useCallback } from 'react'; |
2 | | -import { Address } from 'viem'; |
3 | 2 |
|
| 3 | +import { useModals } from 'src/domains/misc/components/Modal'; |
4 | 4 | import TransactionDetailsModal from 'src/domains/shielder/components/TransactionDetailsModal'; |
5 | | - |
6 | | -import { useModal } from '../../misc/components/Modal'; |
7 | | - |
8 | | -import { useTransactionsHistory } from './useTransactionsHistory'; |
9 | | - |
10 | | -type TransactionIdentifier = { |
11 | | - txHash: Address, |
12 | | -} | { |
13 | | - localId: string, |
14 | | -}; |
| 5 | +import { PartialLocalShielderActivityHistory } from 'src/domains/shielder/stores/getShielderIndexedDB'; |
| 6 | +import { useTransactionsHistory } from 'src/domains/shielder/utils/useTransactionsHistory.tsx'; |
15 | 7 |
|
16 | 8 | /** |
17 | 9 | * Custom hook to handle transaction details modal logic. |
18 | | - * Prevents duplicate modals by using consistent modal IDs and handles |
19 | | - * the transition from localId to txHash when transactions are confirmed. |
| 10 | + * Fetches the latest transaction data and opens a modal with up-to-date information. |
| 11 | + * Handles modal ID transitions when transactions move from pending (localId only) |
| 12 | + * to confirmed state (both localId and txHash), preventing duplicate modals. |
20 | 13 | */ |
21 | | -export const useTransactionDetailsModal = () => { |
22 | | - const { open } = useModal(); |
23 | | - const { data: transactions } = useTransactionsHistory(); |
24 | | - |
25 | | - const openTransactionModal = useCallback((identifier: TransactionIdentifier) => { |
26 | | - // Find the transaction to determine the best modal ID |
27 | | - const transaction = transactions?.find(tx => { |
28 | | - if ('txHash' in identifier && tx.txHash === identifier.txHash) return true; |
29 | | - if ('localId' in identifier && tx.localId === identifier.localId) return true; |
30 | | - return false; |
31 | | - }); |
| 14 | +const useTransactionDetailsModal = () => { |
| 15 | + const { mount, updateId } = useModals(); |
| 16 | + const { refetch } = useTransactionsHistory(); |
| 17 | + |
| 18 | + const openTransactionModal = useCallback(async (tx: PartialLocalShielderActivityHistory) => { |
| 19 | + const { data } = await refetch(); |
| 20 | + const thisTransaction = |
| 21 | + data?.find(transaction => transaction.txHash === tx.txHash || transaction.localId === tx.localId); |
32 | 22 |
|
33 | | - // Determine the modal ID priority: txHash > localId |
34 | | - // This ensures we don't create duplicate modals when a transaction |
35 | | - // transitions from pending (localId only) to confirmed (has txHash) |
36 | | - const modalId = transaction?.txHash ?? ('localId' in identifier ? identifier.localId : identifier.txHash); |
37 | | - |
38 | | - // Determine the props to pass to the modal |
39 | | - const modalProps: TransactionIdentifier = transaction?.txHash ? |
40 | | - { txHash: transaction.txHash } : |
41 | | - identifier; |
42 | | - |
43 | | - open( |
44 | | - <TransactionDetailsModal {...modalProps} />, |
45 | | - { idOverride: modalId } |
46 | | - ); |
47 | | - }, [open, transactions]); |
| 23 | + const modalId = tx.txHash ?? tx.localId; |
| 24 | + |
| 25 | + const modalProps = tx.txHash ? |
| 26 | + { txHash: tx.txHash } : |
| 27 | + { localId: tx.localId }; |
| 28 | + |
| 29 | + const updatedModal = thisTransaction?.localId && thisTransaction.txHash ? |
| 30 | + await updateId(thisTransaction.localId, thisTransaction.txHash) : |
| 31 | + null; |
| 32 | + |
| 33 | + if(updatedModal) { |
| 34 | + console.warn('Transaction modal is already open, updating to new ID:', updatedModal.id); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + mount({ |
| 39 | + id: modalId, |
| 40 | + modal: <TransactionDetailsModal {...modalProps} />, |
| 41 | + }); |
| 42 | + }, [mount, refetch, updateId]); |
48 | 43 |
|
49 | 44 | return { |
50 | 45 | openTransactionModal, |
|
0 commit comments