Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: add csv download buttons, with json flattening

TzeYiing2c9caeaba7f0
diff --git a/studio/components/interfaces/Settings/Logs/LogTable.tsx b/studio/components/interfaces/Settings/Logs/LogTable.tsx
index 15738955037..e235c9715e8 100644
--- a/studio/components/interfaces/Settings/Logs/LogTable.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogTable.tsx
@@ -19,6 +19,7 @@ import { SeverityFormatter, ResponseCodeFormatter, HeaderFormmater } from './Log
// column renders
import DatabaseApiColumnRender from './LogColumnRenderers/DatabaseApiColumnRender'
import DatabasePostgresColumnRender from './LogColumnRenderers/DatabasePostgresColumnRender'
+import CSVButton from 'components/ui/CSVButton'
interface Props {
data?: Array<LogData | Object>
@@ -59,10 +60,8 @@ const LogTable = ({
})
let columns
if (!queryType) {
- console.log('no query type')
columns = DEFAULT_COLUMNS
} else {
- console.log('has query type', queryType)
switch (queryType) {
case 'api':
columns = DatabaseApiColumnRender
@@ -198,6 +197,7 @@ const LogTable = ({
return dedupedData
}
}, [stringData])
+
return (
<>
<section
@@ -240,9 +240,7 @@ const LogTable = ({
Histogram
</Button>
)}
- <Button type="default" icon={<IconDownloadCloud />}>
- Download
- </Button>
+ {showDownload && <CSVButton data={data}>Download</CSVButton>}
</div>
</div>
</div>
diff --git a/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx b/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx
index 598b47d7aea..80dc404e89e 100644
--- a/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx
@@ -134,6 +134,7 @@ export const LogsPreviewer: React.FC<Props> = ({ projectRef, queryType, override
return (
<div className="h-full flex flex-col flex-grow">
<PreviewFilterPanel
+ csvData={logData}
isLoading={isLoading}
newCount={newCount}
templates={TEMPLATES.filter(
@@ -192,11 +193,11 @@ export const LogsPreviewer: React.FC<Props> = ({ projectRef, queryType, override
/>
</LoadingOpacity>
{!error && (
- <div className="p-2">
+ <div className="p-2">
<Button onClick={loadOlder} icon={<IconRewind />} type="default">
- Load older
- </Button>
- </div>
+ Load older
+ </Button>
+ </div>
)}
{error && (
<div className="flex w-full h-full justify-center items-center mx-auto">
diff --git a/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx b/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx
index e212ee913ef..bedc6b0c547 100644
--- a/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx
+++ b/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx
@@ -15,6 +15,7 @@ import utc from 'dayjs/plugin/utc'
import { FILTER_OPTIONS, LogsTableName } from './Logs.constants'
import { LogsFilter } from './Logs.filter'
import DatePickers from './Logs.DatePickers'
+import CSVButton from 'components/ui/CSVButton'
interface Props {
defaultSearchValue?: string
@@ -33,6 +34,7 @@ interface Props {
condensedLayout: Boolean
isShowingEventChart: boolean
onToggleEventChart: () => void
+ csvData?: unknown[]
}
dayjs.extend(utc)
@@ -55,6 +57,7 @@ const PreviewFilterPanel: FC<Props> = ({
condensedLayout,
isShowingEventChart,
onToggleEventChart,
+ csvData,
}) => {
const [search, setSearch] = useState('')
@@ -205,6 +208,7 @@ const PreviewFilterPanel: FC<Props> = ({
Event chart
</Button>
</div>
+ <CSVButton data={csvData} disabled={!Boolean(csvData)} title="Download data" />
</div>
<Button type="secondary" onClick={onExploreClick} iconRight={<IconExternalLink size={10} />}>
diff --git a/studio/components/ui/CSVButton.tsx b/studio/components/ui/CSVButton.tsx
new file mode 100644
index 00000000000..697c40df5fa
--- /dev/null
+++ b/studio/components/ui/CSVButton.tsx
@@ -0,0 +1,70 @@
+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<Props> = ({
+ 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) 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
diff --git a/studio/tests/pages/projects/logs-query.test.js b/studio/tests/pages/projects/logs-query.test.js
index 78c1d951b4f..fff6953421b 100644
--- a/studio/tests/pages/projects/logs-query.test.js
+++ b/studio/tests/pages/projects/logs-query.test.js
@@ -44,7 +44,7 @@ import { useFlag } from 'hooks'
useFlag.mockReturnValue(true)
import { SWRConfig } from 'swr'
-import { LogsExplorerPage as Page } from 'pages/project/[ref]/logs-explorer/query'
+import { LogsExplorerPage as Page } from 'pages/project/[ref]/logs-explorer/index'
const LogsExplorerPage = (props) => (
<SWRConfig
value={{