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 { type Snapshot } from 'valtio' import { ExplorerToolbarAction } from '../ExplorerToolbar' import { type QueryResult } from '../types' import { checkHasNonPositiveValues } from '@/components/ui/QueryBlock/QueryBlock.utils' import { type DatabaseCell as DatabaseCellSchema } from '@/data/content/notebooks/notebook-schema' import { useCurrentNotebook, useNotebooksStateSnapshot } from '@/state/notebooks/notebooks-state' interface DisplaySettingsButtonProps { cell: Snapshot result?: QueryResult columns: string[] disabled: boolean } // [Joshen] TODO support multiple y axis charts export const DisplaySettingsButton = ({ cell, result, columns, disabled, }: DisplaySettingsButtonProps) => { const snap = useNotebooksStateSnapshot() const currentNotebook = useCurrentNotebook() const cells = currentNotebook?.notebook.content?.cells ?? [] const { view, chart } = cell 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') => { const notebookId = currentNotebook?.notebook.id if (!notebookId) return const nextCells = cells.map((c) => c.id === cell.id && c._tag === 'database_cell' ? { ...c, view } : c ) snap.updateCells({ id: notebookId, cells: nextCells }) } const onUpdateChartConfig = ( payload: | { type: 'bar' | 'line' } | { x_column: string } | { y_columns: string[] } | { cumulative: boolean } | { show_labels: boolean } | { scale: 'linear' | 'log' } ) => { const notebookId = currentNotebook?.notebook.id if (!notebookId) return const nextCells = cells.map((c) => { if (c.id !== cell.id || c._tag !== 'database_cell') return c return { ...c, chart: { type: c.chart?.type ?? 'bar', x_column: c.chart?.x_column ?? '', y_columns: c.chart?.y_columns ?? [], cumulative: c.chart?.cumulative ?? false, scale: c.chart?.scale ?? 'linear', show_labels: c.chart?.show_labels ?? false, ...payload, }, } }) snap.updateCells({ id: notebookId, cells: nextCells }) } const resetToLinearScale = useEffectEvent(() => { onUpdateChartConfig({ scale: 'linear' }) }) useEffect(() => { if (hasNonPositiveValues && scale === 'log') { resetToLinearScale() } }, [hasNonPositiveValues, scale]) return ( } tooltip="Result settings" />

Result display settings

{ if (value === 'table' || value === 'chart') onChangeView(value) }} variant="outline" type="single" >

Table

Chart

{view === 'chart' && ( <> { if (value === 'bar' || value === 'line') onUpdateChartConfig({ type: value }) }} variant="outline" type="single" > Bar Line
onUpdateChartConfig({ cumulative: Boolean(cumulative) }) } /> onUpdateChartConfig({ show_labels: Boolean(show_labels) }) } />
)} ) }