Files
supabase/apps/studio/components/interfaces/Reports/ReportWidget.tsx
Gildas Garcia 96d43099bb chore: refactor Button API so that it can be used a standard button (#46880)
## Problem

Our `<Button>` component breaks the default `button` contract by
redefining the `type` prop to set its variant (`primary`, `default`,
etc) instead of the button type (`submit`, `button`, etc).
This is confusing and forces to write more code when using it with
shadcn components that expect/inject the standard button props.

## Solution

- rename the `type` prop to `variant`
- rename the `htmlType` prop to `type`
- propagate the changes where necessary
- format code

## How to test

As this is just prop renaming, if it builds it's ok

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2026-06-16 23:59:58 +02:00

116 lines
3.9 KiB
TypeScript

import { useParams } from 'common'
import { ExternalLink, HelpCircle } from 'lucide-react'
import { NextRouter, useRouter } from 'next/router'
import { ReactNode } from 'react'
import { Button, cn, Loading, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
import type { LogsEndpointParams } from '../Settings/Logs/Logs.types'
import type { BaseReportParams, ReportQueryType } from './Reports.types'
import Panel from '@/components/ui/Panel'
export interface ReportWidgetProps<T = any> {
data: T[]
title: string
description?: string
error?: string | Object | null
tooltip?: string | ReactNode
className?: string
contentClassName?: string
headerClassName?: string
renderer: (props: ReportWidgetRendererProps) => ReactNode
append?: (props: ReportWidgetRendererProps) => ReactNode
// for overriding props, such as data
appendProps?: Partial<ReportWidgetRendererProps>
// omitting params will hide the "View in logs explorer" button
params?: BaseReportParams | LogsEndpointParams
queryType?: ReportQueryType
isLoading: boolean
resolvedSql?: string
}
export interface ReportWidgetRendererProps<T = any> extends ReportWidgetProps<T> {
router: NextRouter
projectRef: string
}
const ReportWidget = (props: ReportWidgetProps) => {
const router = useRouter()
const { ref } = useParams()
const projectRef = ref as string
return (
<Panel noMargin noHideOverflow className={cn('pb-0', props.className)} wrapWithLoading={false}>
<Panel.Content className={cn('space-y-4', props.contentClassName)}>
<div className={cn('flex flex-row items-start justify-between', props.headerClassName)}>
<div className="gap-2">
<div className="flex flex-row gap-2">
<h3 className="w-full h-6">{props.title}</h3>{' '}
{props?.tooltip && (
<Tooltip>
<TooltipTrigger>
<HelpCircle className="text-foreground-light" size={14} strokeWidth={1.5} />
</TooltipTrigger>
<TooltipContent side="bottom">{props.tooltip}</TooltipContent>
</Tooltip>
)}
</div>
<p className="text-sm text-foreground-light">{props.description}</p>
</div>
{props.params && (
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="default"
icon={<ExternalLink />}
className="px-1"
onClick={() => {
const isDbQueryType = props.queryType === 'db'
const pathname = isDbQueryType
? `/project/${projectRef}/sql/new`
: `/project/${projectRef}/logs/explorer`
const query: Record<string, string | undefined> = {}
if (isDbQueryType) {
query.content = props.resolvedSql
} else {
query.q = props.params?.sql
query.its = props.params?.iso_timestamp_start || ''
query.ite = props.params?.iso_timestamp_end || ''
}
router.push({ pathname, query })
}}
/>
</TooltipTrigger>
<TooltipContent side="left">
{props.queryType === 'db' ? 'Open in SQL Editor' : 'Open in Logs Explorer'}
</TooltipContent>
</Tooltip>
)}
</div>
<Loading active={props.isLoading}>
{props.data === undefined
? null
: props.renderer({ ...props, router, projectRef: projectRef as string })}
</Loading>
</Panel.Content>
{props.append && (
<>
{props.append({
...props,
...(props.appendProps || {}),
router,
projectRef: projectRef as string,
})}
</>
)}
</Panel>
)
}
export default ReportWidget