@@ -3,24 +3,48 @@ import { writeTextFile, writeFile, BaseDirectory } from "@tauri-apps/plugin-fs";
33
44export type ExportType = "xlsx" | "ods" | "json" | "csv" ;
55
6- export const exportSpreadsheet = ( args : {
6+ export const exportData = ( args : {
7+ data : Record < string , string > [ ] ;
8+ fileName : string ;
9+ fileType : ExportType ;
10+ } ) => {
11+ const { fileType } = args ;
12+
13+ if ( fileType === "xlsx" || fileType === "ods" ) {
14+ return exportSpreadsheet ( {
15+ ...args ,
16+ fileType,
17+ } ) ;
18+ }
19+
20+ if ( fileType === "json" ) {
21+ return exportJson ( args ) ;
22+ }
23+
24+ if ( fileType === "csv" ) {
25+ return exportCsv ( args ) ;
26+ }
27+ } ;
28+
29+ const exportSpreadsheet = ( args : {
730 data : Record < string , string | Date > [ ] ;
831 fileName : string ;
932 fileType : "xlsx" | "ods" ;
1033} ) => {
1134 const wb = XLSX . utils . book_new ( ) ;
1235
1336 const data = args . data . map ( ( row ) => {
14- return {
15- ...row ,
16- start_date_time : row . start_date_time
17- ? new Date ( row . start_date_time )
18- : null ,
19- estimated_end_date_time : row . estimated_end_date_time
20- ? new Date ( row . estimated_end_date_time )
21- : null ,
22- end_date_time : row . end_date_time ? new Date ( row . end_date_time ) : null ,
23- } ;
37+ const parsedRow : Record < string , unknown > = { } ;
38+
39+ for ( const [ key , value ] of Object . entries ( row ) ) {
40+ if ( isDateString ( value ) ) {
41+ parsedRow [ key ] = new Date ( value as string ) ;
42+ } else {
43+ parsedRow [ key ] = value ;
44+ }
45+ }
46+
47+ return parsedRow ;
2448 } ) ;
2549
2650 const ws = XLSX . utils . json_to_sheet ( data , {
@@ -43,20 +67,8 @@ export const exportSpreadsheet = (args: {
4367 } ) ;
4468} ;
4569
46- const saveFile = ( args : { file : Uint8Array ; fileName : string } ) => {
47- return writeFile ( args . fileName , args . file , {
48- baseDir : BaseDirectory . Download ,
49- } ) ;
50- } ;
51-
52- const saveTextFile = ( args : { content : string ; fileName : string } ) => {
53- return writeTextFile ( args . fileName , args . content , {
54- baseDir : BaseDirectory . Download ,
55- } ) ;
56- } ;
57-
5870export const exportJson = ( args : {
59- data : Record < string , string > [ ] ;
71+ data : Record < string , unknown > [ ] ;
6072 fileName : string ;
6173 fileType : ExportType ;
6274} ) => {
@@ -68,7 +80,7 @@ export const exportJson = (args: {
6880 } ) ;
6981} ;
7082
71- export const exportCsv = ( args : {
83+ const exportCsv = ( args : {
7284 data : Record < string , string > [ ] ;
7385 fileName : string ;
7486 fileType : ExportType ;
@@ -85,25 +97,22 @@ export const exportCsv = (args: {
8597 } ) ;
8698} ;
8799
88- export const exportData = ( args : {
89- data : Record < string , string > [ ] ;
90- fileName : string ;
91- fileType : ExportType ;
92- } ) => {
93- const { fileType } = args ;
100+ const saveFile = ( args : { file : Uint8Array ; fileName : string } ) => {
101+ return writeFile ( args . fileName , args . file , {
102+ baseDir : BaseDirectory . Download ,
103+ } ) ;
104+ } ;
94105
95- if ( fileType === "xlsx" || fileType === "ods" ) {
96- return exportSpreadsheet ( {
97- ...args ,
98- fileType,
99- } ) ;
100- }
106+ const saveTextFile = ( args : { content : string ; fileName : string } ) => {
107+ return writeTextFile ( args . fileName , args . content , {
108+ baseDir : BaseDirectory . Download ,
109+ } ) ;
110+ } ;
101111
102- if ( fileType === "json" ) {
103- return exportJson ( args ) ;
104- }
112+ const isDateString = ( value : unknown ) : boolean => {
113+ if ( typeof value !== "string" ) return false ;
105114
106- if ( fileType === "csv" ) {
107- return exportCsv ( args ) ;
108- }
115+ // Check if it's a valid date string (ISO format or other parseable formats)
116+ const date = new Date ( value ) ;
117+ return ! isNaN ( date . getTime ( ) ) && value . match ( / \d { 4 } - \d { 2 } - \d { 2 } / ) !== null ;
109118} ;
0 commit comments