File tree Expand file tree Collapse file tree
routes/_app/datahub/$subjectId Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import { useInstrument } from '@/hooks/useInstrument';
88import { useInstrumentInfoQuery } from '@/hooks/useInstrumentInfoQuery' ;
99import { useInstrumentRecords } from '@/hooks/useInstrumentRecords' ;
1010import { useAppStore } from '@/store' ;
11+ import { parse , unparse } from 'papaparse' ;
1112
1213type InstrumentVisualizationRecord = {
1314 [ key : string ] : unknown ;
@@ -50,7 +51,7 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
5051 }
5152 } ) ;
5253
53- const dl = ( option : 'JSON' | 'TSV' ) => {
54+ const dl = ( option : 'JSON' | 'TSV' | 'CSV' ) => {
5455 if ( ! instrument ) {
5556 notifications . addNotification ( { message : t ( 'errors.noInstrumentSelected' ) , type : 'error' } ) ;
5657 return ;
@@ -81,6 +82,29 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
8182 . join ( '\n' ) ;
8283 return columnNames + '\n' + rows ;
8384 } ) ;
85+ break ;
86+ case 'CSV' :
87+ void download ( `${ baseFilename } .csv` , ( ) => {
88+ const columnNames = Object . keys ( exportRecords [ 0 ] ! ) . join ( ',' ) ;
89+ const rows = exportRecords
90+ . map ( ( item ) =>
91+ Object . values ( item )
92+ . map ( ( val ) => '"' + JSON . stringify ( val ) + '"' )
93+ . join ( ',' )
94+ )
95+ . join ( '\n' ) ;
96+
97+ const transformed = columnNames + '\n' + rows ;
98+
99+ const parsed = parse ( transformed , {
100+ header : true ,
101+ skipEmptyLines : true ,
102+ dynamicTyping : true
103+ } ) ;
104+
105+ return transformed ;
106+ } ) ;
107+ break ;
84108 }
85109 } ;
86110
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ const RouteComponent = () => {
3838 widthFull
3939 data-spotlight-type = "export-data-dropdown"
4040 disabled = { ! instrumentId }
41- options = { [ 'TSV' , 'JSON' ] }
41+ options = { [ 'TSV' , 'JSON' , 'CSV' ] }
4242 title = { t ( 'core.download' ) }
4343 triggerClassName = "min-w-32"
4444 onSelection = { dl }
You can’t perform that action at this time.
0 commit comments