11import { AddressZero } from '@ethersproject/constants' ;
22import Decimal from 'decimal.js' ;
3+ import { BigNumber } from 'ethers' ;
34import React from 'react' ;
45
56import { ACTIONS_WITH_NETWORK_FEE } from '~constants/actions.ts' ;
6- import { ColonyActionType , type SimpleTarget } from '~gql' ;
7+ import { getNetworkTokenList } from '~constants/tokens/getNetworkTokenList.ts' ;
8+ import { ColonyActionType , type TokenFragment , type SimpleTarget } from '~gql' ;
9+ import useUserByAddress from '~hooks/useUserByAddress.ts' ;
710import FriendlyName from '~shared/FriendlyName/index.ts' ;
811import MaskedAddress from '~shared/MaskedAddress/index.ts' ;
912import Numeral from '~shared/Numeral/index.ts' ;
@@ -16,8 +19,10 @@ import {
1619 type ColonyExtension ,
1720 type Token ,
1821 type Expenditure ,
22+ type ExpenditureStage ,
23+ type ExpenditureSlot ,
1924} from '~types/graphql.ts' ;
20- import { notMaybe } from '~utils/arrays/index.ts' ;
25+ import { notMaybe , notNull } from '~utils/arrays/index.ts' ;
2126import { formatRolesTitle } from '~utils/colonyActions.ts' ;
2227import { getRecipientsNumber , getTokensNumber } from '~utils/expenditures.ts' ;
2328import { getAmountLessFee } from '~utils/getAmountLessFee.ts' ;
@@ -53,8 +58,9 @@ const getDomainNameFromChangelog = (
5358 return changelogItem . newName ;
5459} ;
5560
56- const getRecipientData = (
57- actionData : ColonyAction ,
61+ const useGetRecipientData = (
62+ actionData : ColonyAction | null | undefined ,
63+ expenditure : Expenditure | null | undefined ,
5864) :
5965 | User
6066 | Colony
@@ -70,8 +76,13 @@ const getRecipientData = (
7076 recipientToken,
7177 safeTransaction,
7278 recipientAddress,
73- } = actionData ;
79+ } = actionData || { } ;
7480 const safeRecipient = safeTransaction ?. transactions ?. items [ 0 ] ?. recipient ;
81+ const stagedPaymentRecipientAddress =
82+ expenditure ?. slots [ 0 ] . recipientAddress || '' ;
83+ const stagedPaymentRecipient = useUserByAddress (
84+ stagedPaymentRecipientAddress ,
85+ ) ;
7586
7687 return (
7788 [
@@ -81,12 +92,16 @@ const getRecipientData = (
8192 recipientToken ,
8293 safeRecipient ,
8394 recipientAddress ,
95+ stagedPaymentRecipient . user ,
8496 ] . find ( notMaybe ) || undefined
8597 ) ;
8698} ;
8799
88- const getRecipient = ( actionData : ColonyAction ) => {
89- const recipient = getRecipientData ( actionData ) ;
100+ const useGetRecipient = (
101+ actionData : ColonyAction | null | undefined ,
102+ expenditure : Expenditure | null | undefined ,
103+ ) => {
104+ const recipient = useGetRecipientData ( actionData , expenditure ) ;
90105
91106 return (
92107 < span >
@@ -135,22 +150,76 @@ const getInitiator = (actionData: ColonyAction) => {
135150 ) ;
136151} ;
137152
138- export const mapColonyActionToExpectedFormat = ( {
153+ interface ExpenditureStagesData {
154+ summedAmount : string ;
155+ stagedPaymentToken : TokenFragment | null | undefined ;
156+ }
157+
158+ const getExpenditureStagesData = (
159+ stages : ExpenditureStage [ ] ,
160+ slots : ExpenditureSlot [ ] ,
161+ colony : Pick < Colony , 'tokens' > ,
162+ ) => {
163+ const predefinedTokens = getNetworkTokenList ( ) ;
164+ const colonyTokens = colony . tokens ?. items . filter ( notNull ) || [ ] ;
165+ const allTokens = [ ...colonyTokens , ...predefinedTokens ] ;
166+
167+ const tokensSet = new Set (
168+ slots . flatMap (
169+ ( slot ) => slot . payouts ?. map ( ( payout ) => payout . tokenAddress ) ?? [ ] ,
170+ ) ,
171+ ) ;
172+
173+ const hasSingleTokenType = tokensSet . size === 1 ;
174+
175+ if ( ! hasSingleTokenType ) {
176+ return {
177+ summedAmount : '0' ,
178+ stagedPaymentToken : null ,
179+ } ;
180+ }
181+
182+ const result = stages . reduce < ExpenditureStagesData > (
183+ ( acc , stage ) : ExpenditureStagesData => {
184+ const currentSlot = slots . find ( ( slot ) => slot . id === stage . slotId ) ;
185+ const tokenAddress = currentSlot ?. payouts ?. [ 0 ] ?. tokenAddress ;
186+
187+ const token = allTokens . find (
188+ ( { token : currentToken } ) => currentToken . tokenAddress === tokenAddress ,
189+ ) ;
190+
191+ return {
192+ summedAmount : BigNumber . from ( acc . summedAmount )
193+ . add ( BigNumber . from ( currentSlot ?. payouts ?. [ 0 ] ?. amount || '0' ) )
194+ . toString ( ) ,
195+ stagedPaymentToken : acc . stagedPaymentToken || token ?. token ,
196+ } ;
197+ } ,
198+ {
199+ summedAmount : BigNumber . from ( '0' ) . toString ( ) ,
200+ stagedPaymentToken : null ,
201+ } ,
202+ ) ;
203+
204+ return {
205+ summedAmount : result . summedAmount . toString ( ) ,
206+ stagedPaymentToken : result . stagedPaymentToken ,
207+ } ;
208+ } ;
209+
210+ export const useMapColonyActionToExpectedFormat = ( {
139211 actionData,
140212 colony,
141213 keyFallbackValues = { } ,
142214 expenditureData,
143215 networkInverseFee,
144216} : {
145- actionData : ColonyAction ;
146- colony : Pick < Colony , 'nativeToken' | 'tokens' > ;
217+ actionData : ColonyAction | null | undefined ;
218+ colony : Pick < Colony , 'nativeToken' | 'tokens' > | undefined ;
147219 keyFallbackValues ?: Partial < Record < ActionTitleMessageKeys , React . ReactNode > > ;
148220 expenditureData ?: Expenditure ;
149221 networkInverseFee ?: string ;
150222} ) => {
151- // // @TODO : item.actionType === ColonyMotions.SetUserRolesMotion ? updatedRoles : roles,
152- const formattedRolesTitle = formatRolesTitle ( actionData . roles ) ;
153-
154223 const getFormattedValueWithFallback = (
155224 value : React . ReactNode ,
156225 fallbackKey : ActionTitleMessageKeys ,
@@ -163,6 +232,17 @@ export const mapColonyActionToExpectedFormat = ({
163232 return keyFallbackValues [ fallbackKey ] ;
164233 } ;
165234
235+ const recipient = getFormattedValueWithFallback (
236+ useGetRecipient ( actionData , expenditureData ) ,
237+ ActionTitleMessageKeys . Recipient ,
238+ notMaybe ( useGetRecipientData ( actionData , expenditureData ) ) ,
239+ ) ;
240+ if ( ! actionData || ! colony ) {
241+ return { } ;
242+ }
243+ // // @TODO : item.actionType === ColonyMotions.SetUserRolesMotion ? updatedRoles : roles,
244+ const formattedRolesTitle = formatRolesTitle ( actionData . roles ) ;
245+
166246 const getAmount = (
167247 actionType : ColonyActionType ,
168248 amount ?: string | null ,
@@ -209,6 +289,12 @@ export const mapColonyActionToExpectedFormat = ({
209289 actionData . fromDomain ?. metadata || actionData . pendingDomainMetadata ;
210290 }
211291
292+ const { stagedPaymentToken, summedAmount } = getExpenditureStagesData (
293+ expenditureData ?. metadata ?. stages || [ ] ,
294+ expenditureData ?. slots || [ ] ,
295+ colony ,
296+ ) ;
297+
212298 return {
213299 ...actionData ,
214300 [ ActionTitleMessageKeys . Amount ] : getFormattedValueWithFallback (
@@ -237,11 +323,7 @@ export const mapColonyActionToExpectedFormat = ({
237323 ActionTitleMessageKeys . Initiator ,
238324 notMaybe ( getInitiatorData ( actionData ) ) ,
239325 ) ,
240- [ ActionTitleMessageKeys . Recipient ] : getFormattedValueWithFallback (
241- getRecipient ( actionData ) ,
242- ActionTitleMessageKeys . Recipient ,
243- notMaybe ( getRecipientData ( actionData ) ) ,
244- ) ,
326+ [ ActionTitleMessageKeys . Recipient ] : recipient ,
245327 [ ActionTitleMessageKeys . ToDomain ] : getFormattedValueWithFallback (
246328 actionData . toDomain ?. metadata ?. name ??
247329 formatMessage ( { id : 'unknownDomain' } ) ,
@@ -317,6 +399,12 @@ export const mapColonyActionToExpectedFormat = ({
317399 id : 'decisionMethod.multiSig' ,
318400 } ) } `
319401 : '' ,
402+ [ ActionTitleMessageKeys . StagedAmount ] : (
403+ < Numeral
404+ value = { summedAmount }
405+ decimals = { getTokenDecimalsWithFallback ( stagedPaymentToken ?. decimals ) }
406+ />
407+ ) ,
320408 [ ActionTitleMessageKeys . SplitAmount ] : getFormattedValueWithFallback (
321409 < Numeral
322410 value = {
0 commit comments