1
1
import React , { useState } from 'react' ;
2
- import { StyleSheet , View , Platform , Alert , Modal } from 'react-native' ;
3
- import RNFS from 'react-native-fs' ;
4
- import Button from '../../components/Button' ;
5
- import TextInput from '../../components/TextInput' ;
6
- import { localeString } from '../../utils/LocaleUtils' ;
7
- import { themeColor } from '../../utils/ThemeUtils' ;
2
+ import { StyleSheet , View , Alert , Modal } from 'react-native' ;
3
+
8
4
import Invoice from '../../models/Invoice' ;
9
5
import Payment from '../../models/Payment' ;
10
6
import Transaction from '../../models/Transaction' ;
7
+
8
+ import Button from '../../components/Button' ;
9
+ import TextInput from '../../components/TextInput' ;
11
10
import LoadingIndicator from '../../components/LoadingIndicator' ;
12
11
12
+ import { localeString } from '../../utils/LocaleUtils' ;
13
+ import { themeColor } from '../../utils/ThemeUtils' ;
14
+ import {
15
+ getFormattedDateTime ,
16
+ convertActivityToCsv ,
17
+ saveCsvFile ,
18
+ CSV_KEYS
19
+ } from '../../utils/ActivityCsvUtils' ;
20
+
13
21
interface ActivityProps {
14
22
filteredActivity : Array < Invoice | Payment | Transaction > ;
15
23
isVisible : boolean ;
@@ -29,87 +37,22 @@ const ActivityToCsv: React.FC<ActivityProps> = ({
29
37
closeModal ( ) ;
30
38
} ;
31
39
32
- const getFormattedDateTime = ( ) => {
33
- const now = new Date ( ) ;
34
- const year = now . getFullYear ( ) ;
35
- const month = ( now . getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , '0' ) ;
36
- const day = now . getDate ( ) . toString ( ) . padStart ( 2 , '0' ) ;
37
- const hours = now . getHours ( ) . toString ( ) . padStart ( 2 , '0' ) ;
38
- const minutes = now . getMinutes ( ) . toString ( ) . padStart ( 2 , '0' ) ;
39
- const seconds = now . getSeconds ( ) . toString ( ) . padStart ( 2 , '0' ) ;
40
-
41
- return `${ year } ${ month } ${ day } _${ hours } ${ minutes } ${ seconds } ` ;
42
- } ;
43
-
44
- const convertActivityToCsv = async (
45
- data : Array < Invoice | Payment | Transaction > ,
46
- keysToInclude : Array < any >
47
- ) => {
48
- if ( ! data || data . length === 0 ) {
49
- return '' ;
50
- }
51
-
52
- try {
53
- const header = keysToInclude . map ( ( field ) => field . label ) . join ( ',' ) ;
54
- const rows = data
55
- ?. map ( ( item : any ) =>
56
- keysToInclude
57
- . map ( ( field ) => `"${ item [ field . value ] } "` || '' )
58
- . join ( ',' )
59
- )
60
- . join ( '\n' ) ;
61
-
62
- return `${ header } \n${ rows } ` ;
63
- } catch ( err ) {
64
- console . error ( err ) ;
65
- return '' ;
66
- }
67
- } ;
68
-
69
40
const downloadCsv = async ( ) => {
70
41
setIsLoading ( true ) ;
71
42
setTimeout ( async ( ) => {
72
- const invoiceKeys = [
73
- { label : 'Amount Paid (sat)' , value : 'getAmount' } ,
74
- { label : 'Payment Request' , value : 'getPaymentRequest' } ,
75
- { label : 'Payment Hash' , value : 'getRHash' } ,
76
- { label : 'Memo' , value : 'getMemo' } ,
77
- { label : 'Note' , value : 'getNote' } ,
78
- { label : 'Creation Date' , value : 'getCreationDate' } ,
79
- { label : 'Expiry' , value : 'formattedTimeUntilExpiry' }
80
- ] ;
81
-
82
- const paymentKeys = [
83
- { label : 'Destination' , value : 'getDestination' } ,
84
- { label : 'Payment Request' , value : 'getPaymentRequest' } ,
85
- { label : 'Payment Hash' , value : 'paymentHash' } ,
86
- { label : 'Amount Paid (sat)' , value : 'getAmount' } ,
87
- { label : 'Memo' , value : 'getMemo' } ,
88
- { label : 'Note' , value : 'getNote' } ,
89
- { label : 'Creation Date' , value : 'getDate' }
90
- ] ;
91
-
92
- const transactionKeys = [
93
- { label : 'Transaction Hash' , value : 'tx' } ,
94
- { label : 'Amount (sat)' , value : 'getAmount' } ,
95
- { label : 'Total Fees (sat)' , value : 'getFee' } ,
96
- { label : 'Note' , value : 'getNote' } ,
97
- { label : 'Timestamp' , value : 'getDate' }
98
- ] ;
99
-
100
43
const invoiceCsv = await convertActivityToCsv (
101
44
filteredActivity . filter ( ( item : any ) => item instanceof Invoice ) ,
102
- invoiceKeys
45
+ CSV_KEYS . invoice
103
46
) ;
104
47
const paymentCsv = await convertActivityToCsv (
105
48
filteredActivity . filter ( ( item : any ) => item instanceof Payment ) ,
106
- paymentKeys
49
+ CSV_KEYS . payment
107
50
) ;
108
51
const transactionCsv = await convertActivityToCsv (
109
52
filteredActivity . filter (
110
53
( item : any ) => item instanceof Transaction
111
54
) ,
112
- transactionKeys
55
+ CSV_KEYS . transaction
113
56
) ;
114
57
115
58
if ( ! invoiceCsv && ! paymentCsv && ! transactionCsv ) {
@@ -120,43 +63,21 @@ const ActivityToCsv: React.FC<ActivityProps> = ({
120
63
try {
121
64
const dateTime = getFormattedDateTime ( ) ;
122
65
const baseFileName = customFileName || `zeus_${ dateTime } ` ;
123
- const invoiceFileName = `${ baseFileName } _ln_invoices.csv` ;
124
- const paymentFileName = `${ baseFileName } _ln_payments.csv` ;
125
- const transactionFileName = `${ baseFileName } _onchain.csv` ;
126
-
127
- const invoiceFilePath =
128
- Platform . OS === 'android'
129
- ? `${ RNFS . DownloadDirectoryPath } /${ invoiceFileName } `
130
- : `${ RNFS . DocumentDirectoryPath } /${ invoiceFileName } ` ;
131
-
132
- const paymentFilePath =
133
- Platform . OS === 'android'
134
- ? `${ RNFS . DownloadDirectoryPath } /${ paymentFileName } `
135
- : `${ RNFS . DocumentDirectoryPath } /${ paymentFileName } ` ;
136
-
137
- const transactionFilePath =
138
- Platform . OS === 'android'
139
- ? `${ RNFS . DownloadDirectoryPath } /${ transactionFileName } `
140
- : `${ RNFS . DocumentDirectoryPath } /${ transactionFileName } ` ;
141
-
142
- if ( invoiceCsv ) {
143
- console . log ( 'invoiceFilePath' , invoiceFilePath ) ;
144
- await RNFS . writeFile ( invoiceFilePath , invoiceCsv , 'utf8' ) ;
145
- }
146
-
147
- if ( paymentCsv ) {
148
- console . log ( 'paymentFilePath' , paymentFilePath ) ;
149
- await RNFS . writeFile ( paymentFilePath , paymentCsv , 'utf8' ) ;
150
- }
151
-
152
- if ( transactionCsv ) {
153
- console . log ( 'transactionFilePath' , transactionFilePath ) ;
154
- await RNFS . writeFile (
155
- transactionFilePath ,
156
- transactionCsv ,
157
- 'utf8'
66
+ if ( invoiceCsv )
67
+ await saveCsvFile (
68
+ `${ baseFileName } _ln_invoices.csv` ,
69
+ invoiceCsv
70
+ ) ;
71
+ if ( paymentCsv )
72
+ await saveCsvFile (
73
+ `${ baseFileName } _ln_payments.csv` ,
74
+ paymentCsv
75
+ ) ;
76
+ if ( transactionCsv )
77
+ await saveCsvFile (
78
+ `${ baseFileName } _onchain.csv` ,
79
+ transactionCsv
158
80
) ;
159
- }
160
81
161
82
Alert . alert (
162
83
localeString ( 'general.success' ) ,
0 commit comments