Files
supabase/apps/studio/components/ui/CSVButton.tsx
T
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

71 lines
1.7 KiB
TypeScript

import { Button, IconDownloadCloud } from 'ui'
import { ButtonProps } from 'ui/src/components/Button/Button'
import { flattenDeep } from 'lodash'
import React, { PropsWithChildren, useMemo, useRef } from 'react'
import { CSVLink } from 'react-csv'
interface CSVButtonProps {
buttonType?: ButtonProps['type']
onClick?: ButtonProps['onClick']
disabled?: ButtonProps['disabled']
icon?: React.ReactNode
data?: unknown[]
title?: string
}
const CSVButton = ({
onClick,
buttonType = 'default',
icon,
children,
disabled,
data,
title,
}: PropsWithChildren<CSVButtonProps>) => {
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 (
<>
<CSVLink
ref={csvRef}
className="hidden"
data={formattedData || ([] as any)}
filename={`supabase_logs.csv`}
title={title}
/>
<Button
type={buttonType}
icon={icon || <IconDownloadCloud />}
disabled={disabled}
onClick={(e) => {
if (onClick) onClick(e)
handleDownload()
}}
>
{children}
</Button>
</>
)
}
export default CSVButton