import { useMemo } from 'react' ; import type { PublicKey } from '@solana/web3.js' ; import { useTokenAccountsContext } from '../../../tokenAccounts' ; import type { CustomParsedTransaction , Transaction , TransactionDetails , TransactionInfo , } from '../../models' ; import { useParsedTransactionData } from '../useParsedTransactionData' ; const transactionDetails = ( transaction , sources ) : TransactionDetails => { if ( ! transaction ?. data ) { return { type : 'transaction' , icon : 'db' , isReceiver : false } ; } return transaction . data . details ( sources ) ; } ; export const useTransaction = ( key , source ? ) : Transaction < CustomParsedTransaction > | undefined => { const { loading , data } = useParsedTransactionData ( key ) ; const { userTokenAccountKeys } = useTokenAccountsContext ( ) ; const sources = useMemo ( ( ) => { if ( source ) { return [ source ] ; } return userTokenAccountKeys . map ( ( v ) => v . toBase58 ( ) ) ; } , [ source , userTokenAccountKeys ] ) ; return useMemo ( ( ) => { const details = transactionDetails ( data ?. transactionInfo , sources ) ; try { return { key , loading , data : data ? data . transactionInfo . data : null , details , raw : data ?. raw } ; } catch ( e ) { console . warn ( `Error parsing ${ data ?. transactionId . toString ( ) ?? '(unknown)' }` , e ) ; return undefined ; } } , [ data , key , loading , sources ] ) ; } ;