import type { Box } from '@rocket.chat/fuselage'; import { IconButton } from '@rocket.chat/fuselage'; import { useToastMessageDispatch } from '@rocket.chat/ui-contexts'; import type { ComponentProps } from 'react'; import { useTranslation } from 'react-i18next'; import { downloadCsvAs } from '../../lib/download'; type RowFor = readonly unknown[] & { length: THeaders['length']; }; type DownloadDataButtonProps = { attachmentName: string; headers: RowFor; dataAvailable: boolean; dataExtractor: () => Promise[] | undefined> | RowFor[] | undefined; } & Omit, 'attachmentName' | 'headers' | 'data'>; const DownloadDataButton = ({ attachmentName, headers, dataAvailable, dataExtractor, ...props }: DownloadDataButtonProps) => { const { t } = useTranslation(); const dispatchToastMessage = useToastMessageDispatch(); const handleClick = (): void => { if (!dataAvailable) { return; } Promise.resolve(dataExtractor()) .then((data) => { if (!data) { return; } downloadCsvAs([headers, ...data], attachmentName); }) .catch((error) => { dispatchToastMessage({ type: 'error', message: error }); }); }; return ( ); }; export default DownloadDataButton;