import { CircleCheckIcon, Loader2Icon, PlayIcon } from 'lucide-react' import type { ComponentProps, ReactNode } from 'react' import { cn } from 'ui' import { type SqlSnippetSource } from '../../SQLEditor/querySource' import { type QueryResult } from '../types' import { QueryResultChart } from './QueryResultChart' import { QueryResultError } from './QueryResultError' import { DataGridResults } from '@/components/ui/DataGridResults' import { type ChartConfig } from '@/data/content/notebooks/notebook-schema' interface QueryResultRendererProps { result?: QueryResult /** * While a run is in flight the results region shows a running state. Previous rows stay * visible underneath, dimmed, so notebook cells keep their height instead of collapsing. */ isExecuting?: boolean view?: 'table' | 'chart' chart?: ChartConfig /** The query that produced `result`, used to build the "Debug with Assistant" prompt on error. */ sql?: string source?: SqlSnippetSource onDebug?: (prompt: string) => void } interface QueryResultStateProps extends ComponentProps<'div'> { icon: ReactNode children: ReactNode } /** Centered icon-over-label state for the results region (idle, running, empty). */ const QueryResultState = ({ icon, children, className, ...props }: QueryResultStateProps) => (
{icon}

{children}

) const RunningState = ({ className }: { className?: string }) => ( } > Running query… ) const getRunAnnouncement = ({ result, isExecuting }: QueryResultRendererProps) => { if (isExecuting) return 'Running query…' if (!result) return '' if (result.error) return 'Query failed' const rowCount = result.rows?.length ?? 0 if (rowCount === 0) return 'No rows returned' return `${rowCount} ${rowCount === 1 ? 'row' : 'rows'} returned` } const QueryResultContent = ({ result, isExecuting = false, view, chart, sql, source, onDebug, }: QueryResultRendererProps) => { const { rows, error, autoLimit } = result ?? {} const hasRows = rows !== undefined && rows.length > 0 if (isExecuting && !hasRows) return if (!result) { return ( }> Run the query to see results ) } if (error) { return ( ) } if (!hasRows) { return ( }> Success. No rows returned ) } const rowsContent = ( <> {view === 'table' && } {view === 'chart' && } ) if (!isExecuting) return rowsContent return (
{rowsContent}
) } /** * The visible states swap whole elements, so screen readers get run progress from one * status region that stays mounted while only its text changes. */ export const QueryResultRenderer = (props: QueryResultRendererProps) => ( <>

{getRunAnnouncement(props)}

)