Skip to content

Commit 1847144

Browse files
committed
chore: adding csv as an option to subject table
1 parent f306b74 commit 1847144

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

apps/web/src/hooks/useInstrumentVisualization.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useInstrument } from '@/hooks/useInstrument';
88
import { useInstrumentInfoQuery } from '@/hooks/useInstrumentInfoQuery';
99
import { useInstrumentRecords } from '@/hooks/useInstrumentRecords';
1010
import { useAppStore } from '@/store';
11+
import { parse, unparse } from 'papaparse';
1112

1213
type 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

apps/web/src/routes/_app/datahub/$subjectId/table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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}

0 commit comments

Comments
 (0)