forked from GenSpectrum/cov-spectrum-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadWrapper.tsx
59 lines (50 loc) · 2.08 KB
/
DownloadWrapper.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { useContext, useEffect, useRef } from 'react';
import { DeregistrationHandle, ExportManagerContext } from '../components/CombinedExport/ExportManager';
import { csvStringify } from '../helpers/csvStringifyHelper';
import download from 'downloadjs';
import { pprettyFileFormats, PprettyRequest } from '../data/ppretty/ppretty-request';
import { getPlotUrl } from '../data/ppretty/api-ppretty';
import { PprettyGridExportManagerContext } from '../components/CombinedExport/PprettyGridExportManager';
interface Props {
name: string;
csvData?: { [key: string]: any }[];
pprettyRequest?: PprettyRequest;
children: React.ReactNode;
}
// Adds items to "Export" dropdown to download the wrapped component as an image or CSV
const DownloadWrapper = ({ name = 'plot', csvData, pprettyRequest, children }: Props) => {
const componentRef = useRef(null);
const exportManager = useContext(ExportManagerContext);
const pprettyGridExportManager = useContext(PprettyGridExportManagerContext);
useEffect(() => {
if (pprettyRequest) {
const handles: DeregistrationHandle[] = [];
for (const format of pprettyFileFormats) {
const handle = exportManager.register('Download ' + format.toUpperCase(), async () => {
const pprettyUrl = await getPlotUrl(pprettyRequest, format);
window.open(pprettyUrl);
});
handles.push(handle);
}
const pprettyHandle = pprettyGridExportManager.register(pprettyRequest);
return () => {
handles.forEach(handle => handle.deregister());
pprettyHandle.deregister();
};
}
}, [componentRef, pprettyRequest, exportManager, pprettyGridExportManager, name]);
useEffect(() => {
if (csvData) {
const handle = exportManager.register('Download CSV', async () => {
download(await csvStringify(csvData), `${name}.csv`, 'text/csv');
});
return handle.deregister;
}
}, [componentRef, csvData, exportManager, name]);
return (
<div className='relative h-full' ref={componentRef}>
{children}
</div>
);
};
export default DownloadWrapper;