import styles from './Selector/Selector.module.css'; interface Props { format: string; url: URL | (() => Promise) | undefined; } function ExportEntry(props: Props) { const { format, url } = props; const label = `Export to ${format.toUpperCase()}`; const filename = `data.${format}`; async function handleBtnClick(getURLOrBlob: () => Promise) { const urlOrBlob = await getURLOrBlob(); /* Because `getURLOrBlob` is async, we can't rely on the browser's default click action * to start the download. We have to create a dummy anchor element, add it to the DOM * and click on it programmatically. */ const anchor = document.createElement('a'); anchor.download = filename; document.body.append(anchor); if (urlOrBlob instanceof Blob) { const blobUrl = URL.createObjectURL(urlOrBlob); anchor.href = blobUrl; anchor.click(); URL.revokeObjectURL(blobUrl); } else { anchor.href = urlOrBlob.href; anchor.click(); } anchor.remove(); } if (url instanceof URL) { return ( {label} ); } if (typeof url === 'function') { return ( ); } return null; } export type { Props as ExportEntryProps }; export default ExportEntry;