Skip to content

Commit 51cc8ff

Browse files
committed
feat: add a csv option to subject table download
1 parent 1847144 commit 51cc8ff

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

apps/web/src/hooks/useInstrumentVisualization.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { useEffect, useMemo, useState } from 'react';
33
import { useDownload, useNotificationsStore, useTranslation } from '@douglasneuroinformatics/libui/hooks';
44
import type { AnyUnilingualScalarInstrument, InstrumentKind } from '@opendatacapture/runtime-core';
55
import { omit } from 'lodash-es';
6+
import { unparse } from 'papaparse';
67

78
import { useInstrument } from '@/hooks/useInstrument';
89
import { useInstrumentInfoQuery } from '@/hooks/useInstrumentInfoQuery';
910
import { useInstrumentRecords } from '@/hooks/useInstrumentRecords';
1011
import { useAppStore } from '@/store';
11-
import { parse, unparse } from 'papaparse';
1212

1313
type InstrumentVisualizationRecord = {
1414
[key: string]: unknown;
@@ -51,7 +51,7 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
5151
}
5252
});
5353

54-
const dl = (option: 'JSON' | 'TSV' | 'CSV') => {
54+
const dl = (option: 'CSV' | 'JSON' | 'TSV') => {
5555
if (!instrument) {
5656
notifications.addNotification({ message: t('errors.noInstrumentSelected'), type: 'error' });
5757
return;
@@ -67,6 +67,32 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
6767
const exportRecords = records.map((record) => omit(record, ['__date__', '__time__']));
6868

6969
switch (option) {
70+
case 'CSV':
71+
void download(`${baseFilename}.csv`, () => {
72+
const columnNames = Object.keys(exportRecords[0]!);
73+
74+
//fill object array with export record items
75+
const rows = exportRecords.map((item) => {
76+
const obj: { [key: string]: any } = {};
77+
for (const key of columnNames) {
78+
const val = item[key];
79+
obj[key] = typeof val === 'object' ? JSON.stringify(val) : val;
80+
}
81+
return obj;
82+
});
83+
84+
const csv = unparse(rows, {
85+
delimiter: ',',
86+
escapeChar: '"',
87+
header: true,
88+
quoteChar: '"',
89+
quotes: false,
90+
skipEmptyLines: true
91+
});
92+
93+
return csv;
94+
});
95+
break;
7096
case 'JSON':
7197
void download(`${baseFilename}.json`, () => Promise.resolve(JSON.stringify(exportRecords, null, 2)));
7298
break;
@@ -83,28 +109,6 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
83109
return columnNames + '\n' + rows;
84110
});
85111
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;
108112
}
109113
};
110114

0 commit comments

Comments
 (0)