import { Button, IconDownloadCloud } from '@supabase/ui' import { ButtonProps } from '@supabase/ui/dist/cjs/components/Button/Button' import { flattenDeep } from 'lodash' import React, { useMemo, useRef } from 'react' import { CSVLink } from 'react-csv' interface Props { buttonType?: ButtonProps['type'] onClick?: ButtonProps['onClick'] disabled?: ButtonProps['disabled'] icon?: React.ReactNode data?: unknown[] title?: string } const CSVButton: React.FC = ({ onClick, buttonType = 'default', icon, children, disabled, data, title, }) => { const csvRef = useRef(null) const handleDownload = () => { ;(csvRef.current as any)?.link.click() } const formattedData = useMemo(() => { const first = data?.[0] if (!first || !data) return const keys = Object.keys(first as any) return data.map((datum: any) => { return keys.reduce((acc: any, key) => { if (typeof datum[key] === 'object') { acc[key] = JSON.stringify(datum[key]) as string } else { acc[key] = String(datum[key]) } return acc }, {}) }) // retrieve dot notation of all keys }, [JSON.stringify(data)]) return ( <> ) } export default CSVButton