import React, { useState } from 'react' import Button from '../Button/Button' import DropdownMenu from '../DropdownMenu/DropdownMenu' import Icon from '../Icons/Icon' import { type PopoverProps } from '../Popover/Popover' import Spinner from '../Spinner/Spinner' import SingleCsv from './SingleCsv' import styles from './_csv-export.module.scss' type SingleCsvProps = React.ComponentProps export type CsvExportProps = { /** The download options that are defined in the SingleCsvProps. Includes `csvName`, `linkName`, `csvData` (if we create the CSV on our end), `csvFormat` (if the backend creates the CSV), and other options. */ csvDownloadOptions: SingleCsvProps['csv'][] /** Determines if this component is shown immediately. A reason why this would be `false` is if we have to wait for an api to finish so that the data will be available for the CSV. This only happens when the frontend generates the CSV and should never be false when the backend generates the CSV. */ initialDisplay: boolean /** Determines when to show the download icon. */ show: boolean /** Boolean value that opens popover even when there is only one download option */ alwaysShowDropdown?: boolean /** Optional custom id for the HTML id attribute that corresponds to the component */ csvId?: string /** Optional tippy placement override */ tippyPlacement?: PopoverProps['position'] /** Optional prop to add a test id to the CsvExport for QA testing */ qaTestId?: string } const CsvExport = ({ csvDownloadOptions, initialDisplay, show, alwaysShowDropdown, csvId, tippyPlacement, qaTestId = 'csv-export', }: CsvExportProps): React.JSX.Element => { const [loading, setLoading] = useState(false) if (!initialDisplay) { return <> } else { if (csvDownloadOptions.length > 1 || alwaysShowDropdown) { const updatedOptions = csvDownloadOptions.map((csv) => ({ csv: { ...csv }, setLoading: setLoading, loading: loading, })) return ( {!loading ? ( ) : ( )} } popoverProps={{ position: tippyPlacement ?? 'bottom' }} qaTestId={qaTestId} /> ) } else { // eslint-disable-next-line @typescript-eslint/no-unused-expressions csvDownloadOptions[0].customClass ?? '' return (