Skip to content

Commit 2dc7552

Browse files
committed
feat: add long options create makeWideRows helper function for wide obj creation
1 parent 27a15a2 commit 2dc7552

2 files changed

Lines changed: 20 additions & 32 deletions

File tree

apps/web/src/hooks/useInstrumentVisualization.ts

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
5252
}
5353
});
5454

55-
const dl = (option: 'CSV' | 'JSON' | 'TSV') => {
55+
const dl = (option: 'CSV' | 'CSV Long' | 'JSON' | 'TSV' | 'TSV Long') => {
5656
if (!instrument) {
5757
notifications.addNotification({ message: t('errors.noInstrumentSelected'), type: 'error' });
5858
return;
@@ -67,24 +67,26 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
6767

6868
const exportRecords = records.map((record) => omit(record, ['__time__']));
6969

70+
const makeWideRows = () => {
71+
const columnNames = Object.keys(exportRecords[0]!);
72+
return exportRecords.map((item) => {
73+
const obj: { [key: string]: any } = { subjectId: params.subjectId };
74+
for (const key of columnNames) {
75+
const val = item[key];
76+
if (key === '__date__') {
77+
obj.Date = toBasicISOString(val as Date);
78+
continue;
79+
}
80+
obj[key] = typeof val === 'object' ? JSON.stringify(val) : val;
81+
}
82+
return obj;
83+
});
84+
};
85+
7086
switch (option) {
7187
case 'CSV':
7288
void download(`${baseFilename}.csv`, () => {
73-
const columnNames = Object.keys(exportRecords[0]!);
74-
75-
const rows = exportRecords.map((item) => {
76-
const obj: { [key: string]: any } = {};
77-
obj.subjectId = params.subjectId;
78-
for (const key of columnNames) {
79-
const val = item[key];
80-
if (key === '__date__') {
81-
obj.Date = toBasicISOString(val as Date);
82-
continue;
83-
}
84-
obj[key] = typeof val === 'object' ? JSON.stringify(val) : val;
85-
}
86-
return obj;
87-
});
89+
const rows = makeWideRows();
8890

8991
const csv = unparse(rows, {
9092
delimiter: ',',
@@ -107,21 +109,7 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
107109
}
108110
case 'TSV':
109111
void download(`${baseFilename}.tsv`, () => {
110-
const columnNames = Object.keys(exportRecords[0]!);
111-
112-
const rows = exportRecords.map((item) => {
113-
const obj: { [key: string]: any } = {};
114-
obj.subjectId = params.subjectId;
115-
for (const key of columnNames) {
116-
const val = item[key];
117-
if (key === '__date__') {
118-
obj.Date = toBasicISOString(val as Date);
119-
continue;
120-
}
121-
obj[key] = typeof val === 'object' ? JSON.stringify(val) : val;
122-
}
123-
return obj;
124-
});
112+
const rows = makeWideRows();
125113

126114
const tsv = unparse(rows, {
127115
delimiter: '\t',

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', 'CSV']}
41+
options={['TSV', 'TSV Long', 'JSON', 'CSV', 'CSV Long']}
4242
title={t('core.download')}
4343
triggerClassName="min-w-32"
4444
onSelection={dl}

0 commit comments

Comments
 (0)