@@ -11,6 +11,7 @@ import { DisplayAlgo } from '@/features/common/components/display-algo'
1111import { DescriptionList } from '@/features/common/components/description-list'
1212import { ellipseAddress } from '@/utils/ellipse-address'
1313import { transactionPageConstants } from '@/features/theme/constant'
14+ import { flattenInnerTransactions } from '@/utils/flatten-inner-transactions'
1415
1516const graphConfig = {
1617 rowHeight : 40 ,
@@ -262,38 +263,6 @@ function TransactionRow({
262263 )
263264}
264265
265- function unpackTransaction ( transaction : TransactionModel , nestingLevel = 0 ) {
266- let transactionCount = 1
267- let accounts : string [ ] = [ ]
268- let maxNestingLevel = nestingLevel
269-
270- accounts . push ( transaction . sender )
271- if ( transaction . type === TransactionType . Payment ) {
272- accounts . push ( transaction . receiver )
273- }
274-
275- transaction . transactions ?. forEach ( ( transaction ) => {
276- const {
277- transactionCount : childTransactionCount ,
278- accounts : childAccounts ,
279- nestingLevel : childNestingLevel ,
280- } = unpackTransaction ( transaction , nestingLevel + 1 )
281-
282- transactionCount += childTransactionCount
283- accounts = [ ...accounts , ...childAccounts ]
284- maxNestingLevel = Math . max ( maxNestingLevel , childNestingLevel )
285- } )
286-
287- // Remove duplicates
288- accounts = Array . from ( new Set ( accounts ) )
289-
290- return {
291- transactionCount : transactionCount ,
292- accounts : accounts ,
293- nestingLevel : maxNestingLevel ,
294- }
295- }
296-
297266function calcArrow ( transaction : TransactionModel , accounts : string [ ] ) : Arrow {
298267 const fromAccount = accounts . findIndex ( ( a ) => transaction . sender === a )
299268
@@ -320,13 +289,23 @@ type Props = {
320289}
321290
322291export function TransactionViewVisual ( { transaction } : Props ) {
323- const { transactionCount, accounts, nestingLevel } = unpackTransaction ( transaction )
292+ const flattenedTransactions = useMemo ( ( ) => flattenInnerTransactions ( transaction ) , [ transaction ] )
293+ const transactionCount = flattenedTransactions . length
294+ const accounts = Array . from (
295+ new Set ( [
296+ ...flattenedTransactions
297+ . map ( ( t ) => [ t . transaction . sender , t . transaction . type === TransactionType . Payment ? t . transaction . receiver : undefined ] )
298+ . flat ( )
299+ . filter ( isDefined ) ,
300+ ] )
301+ )
302+ const maxNestingLevel = Math . max ( ...flattenedTransactions . map ( ( t ) => t . nestingLevel ) )
324303
325304 return (
326305 < div
327306 className = { cn ( 'relative grid' ) }
328307 style = { {
329- gridTemplateColumns : `minmax(${ graphConfig . colWidth } px, ${ graphConfig . colWidth + nestingLevel * graphConfig . indentationWidth } px) repeat(${ accounts . length } , ${ graphConfig . colWidth } px)` ,
308+ gridTemplateColumns : `minmax(${ graphConfig . colWidth } px, ${ graphConfig . colWidth + maxNestingLevel * graphConfig . indentationWidth } px) repeat(${ accounts . length } , ${ graphConfig . colWidth } px)` ,
330309 gridTemplateRows : `repeat(${ transactionCount + 1 } , ${ graphConfig . rowHeight } px)` ,
331310 } }
332311 >
0 commit comments