Files
supabase/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultChart.tsx
T
Saxon FletcherandJoshen Lim de2a7d8d9e Add view options to Query (#49447)
Currently Query tabs in explorer do not support view options e.g. table
vs chart. This adds display state to query tabs to match the behaviour
of Notebooks

## To test
- create a query in explorer
- Run a query
- Set the display options via toolbar

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
  * Query results can now be displayed as either a table or chart.
* Chart settings are saved with each query draft and restored when
reopened.
* Display preferences are maintained independently across query drafts.
  * Editing a preview query now converts it into a permanent tab.
* **Bug Fixes**
* Invalid or legacy display settings safely fall back to the table view
without removing saved drafts.
* Charts and empty states now use the available editor space more
effectively.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-08-24 16:19:17 +08:00

128 lines
4.1 KiB
TypeScript

import { useMemo } from 'react'
import { type ChartConfig as ChartSeriesConfig } from 'ui'
import { Chart, ChartBar, ChartCard, ChartContent, ChartLine } from 'ui-patterns/Chart'
import { type QueryResult } from '../types'
import NoDataPlaceholder from '@/components/ui/Charts/NoDataPlaceholder'
import { formatLogTick, getCumulativeResults } from '@/components/ui/QueryBlock/QueryBlock.utils'
import { type ChartConfig } from '@/data/content/notebooks/notebook-schema'
interface QueryResultChartProps {
chart?: ChartConfig
result?: QueryResult
}
const Y_SERIES_COLORS = [
'hsl(var(--brand-default))',
'hsl(var(--chart-blue))',
'hsl(var(--chart-3))',
]
const toChartValue = (value: unknown): string | number => {
if (typeof value === 'number' || typeof value === 'string') return value
if (value === null || value === undefined) return ''
return String(value)
}
export const QueryResultChart = ({ chart, result }: QueryResultChartProps) => {
const { type, x_column, y_series = [], cumulative, show_labels, scale } = chart ?? {}
const hasConfig = !!x_column && y_series.length > 0
// Logarithmic scale only makes sense for a single series — DisplaySettingsButton
// resets `scale` to linear once a second Y column is added
const effectiveScale = y_series.length > 1 ? 'linear' : scale
const chartConfig: ChartSeriesConfig = useMemo(
() =>
y_series.reduce((acc, key, index) => {
acc[key] = { label: key, color: Y_SERIES_COLORS[index] }
return acc
}, {} as ChartSeriesConfig),
[y_series]
)
const chartRows = useMemo(() => {
const xKey = x_column ?? ''
return (result?.rows ?? []).map((row) => {
const chartRow: Record<string, string | number> = { [xKey]: toChartValue(row[xKey]) }
y_series.forEach((yKey) => {
chartRow[yKey] = toChartValue(row[yKey])
})
return chartRow
})
}, [result, x_column, y_series])
const cumulativeResults = useMemo(
() => getCumulativeResults({ rows: chartRows }, { yKey: y_series }),
[chartRows, y_series]
)
const resultToRender = cumulative ? cumulativeResults : chartRows
if (!result || (result?.rows && result.rows.length === 0)) {
return (
<NoDataPlaceholder
isFullHeight
className="border-0"
size="normal"
message="No results"
description="Your query returned no rows"
/>
)
}
if (!hasConfig) {
return (
<NoDataPlaceholder
isFullHeight
className="border-0"
size="normal"
message="Configure your chart"
description="Select your X and Y axis in the display settings"
/>
)
}
return (
<Chart className="flex flex-grow min-h-0">
<ChartCard className="flex flex-grow rounded-none border-0 min-h-0">
<ChartContent className="min-h-0 w-full">
{type === 'bar' && (
<ChartBar
isFullHeight
xKey={x_column}
dataKey={y_series[0]}
dataKeys={y_series}
config={chartConfig}
showXAxis={show_labels}
showYAxis={show_labels}
data={resultToRender}
YAxisProps={{
scale: effectiveScale === 'log' ? 'log' : 'auto',
domain: effectiveScale === 'log' ? [1, 'auto'] : undefined,
tickFormatter: effectiveScale === 'log' ? formatLogTick : undefined,
}}
/>
)}
{type === 'line' && (
<ChartLine
isFullHeight
xKey={x_column}
dataKey={y_series[0]}
dataKeys={y_series}
config={chartConfig}
showXAxis={show_labels}
showYAxis={show_labels}
data={resultToRender}
YAxisProps={{
scale: effectiveScale === 'log' ? 'log' : 'auto',
domain: effectiveScale === 'log' ? [1, 'auto'] : undefined,
tickFormatter: effectiveScale === 'log' ? formatLogTick : undefined,
}}
/>
)}
</ChartContent>
</ChartCard>
</Chart>
)
}