mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03:00
## Stack Depends on #49027. Followed by #49038. ## Summary - extract a controlled `QueryEditor` from the existing notebook query cell - reuse it from `QueryCell`, leaving notebook persistence and sortable-block behavior in the adapter - make table/chart result settings controlled so other query surfaces can share them - persist notebook SQL on editor blur and query execution ## To test 1. Open a notebook query cell, edit and rename it, then run the query and confirm results appear. 2. Switch between table and chart results and confirm notebook move/delete actions still work. ## Why Notebooks, query tabs, and future chat tabs need consistent query actions and result rendering without duplicating the notebook implementation. ## Impact This is primarily a refactor of the existing notebook query experience. It introduces no new query-tab routes or source-selection behavior. ## Validation - fresh non-incremental Studio TypeScript check - focused NotebookEditor component tests <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a shared query editor with SQL editing, execution, validation, visibility controls, editable titles, row limits, and loading/error states. * Added table and chart result views, including customizable bar and line charts. * Added support for switching display modes and updating chart settings. * **Improvements** * Improved query result handling and display-setting updates. * Repositioned the logarithmic-scale tooltip for better visibility. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
248 lines
8.2 KiB
TypeScript
248 lines
8.2 KiB
TypeScript
import { BarChart2, Settings2, Table } from 'lucide-react'
|
|
import { useEffect, useEffectEvent, useMemo } from 'react'
|
|
import {
|
|
Checkbox,
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverSeparator,
|
|
PopoverTrigger,
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
ToggleGroup,
|
|
ToggleGroupItem,
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipTrigger,
|
|
} from 'ui'
|
|
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
|
|
|
|
import { ExplorerToolbarAction } from '../ExplorerToolbar'
|
|
import { type QueryChartConfig, type QueryDisplay, type QueryResult } from '../types'
|
|
import { checkHasNonPositiveValues } from '@/components/ui/QueryBlock/QueryBlock.utils'
|
|
|
|
interface DisplaySettingsButtonProps {
|
|
display: QueryDisplay
|
|
result?: QueryResult
|
|
columns: string[]
|
|
disabled: boolean
|
|
onChange: (display: QueryDisplay) => void
|
|
}
|
|
|
|
// [Joshen] TODO support multiple y axis charts
|
|
|
|
export const DisplaySettingsButton = ({
|
|
display,
|
|
result,
|
|
columns,
|
|
disabled,
|
|
onChange,
|
|
}: DisplaySettingsButtonProps) => {
|
|
const { view, chart } = display
|
|
const {
|
|
type = 'bar',
|
|
x_column,
|
|
y_columns = [],
|
|
cumulative = false,
|
|
show_labels = false,
|
|
scale = 'linear',
|
|
} = chart ?? {}
|
|
|
|
const hasNonPositiveValues = useMemo(
|
|
() => checkHasNonPositiveValues(result?.rows ?? [], y_columns[0]),
|
|
[result, y_columns]
|
|
)
|
|
|
|
const canToggleLogScale = useMemo(() => {
|
|
if (y_columns.length === 0 || !result || (result.rows ?? []).length === 0) return false
|
|
return !hasNonPositiveValues
|
|
}, [hasNonPositiveValues, result, y_columns.length])
|
|
|
|
const onChangeView = (view: 'table' | 'chart') => {
|
|
onChange({ ...display, view })
|
|
}
|
|
|
|
const onUpdateChartConfig = (payload: Partial<QueryChartConfig>) => {
|
|
onChange({
|
|
...display,
|
|
chart: {
|
|
type: chart?.type ?? 'bar',
|
|
x_column: chart?.x_column ?? '',
|
|
y_columns: chart?.y_columns ?? [],
|
|
cumulative: chart?.cumulative ?? false,
|
|
scale: chart?.scale ?? 'linear',
|
|
show_labels: chart?.show_labels ?? false,
|
|
...payload,
|
|
},
|
|
})
|
|
}
|
|
|
|
const resetToLinearScale = useEffectEvent(() => {
|
|
onUpdateChartConfig({ scale: 'linear' })
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (hasNonPositiveValues && scale === 'log') {
|
|
resetToLinearScale()
|
|
}
|
|
}, [hasNonPositiveValues, scale])
|
|
|
|
return (
|
|
<Popover>
|
|
<PopoverTrigger asChild>
|
|
<ExplorerToolbarAction disabled={disabled} icon={<Settings2 />} tooltip="Result settings" />
|
|
</PopoverTrigger>
|
|
<PopoverContent side="bottom" className="flex flex-col gap-y-3 p-0 py-3 mr-8">
|
|
<div className="flex flex-col gap-y-3 px-3">
|
|
<p className="text-xs tracking-tighter uppercase font-mono text-foreground-lighter">
|
|
Result display settings
|
|
</p>
|
|
|
|
<FormItemLayout isReactForm={false} label="View data as">
|
|
<ToggleGroup
|
|
value={view}
|
|
onValueChange={(value) => {
|
|
if (value === 'table' || value === 'chart') onChangeView(value)
|
|
}}
|
|
variant="outline"
|
|
type="single"
|
|
>
|
|
<ToggleGroupItem value="table" className="w-full gap-x-2 data-[state=on]:bg-accent">
|
|
<Table size={14} />
|
|
<p>Table</p>
|
|
</ToggleGroupItem>
|
|
<ToggleGroupItem value="chart" className="w-full gap-x-2 data-[state=on]:bg-accent">
|
|
<BarChart2 size={14} />
|
|
<p>Chart</p>
|
|
</ToggleGroupItem>
|
|
</ToggleGroup>
|
|
</FormItemLayout>
|
|
</div>
|
|
|
|
{view === 'chart' && (
|
|
<>
|
|
<PopoverSeparator />
|
|
|
|
<FormItemLayout isReactForm={false} label="Render chart as" className="px-3 mb-1">
|
|
<ToggleGroup
|
|
value={type}
|
|
onValueChange={(value) => {
|
|
if (value === 'bar' || value === 'line') onUpdateChartConfig({ type: value })
|
|
}}
|
|
variant="outline"
|
|
type="single"
|
|
>
|
|
<ToggleGroupItem value="bar" className="w-full data-[state=on]:bg-accent">
|
|
Bar
|
|
</ToggleGroupItem>
|
|
<ToggleGroupItem value="line" className="w-full data-[state=on]:bg-accent">
|
|
Line
|
|
</ToggleGroupItem>
|
|
</ToggleGroup>
|
|
</FormItemLayout>
|
|
|
|
<div className="px-3 flex flex-col gap-y-2">
|
|
<FormItemLayout isReactForm={false} layout="flex-row-reverse" label="X axis">
|
|
<Select
|
|
value={x_column}
|
|
onValueChange={(x_column) => onUpdateChartConfig({ x_column })}
|
|
>
|
|
<SelectTrigger className="w-32">
|
|
<SelectValue placeholder="Select a column" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{columns.map((x) => (
|
|
<SelectItem key={x} value={x}>
|
|
{x}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</FormItemLayout>
|
|
|
|
<FormItemLayout isReactForm={false} layout="flex-row-reverse" label="Y Axis">
|
|
<Select
|
|
value={y_columns?.[0]}
|
|
onValueChange={(y_column) => onUpdateChartConfig({ y_columns: [y_column] })}
|
|
>
|
|
<SelectTrigger className="w-32">
|
|
<SelectValue placeholder="Select a column" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{columns.map((x) => (
|
|
<SelectItem key={x} value={x}>
|
|
{x}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</FormItemLayout>
|
|
|
|
<FormItemLayout isReactForm={false} layout="flex-row-reverse" label="Scale">
|
|
<Select
|
|
value={scale}
|
|
onValueChange={(scale) =>
|
|
onUpdateChartConfig({ scale: scale as 'log' | 'linear' })
|
|
}
|
|
>
|
|
<SelectTrigger className="w-32">
|
|
<SelectValue placeholder="Select a column" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="linear">Linear</SelectItem>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<SelectItem
|
|
disabled={!canToggleLogScale}
|
|
value="log"
|
|
className={!canToggleLogScale ? '!pointer-events-auto' : undefined}
|
|
>
|
|
Logarithmic
|
|
</SelectItem>
|
|
</TooltipTrigger>
|
|
{!canToggleLogScale && (
|
|
<TooltipContent side="left">
|
|
{y_columns.length === 0
|
|
? 'Select a column for the Y axis first'
|
|
: 'Data contains zero or negative values'}
|
|
</TooltipContent>
|
|
)}
|
|
</Tooltip>
|
|
</SelectContent>
|
|
</Select>
|
|
</FormItemLayout>
|
|
|
|
<FormItemLayout id="cumulative" isReactForm={false} layout="flex" label="Cumulative">
|
|
<Checkbox
|
|
id="cumulative"
|
|
checked={cumulative}
|
|
onCheckedChange={(cumulative) =>
|
|
onUpdateChartConfig({ cumulative: Boolean(cumulative) })
|
|
}
|
|
/>
|
|
</FormItemLayout>
|
|
|
|
<FormItemLayout
|
|
id="show_labels"
|
|
isReactForm={false}
|
|
layout="flex"
|
|
label="Show labels"
|
|
>
|
|
<Checkbox
|
|
id="show_labels"
|
|
checked={show_labels}
|
|
onCheckedChange={(show_labels) =>
|
|
onUpdateChartConfig({ show_labels: Boolean(show_labels) })
|
|
}
|
|
/>
|
|
</FormItemLayout>
|
|
</div>
|
|
</>
|
|
)}
|
|
</PopoverContent>
|
|
</Popover>
|
|
)
|
|
}
|