Files
Saxon FletcherandClaude Opus 5.5 7ee828f3f6 chore(studio): center Explorer query result states (#53437)
## What

The Explorer results area showed its idle and no-rows states as a small
line in the top-left corner, which was easy to miss, and showed nothing
while a query ran. These states now sit in the center of the results
area, with an icon above the label:

- **Before a run:** "Run the query to see results"
- **While running:** "Running query…" (new). When a re-run has previous
rows, they stay on screen, dimmed, under the running state, so notebook
cells don't collapse and grow back.
- **No rows:** "Success. No rows returned"

Assistant query cells also show the running state after you approve a
query, since that run happens outside the editor.

## Testing

Enable the `explorer` flag and open Explorer.

1. Open a new query tab and check that "Run the query to see results" is
centered.
2. Run `select pg_sleep(2)`. "Running query…" shows while it runs, then
the result appears.
3. Run `select 1 where false` and check that "Success. No rows returned"
is centered.
4. Run `select * from pg_tables`, then re-run it. The rows stay visible,
dimmed, under the running state.
5. In a notebook with several cells, click **Run notebook**. Cells keep
their height while they run.
6. Ask the Assistant to run a query and approve it. The cell shows
"Running query…" until the result arrives.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:54:25 +10:00

145 lines
3.9 KiB
TypeScript

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) => (
<div
className={cn(
'flex w-full flex-1 flex-col items-center justify-center gap-y-2 px-6 py-8 text-center',
className
)}
{...props}
>
<span className="text-foreground-muted" aria-hidden>
{icon}
</span>
<p className="text-sm text-foreground-light">{children}</p>
</div>
)
const RunningState = ({ className }: { className?: string }) => (
<QueryResultState
className={className}
icon={
<Loader2Icon
size={20}
strokeWidth={1.5}
className="animate-spin motion-reduce:animate-none"
/>
}
>
Running query…
</QueryResultState>
)
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 <RunningState />
if (!result) {
return (
<QueryResultState icon={<PlayIcon size={20} strokeWidth={1.5} />}>
Run the query to see results
</QueryResultState>
)
}
if (error) {
return (
<QueryResultError
error={error}
autoLimit={autoLimit}
sql={sql}
source={source}
onDebug={onDebug}
/>
)
}
if (!hasRows) {
return (
<QueryResultState icon={<CircleCheckIcon size={20} strokeWidth={1.5} />}>
Success. No rows returned
</QueryResultState>
)
}
const rowsContent = (
<>
{view === 'table' && <DataGridResults rows={rows} />}
{view === 'chart' && <QueryResultChart chart={chart} result={result} />}
</>
)
if (!isExecuting) return rowsContent
return (
<div className="relative flex min-h-0 w-full flex-1 flex-col">
<div className="pointer-events-none flex min-h-0 flex-1 flex-col opacity-40" aria-hidden>
{rowsContent}
</div>
<RunningState className="absolute inset-0" />
</div>
)
}
/**
* 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) => (
<>
<p role="status" className="sr-only">
{getRunAnnouncement(props)}
</p>
<QueryResultContent {...props} />
</>
)