@@ -22,12 +22,15 @@ import {
22
22
import { CompletedPayments } from "@/components/view-recurring-payments/blocks/completed-payments" ;
23
23
import { FrequencyBadge } from "@/components/view-recurring-payments/blocks/frequency-badge" ;
24
24
import { formatCurrencyLabel } from "@/lib/constants/currencies" ;
25
+ import {
26
+ calculateTotalsByCurrency ,
27
+ formatCurrencyTotals ,
28
+ } from "@/lib/helpers/currency" ;
25
29
import { useCancelRecurringPayment } from "@/lib/hooks/use-cancel-recurring-payment" ;
26
30
import type { SubscriptionWithDetails } from "@/lib/types" ;
27
31
import { getCanCancelPayment } from "@/lib/utils" ;
28
32
import { api } from "@/trpc/react" ;
29
33
import { addDays , format } from "date-fns" ;
30
- import { BigNumber , utils } from "ethers" ;
31
34
import { Ban , CreditCard , DollarSign , Loader2 } from "lucide-react" ;
32
35
import { useState } from "react" ;
33
36
import { MultiCurrencyStatCard } from "../multi-currency-stat-card" ;
@@ -206,64 +209,27 @@ export const Subscriptions = ({ initialSubscriptions }: SubscriptionProps) => {
206
209
initialData : initialSubscriptions ,
207
210
} ) ;
208
211
209
- const commitmentsByCurrency =
210
- subscriptions ?. reduce (
211
- ( acc , sub ) => {
212
- if ( ACTIVE_STATUSES . includes ( sub . status ) && sub . paymentCurrency ) {
213
- const currency = sub . paymentCurrency ;
214
- try {
215
- const nextPaymentAmount = utils . parseUnits ( sub . totalAmount , 18 ) ;
216
- if ( ! acc [ currency ] ) {
217
- acc [ currency ] = BigNumber . from ( "0" ) ;
218
- }
219
- acc [ currency ] = acc [ currency ] . add ( nextPaymentAmount ) ;
220
- } catch ( error ) {
221
- console . error ( "Error calculating commitment amount:" , error ) ;
222
- }
223
- }
224
- return acc ;
225
- } ,
226
- { } as Record < string , BigNumber > ,
227
- ) || { } ;
228
-
229
- const totalSpentByCurrency =
230
- subscriptions ?. reduce (
231
- ( acc , sub ) => {
232
- if ( sub . payments && sub . payments . length > 0 && sub . paymentCurrency ) {
233
- const currency = sub . paymentCurrency ;
234
- try {
235
- const paymentAmount = utils . parseUnits ( sub . totalAmount , 18 ) ;
236
- const completedPayments = BigNumber . from (
237
- sub . payments . length . toString ( ) ,
238
- ) ;
239
- const totalSpent = paymentAmount . mul ( completedPayments ) ;
212
+ const commitmentItems =
213
+ subscriptions
214
+ ?. filter ( ( sub ) => ACTIVE_STATUSES . includes ( sub . status ) )
215
+ . map ( ( sub ) => ( {
216
+ amount : sub . totalAmount ,
217
+ currency : sub . paymentCurrency ,
218
+ } ) ) || [ ] ;
240
219
241
- if ( ! acc [ currency ] ) {
242
- acc [ currency ] = BigNumber . from ( "0" ) ;
243
- }
244
- acc [ currency ] = acc [ currency ] . add ( totalSpent ) ;
245
- } catch ( error ) {
246
- console . error ( "Error calculating total spent:" , error ) ;
247
- }
248
- }
249
- return acc ;
250
- } ,
251
- { } as Record < string , BigNumber > ,
252
- ) || { } ;
220
+ const spentItems =
221
+ subscriptions
222
+ ?. filter ( ( sub ) => ( sub ?. payments ? sub . payments . length > 0 : false ) )
223
+ . flatMap ( ( sub ) => ( {
224
+ amount : sub . totalAmount ,
225
+ currency : sub . paymentCurrency ,
226
+ } ) ) || [ ] ;
253
227
254
- const commitmentValues = Object . entries ( commitmentsByCurrency ) . map (
255
- ( [ currency , amount ] ) => ( {
256
- amount : utils . formatUnits ( amount , 18 ) ,
257
- currency,
258
- } ) ,
259
- ) ;
228
+ const commitmentTotals = calculateTotalsByCurrency ( commitmentItems ) ;
229
+ const spentTotals = calculateTotalsByCurrency ( spentItems ) ;
260
230
261
- const spentValues = Object . entries ( totalSpentByCurrency ) . map (
262
- ( [ currency , amount ] ) => ( {
263
- amount : utils . formatUnits ( amount , 18 ) ,
264
- currency,
265
- } ) ,
266
- ) ;
231
+ const commitmentValues = formatCurrencyTotals ( commitmentTotals ) ;
232
+ const spentValues = formatCurrencyTotals ( spentTotals ) ;
267
233
268
234
return (
269
235
< div className = "space-y-6" >
0 commit comments