Files
supabase/apps/studio/components/interfaces/Explorer/QueryEditor/QueryResultRenderer.tsx
T
Joshen Lim 8ac64a4349 Add copy notebook as markdown action (#50200)
## Context

Adds a "Copy as Markdown" CTA for notebooks
<img width="265" height="198" alt="image"
src="https://github.com/user-attachments/assets/5eccf36e-24ea-4d2f-b1cf-72d5353b70a2"
/>

Query cell titles will be rendered as h3 tags and labelled either
Postgres or Logs - Clickhouse (with time range)
The query content will then be rendered as triple backticks with `sql`
e.g `
```sql...```
`

Query results will be copied to markdown if the query has been run, will otherwise be omitted
Also, if the query was updated (e.g content, source, etc) after it was run (as the result is hence stale), result will also be omitted
e.g:
| Notebook | Markdown |
| --- | --- |
| <img width="1291" height="630" alt="image" src="https://github.com/user-attachments/assets/c49f23e5-c70b-46dd-a298-6e1a90cd30d7" /> | <img width="731" height="536" alt="image" src="https://github.com/user-attachments/assets/4bbe3041-bd8e-42d3-86d2-1691e7a6bc7b" /> |
| <img width="1220" height="832" alt="image" src="https://github.com/user-attachments/assets/67de523a-18f7-4f1b-b764-7f0f3152f9e7" /> | <img width="757" height="803" alt="image" src="https://github.com/user-attachments/assets/d217504b-bee6-4088-9049-87ff647b9bc7" /> |



<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **New Features**
  * Added Markdown export for notebook queries, results, errors, and time ranges.
  * Added error notifications when copying notebook content fails.

* **Bug Fixes**
  * Prevented stale query results after relevant source or time-range changes.
  * Improved Markdown export for queries containing backticks.
  * Escaped backslashes, pipes, and line breaks in Markdown tables.

* **Style**
  * Adjusted spacing for empty query-result messages.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-11 11:25:22 +08:00

62 lines
1.6 KiB
TypeScript

import { Play } from 'lucide-react'
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
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
}
export const QueryResultRenderer = ({
result,
view,
chart,
sql,
source,
onDebug,
}: QueryResultRendererProps) => {
const { rows, error, autoLimit } = result ?? {}
if (!result) {
return (
<div className="flex items-center gap-x-2 p-3 w-full">
<Play size={12} className="text-foreground-lighter" />
<p className="text-xs text-foreground-lighter">Run the query to see results</p>
</div>
)
}
if (error) {
return (
<QueryResultError
error={error}
autoLimit={autoLimit}
sql={sql}
source={source}
onDebug={onDebug}
/>
)
}
if ((rows ?? []).length === 0) {
return <p className="text-xs text-foreground-lighter p-3">Success. No rows returned</p>
}
if (rows && rows.length > 0) {
if (view === 'table') return <DataGridResults rows={rows} />
if (view === 'chart') return <QueryResultChart chart={chart} result={result} />
}
return null
}