Files
Joshen Lim fc3f6aaea8 Add source selector for explorer query tab (#49063)
## Context

This is just pulling out the relevant changes from
https://github.com/supabase/supabase/pull/49028 as I might have messed
up the stack while making changes down the PRs 🙏

Builds on the Query Tab in the Explorer UI, adds the source selector
component to run either a database query or a logs query - will
subsequently be looking into have the source selector component in the
QueryCell as well (within notebooks)

<img width="1095" height="594" alt="image"
src="https://github.com/user-attachments/assets/ab6e8d91-aed3-4845-9777-acbd7a3a7cb1"
/>
<img width="1086" height="667" alt="image"
src="https://github.com/user-attachments/assets/75d99063-755d-4ba6-a128-7ddcbbc9f161"
/>


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

## Summary by CodeRabbit

- **New Features**
  - Added query-source selection for Explorer and notebook queries.
- Supports log queries, database selection, and read-replica
connections.
- Added validation for custom log time ranges and retention limits, with
upgrade guidance when applicable.
  - Query source choices are saved and restored across sessions.
  - Changing sources clears previous results to prevent stale data.

- **Bug Fixes**
- Improved handling of unavailable log querying and missing database
connections.
- Legacy saved queries now fall back safely to the default database
source.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-14 01:07:20 +07:00

92 lines
3.0 KiB
TypeScript

import { useParams } from 'common'
import { Loader2, SquareCode } from 'lucide-react'
import { useRouter } from 'next/router'
import { useContext, useEffect, useState } from 'react'
import { Button } from 'ui'
import { QueryEditor } from './QueryEditor'
import { type QueryResult } from './types'
import { explorerQueryState, useExplorerQueryStateSnapshot } from '@/state/explorer-query'
import { createTabId, TabsStateContext } from '@/state/tabs'
const QUERY_ROW_LIMIT = 100
/** Query-tab lifecycle adapter around the shared QueryEditor. */
export const QueryTab = () => {
const { id, ref } = useParams()
const router = useRouter()
const tabs = useContext(TabsStateContext)
const querySnap = useExplorerQueryStateSnapshot()
const [hasRestored, setHasRestored] = useState(false)
const stateDraft = id ? querySnap.drafts[id] : undefined
const draft = stateDraft?.projectRef === ref ? stateDraft : undefined
const result = draft && id ? querySnap.results[id] : undefined
useEffect(() => {
if (!id || !ref) return
const restored = explorerQueryState.restoreDraft({ id, projectRef: ref })
const restoredDraft = explorerQueryState.drafts[id]
if (restored && restoredDraft) {
tabs.addTab({
id: createTabId('query', { id }),
type: 'query',
label: restoredDraft.name,
metadata: { queryId: id },
isPreview: false,
})
}
setHasRestored(true)
}, [id, ref, tabs])
if (!hasRestored) {
return (
<div className="flex h-full items-center justify-center bg-surface-100">
<Loader2 className="animate-spin text-foreground-muted" size={18} />
</div>
)
}
if (!id || !draft) {
return (
<div className="flex h-full flex-col items-center justify-center gap-3 bg-surface-100">
<SquareCode className="text-foreground-muted" size={24} />
<div className="text-center">
<p className="text-sm font-medium">Query draft not found</p>
<p className="text-sm text-foreground-lighter">
This local draft may have been closed or cleared from this browser.
</p>
</div>
<Button onClick={() => router.push(`/project/${ref}/explorer`)}>Back to Explorer</Button>
</div>
)
}
const handleResultChange = (nextResult: QueryResult) => {
explorerQueryState.setResult({
id,
result: { ...nextResult, executedAt: Date.now() },
})
}
return (
<QueryEditor
id={id}
variant="viewport"
title={draft.name}
sql={draft.uncheckedSql}
source={draft.source}
result={result}
rowLimit={QUERY_ROW_LIMIT}
onTitleChange={(value) => {
const name = value.trim() || 'Untitled query'
explorerQueryState.updateDraft({ id, name })
tabs.updateTab(createTabId('query', { id }), { label: name })
}}
onSqlChange={(sql) => explorerQueryState.updateDraft({ id, sql })}
onSourceChange={(source) => explorerQueryState.updateDraft({ id, source })}
onResultChange={handleResultChange}
/>
)
}