Skip to content

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

Replace LogChartHandler with LazyComposedChartHandler in ReportChartUpsell (#39286)

Joshen Lim50ece6a4f692
diff --git a/apps/studio/components/interfaces/Reports/v2/ReportChartUpsell.tsx b/apps/studio/components/interfaces/Reports/v2/ReportChartUpsell.tsx
index d5bd366bf00..77bda75ecc7 100644
--- a/apps/studio/components/interfaces/Reports/v2/ReportChartUpsell.tsx
+++ b/apps/studio/components/interfaces/Reports/v2/ReportChartUpsell.tsx
@@ -1,24 +1,23 @@
-import { LogChartHandler } from 'components/ui/Charts/LogChartHandler'
import Link from 'next/link'
import { useRef, useState } from 'react'
+import { LazyComposedChartHandler } from 'components/ui/Charts/ComposedChartHandler'
import { Button, Card, cn } from 'ui'
-export function ReportChartUpsell({
- report,
- orgSlug,
-}: {
+interface ReportsChartUpsellProps {
report: {
label: string
availableIn: string[]
}
orgSlug: string
-}) {
- const [isHoveringUpgrade, setIsHoveringUpgrade] = useState(false)
+}
+export const ReportChartUpsell = ({ report, orgSlug }: ReportsChartUpsellProps) => {
const startDate = '2025-01-01'
const endDate = '2025-01-02'
+ const [isHoveringUpgrade, setIsHoveringUpgrade] = useState(false)
+
const getExpDemoChartData = () =>
new Array(20).fill(0).map((_, index) => ({
period_start: new Date(startDate).getTime() + index * 1000,
@@ -65,7 +64,7 @@ export function ReportChartUpsell({
</Button>
</div>
<div className="absolute top-0 left-0 w-full h-full z-0">
- <LogChartHandler
+ <LazyComposedChartHandler
attributes={[
{
attribute: 'demo',
@@ -74,7 +73,7 @@ export function ReportChartUpsell({
provider: 'logs',
},
]}
- label={''}
+ label="Sample Report"
startDate={startDate}
endDate={endDate}
data={demoData as any}
diff --git a/apps/studio/components/ui/Charts/ComposedChartHandler.tsx b/apps/studio/components/ui/Charts/ComposedChartHandler.tsx
index e931c7ec848..008bb5acade 100644
--- a/apps/studio/components/ui/Charts/ComposedChartHandler.tsx
+++ b/apps/studio/components/ui/Charts/ComposedChartHandler.tsx
@@ -26,7 +26,7 @@ export interface ComposedChartHandlerProps {
attributes: MultiAttribute[]
startDate: string
endDate: string
- interval: string
+ interval?: string
customDateFormat?: string
defaultChartStyle?: 'bar' | 'line' | 'stackedAreaLine'
hideChartType?: boolean
diff --git a/apps/studio/components/ui/Charts/LogChartHandler.tsx b/apps/studio/components/ui/Charts/LogChartHandler.tsx
deleted file mode 100644
index 6a8e4569def..00000000000
--- a/apps/studio/components/ui/Charts/LogChartHandler.tsx
+++ /dev/null
@@ -1,246 +0,0 @@
-import { Loader2 } from 'lucide-react'
-import React, { PropsWithChildren, useEffect, useRef, useState } from 'react'
-import { cn, WarningIcon } from 'ui'
-
-import Panel from 'components/ui/Panel'
-import { ComposedChart } from './ComposedChart'
-
-import { AnalyticsInterval } from 'data/analytics/constants'
-import { useInfraMonitoringQueries } from 'data/analytics/infra-monitoring-queries'
-import { InfraMonitoringAttribute } from 'data/analytics/infra-monitoring-query'
-import { useProjectDailyStatsQueries } from 'data/analytics/project-daily-stats-queries'
-import { ProjectDailyStatsAttribute } from 'data/analytics/project-daily-stats-query'
-import { useChartHighlight } from './useChartHighlight'
-
-import type { UpdateDateRange } from 'pages/project/[ref]/reports/database'
-import type { ChartData } from './Charts.types'
-import type { MultiAttribute } from './ComposedChart.utils'
-
-interface LogChartHandlerProps {
- id?: string
- label: string
- attributes: MultiAttribute[]
- startDate: string
- endDate: string
- customDateFormat?: string
- defaultChartStyle?: 'bar' | 'line' | 'stackedAreaLine'
- hideChartType?: boolean
- data?: ChartData
- isLoading?: boolean
- format?: string
- highlightedValue?: string | number
- className?: string
- showTooltip?: boolean
- showLegend?: boolean
- showTotal?: boolean
- showMaxValue?: boolean
- updateDateRange: UpdateDateRange
- valuePrecision?: number
- isVisible?: boolean
- titleTooltip?: string
- docsUrl?: string
- syncId?: string
-}
-
-/**
- * Wrapper component that handles intersection observer logic for lazy loading
- */
-const LazyChartWrapper = ({ children }: PropsWithChildren) => {
- const [isVisible, setIsVisible] = useState(false)
- const ref = useRef<HTMLDivElement>(null)
-
- useEffect(() => {
- const observer = new IntersectionObserver(
- ([entry]) => {
- if (entry.isIntersecting) {
- setIsVisible(true)
- observer.disconnect()
- }
- },
- {
- rootMargin: '150px 0px', // Start loading before the component enters viewport
- threshold: 0,
- }
- )
-
- const currentRef = ref.current
- if (currentRef) {
- observer.observe(currentRef)
- }
-
- return () => {
- if (currentRef) {
- observer.unobserve(currentRef)
- }
- }
- }, [])
-
- return <div ref={ref}>{React.cloneElement(children as React.ReactElement, { isVisible })}</div>
-}
-
-/**
- * Controls chart display state. Optionally fetches static chart data if data is not provided.
- *
- * If the `data` prop is provided, it will disable automatic chart data fetching and pass the data directly to the chart render.
- * - loading state can also be provided through the `isLoading` prop, to display loading placeholders. Ignored if `data` key not provided.
- * - if `isLoading=true` and `data` is `undefined`, loading error message will be shown.
- *
- * Provided data must be in the expected chart format.
- */
-export const LogChartHandler = ({
- label,
- attributes,
- customDateFormat,
- children = null,
- defaultChartStyle = 'bar',
- hideChartType = false,
- data,
- isLoading,
- format,
- highlightedValue,
- className,
- showTooltip,
- showLegend,
- showMaxValue,
- showTotal,
- updateDateRange,
- valuePrecision,
- titleTooltip,
- id,
- syncId,
- ...otherProps
-}: PropsWithChildren<LogChartHandlerProps>) => {
- const [chartStyle, setChartStyle] = useState<string>(defaultChartStyle)
- const chartHighlight = useChartHighlight()
-
- if (!data) {
- return (
- <div className="flex h-52 w-full flex-col items-center justify-center gap-y-2">
- <WarningIcon />
- <p className="text-xs text-foreground-lighter">Unable to load data for {label}</p>
- </div>
- )
- }
-
- // Rest of the component remains similar, but pass all attributes to charts
- return (
- <Panel
- noMargin
- noHideOverflow
- className={cn('relative w-full overflow-hidden scroll-mt-16', className)}
- wrapWithLoading={false}
- id={id ?? label.toLowerCase().replaceAll(' ', '-')}
- >
- {isLoading && (
- <div className="absolute inset-0 rounded-md flex w-full flex-col items-center justify-center gap-y-2 bg-surface-100 backdrop z-20">
- <Loader2 size={18} className="animate-spin text-border-strong" />
- <p className="text-xs text-foreground-lighter">Loading data for {label}</p>
- </div>
- )}
- <Panel.Content className="flex flex-col gap-4">
- <div className="absolute right-6 z-50 flex justify-between scroll-mt-16">{children}</div>
- <ComposedChart
- attributes={attributes}
- data={data as any}
- format={format}
- xAxisKey="period_start"
- yAxisKey={attributes[0]?.attribute}
- highlightedValue={highlightedValue}
- title={label}
- customDateFormat={customDateFormat}
- chartHighlight={chartHighlight}
- chartStyle={chartStyle}
- showTooltip={showTooltip}
- showLegend={showLegend}
- showTotal={showTotal}
- showMaxValue={showMaxValue}
- onChartStyleChange={setChartStyle}
- updateDateRange={updateDateRange}
- valuePrecision={valuePrecision}
- hideChartType={hideChartType}
- titleTooltip={titleTooltip}
- syncId={syncId}
- {...otherProps}
- />
- </Panel.Content>
- </Panel>
- )
-}
-
-export const useAttributeQueries = (
- attributes: MultiAttribute[],
- ref: string | string[] | undefined,
- startDate: string,
- endDate: string,
- interval: AnalyticsInterval,
- databaseIdentifier: string | undefined,
- data: ChartData | undefined,
- isVisible: boolean
-) => {
- const infraAttributes = attributes.filter((attr) => attr.provider === 'infra-monitoring')
- const dailyStatsAttributes = attributes.filter((attr) => attr.provider === 'daily-stats')
-
- const infraQueries = useInfraMonitoringQueries(
- infraAttributes.map((attr) => attr.attribute as InfraMonitoringAttribute),
- ref,
- startDate,
- endDate,
- interval,
- databaseIdentifier,
- data,
- isVisible
- )
- const dailyStatsQueries = useProjectDailyStatsQueries(
- dailyStatsAttributes.map((attr) => attr.attribute as ProjectDailyStatsAttribute),
- ref,
- startDate,
- endDate,
- data,
- isVisible
- )
-
- let infraIdx = 0
- let dailyStatsIdx = 0
- return attributes
- .filter((attr) => attr.provider !== 'logs')
- .map((attr) => {
- if (attr.provider === 'infra-monitoring') {
- return {
- ...infraQueries[infraIdx++],
- data: { ...infraQueries[infraIdx - 1]?.data, provider: 'infra-monitoring' },
- }
- } else if (attr.provider === 'daily-stats') {
- return {
- ...dailyStatsQueries[dailyStatsIdx++],
- data: { ...dailyStatsQueries[dailyStatsIdx - 1]?.data, provider: 'daily-stats' },
- }
- } else if (attr.provider === 'reference-line') {
- let value = attr.value || 0
- return {
- data: {
- data: [],
- attribute: attr.attribute,
- total: value,
- maximum: value,
- totalGrouped: { [attr.attribute]: value },
- provider: 'reference-line',
- },
- isLoading: false,
- isError: false,
- }
- } else {
- return {
- isLoading: false,
- data: undefined,
- }
- }
- })
-}
-
-export default function LazyLogChartHandler(props: LogChartHandlerProps) {
- return (
- <LazyChartWrapper>
- <LogChartHandler {...props} />
- </LazyChartWrapper>
- )
-}