mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 09:55:06 +03:00
## Context Just reorganizing the files under the Explorer folder as details are a bit more clearer Mainly shifting related and exclusive files into their own folder and tests into `__tests__` folder + renaming some files <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added query result display settings for switching between table and chart views. - Added chart configuration options for chart type, axes, scaling, cumulative mode, and labels. - Automatically prevents invalid logarithmic scaling when chart data is incompatible. - **Refactor** - Standardized Explorer tab and query source naming across the interface without changing existing behavior. - Updated Explorer navigation, routing, and page wiring to use the standardized components. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
105 lines
3.4 KiB
TypeScript
105 lines
3.4 KiB
TypeScript
import { useMemo } from 'react'
|
|
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
|
|
}
|
|
|
|
// [Joshen] Will need to implement log scale - refer to QueryBlock.tsx `effectiveLogScale`
|
|
// [Joshen] Will also need to implement error handling where appropriate (e.g if query errors)
|
|
|
|
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_columns = [], cumulative, show_labels, scale } = chart ?? {}
|
|
|
|
const hasConfig = !!x_column && y_columns.length > 0
|
|
const chartRows = useMemo(() => {
|
|
const xKey = x_column ?? ''
|
|
const yKey = y_columns[0] ?? ''
|
|
return (result?.rows ?? []).map((row) => ({
|
|
[xKey]: toChartValue(row[xKey]),
|
|
[yKey]: toChartValue(row[yKey]),
|
|
}))
|
|
}, [result, x_column, y_columns])
|
|
|
|
const cumulativeResults = useMemo(
|
|
() => getCumulativeResults({ rows: chartRows }, { yKey: y_columns[0] ?? '' }),
|
|
[chartRows, y_columns]
|
|
)
|
|
const resultToRender = cumulative ? cumulativeResults : chartRows
|
|
|
|
if (!result || (result?.rows && result.rows.length === 0)) {
|
|
return (
|
|
<NoDataPlaceholder
|
|
className="bg border-0"
|
|
size="normal"
|
|
message="No results"
|
|
description="Your query returned no rows"
|
|
/>
|
|
)
|
|
}
|
|
|
|
if (!hasConfig) {
|
|
return (
|
|
<NoDataPlaceholder
|
|
className="bg border-0"
|
|
size="normal"
|
|
message="Configure your chart"
|
|
description="Select your X and Y axis in the display settings"
|
|
/>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<Chart>
|
|
<ChartCard className="rounded-none border-0">
|
|
<ChartContent>
|
|
<div className="h-40">
|
|
{type === 'bar' && (
|
|
<ChartBar
|
|
isFullHeight
|
|
xKey={x_column}
|
|
dataKey={y_columns[0]}
|
|
showXAxis={show_labels}
|
|
showYAxis={show_labels}
|
|
data={resultToRender}
|
|
YAxisProps={{
|
|
scale: scale === 'log' ? 'log' : 'auto',
|
|
domain: scale === 'log' ? [1, 'auto'] : undefined,
|
|
tickFormatter: scale === 'log' ? formatLogTick : undefined,
|
|
}}
|
|
/>
|
|
)}
|
|
{type === 'line' && (
|
|
<ChartLine
|
|
isFullHeight
|
|
xKey={x_column}
|
|
dataKey={y_columns[0]}
|
|
showXAxis={show_labels}
|
|
showYAxis={show_labels}
|
|
data={resultToRender}
|
|
YAxisProps={{
|
|
scale: scale === 'log' ? 'log' : 'auto',
|
|
domain: scale === 'log' ? [1, 'auto'] : undefined,
|
|
tickFormatter: scale === 'log' ? formatLogTick : undefined,
|
|
}}
|
|
/>
|
|
)}
|
|
</div>
|
|
</ChartContent>
|
|
</ChartCard>
|
|
</Chart>
|
|
)
|
|
}
|