Files
supabase/apps/studio/components/interfaces/Explorer/ExplorerQuerySourceMenu.tsx
T
Charis 0ed49231b7 refactor(studio): unify CellSource and the SQL editor's QuerySource into QuerySourceBinding (#49072)
Third of a stack. **Stacked on #49070** (which is stacked on #49069) —
review those first. Base retargets automatically as each merges.

Mechanical throughout; no behavior change.

## The problem

Three types described where a query runs, and no two agreed:

| | shape |
|---|---|
| `CellSource` (registry) | `{ id, type, parameters: { … } }` — `id` and
`type` always held the same literal |
| `QuerySource` (SQL editor) | `{ type: 'database' } \| { type: 'logs',
dateRange }` |
| notebook cells | flat per-backend fields, neither of the above |

Anything crossing between them needed a translation that dropped fields
on the way — which is how a notebook cell's replica selection had
nowhere to go.

## What changed

One `QuerySourceBinding`: a backend `_tag` with that backend's
parameters spread flat beside it, borrowed from the wire schema (#49069)
so the binding and the persisted cell agree by construction.

- **`QuerySource` is deleted.** `useRunSource` returns the shared
binding, so `runSource.type`/`dateRange` become `_tag`/`time_range`
across the SQL editor — that is most of the file count here.
- **`getQuerySourceBinding`** projects a notebook cell onto a binding;
**`toQuerySourceBinding`** does the same for any backend-tagged carrier.
Both overloaded so an already-narrowed caller gets the matching binding
back rather than the union, which keeps the result spreadable without
re-narrowing.
- **`ExplorerQuerySourceMenu`** drops its inline copy of the
custom-range and upgrade-prompt logic in favor of `useLogsCustomRange`,
which the SQL editor menu already used.

The registry keeps only what is genuinely runtime: endpoints, labels,
icons, availability, defaults. What a query *is* stays in the wire
schema.

## Verification

Typecheck, Prettier, and the lint ratchet clean. 405 tests pass across
the notebook schema, query sources, the logs components, the SQL editor,
and the Explorer surfaces.

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

* **Improvements**
* Updated query source handling across Explorer and SQL Editor for a
more consistent selection experience.
* Database and log sources now preserve identifiers and time ranges more
reliably when switching or editing queries.
* Source menus, labels, icons, validation, and query execution now
reflect the selected source more accurately.

* **Bug Fixes**
* Invalid or outdated saved source settings now safely fall back to a
database source.
* Improved log-source detection and time-range handling throughout query
editing and execution.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-08-14 15:09:29 +07:00

130 lines
4.5 KiB
TypeScript

import { useFlag, useParams } from 'common'
import { Check, ChevronDown } from 'lucide-react'
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from 'ui'
import { DatabaseParametersSubMenu } from '@/components/interfaces/QuerySources/DatabaseParametersSubMenu'
import { LogsCustomRangeDialog } from '@/components/interfaces/QuerySources/LogsCustomRangeDialog'
import { LogsTimeRangeSubMenu } from '@/components/interfaces/QuerySources/LogsTimeRangeSubMenu'
import { QuerySourceIcon } from '@/components/interfaces/QuerySources/QuerySourceIcon'
import { useLogsCustomRange } from '@/components/interfaces/QuerySources/useLogsCustomRange'
import UpgradePrompt from '@/components/interfaces/Settings/Logs/UpgradePrompt'
import {
createDefaultSourceBinding,
QUERY_SOURCE_LABELS,
QUERY_SOURCES,
type QuerySourceBinding,
} from '@/data/query-sources/query-source-registry'
export type ExplorerQuerySourceMenuProps = {
source: QuerySourceBinding
onSourceChange: (source: QuerySourceBinding) => void
}
/**
* Source binding and parameter controls shared by standalone Explorer queries
* and notebook query-cell toolbars. The consumer owns the binding; this menu
* only emits complete, validated-by-construction `QuerySourceBinding` values.
*
* Selecting a different backend emits that backend's default binding — deciding
* what happens to the query body is the consumer's call, since a notebook cell
* has SQL to preserve or discard and a fresh draft does not.
*/
export const ExplorerQuerySourceMenu = ({
source,
onSourceChange,
}: ExplorerQuerySourceMenuProps) => {
const { ref } = useParams()
const isLogsSourceEnabled = useFlag('sqlEditorLogsSource')
const isOtelLogsEnabled = useFlag('otelLegacyLogs')
const {
isCustomRangeOpen,
setIsCustomRangeOpen,
showUpgradePrompt,
setShowUpgradePrompt,
handleApplyCustomRange,
} = useLogsCustomRange({
onRangeChange: (time_range) => onSourceChange({ _tag: 'logs', time_range }),
})
const availableSources = QUERY_SOURCES.filter(
(candidate) =>
candidate._tag !== 'logs' ||
(isLogsSourceEnabled && isOtelLogsEnabled) ||
source._tag === 'logs'
)
return (
<>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="text"
size="tiny"
aria-label={`Query source: ${QUERY_SOURCE_LABELS[source._tag]}`}
icon={<QuerySourceIcon source={source._tag} className="text-foreground-light" />}
iconRight={<ChevronDown className="text-foreground-light" />}
>
{QUERY_SOURCE_LABELS[source._tag]}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-60">
{availableSources.map((candidate) => (
<DropdownMenuItem
key={candidate._tag}
className="justify-between"
onSelect={(event) => {
event.preventDefault()
if (candidate._tag !== source._tag) {
onSourceChange(createDefaultSourceBinding(candidate._tag))
}
}}
>
<span className="flex items-center gap-x-2">
<QuerySourceIcon source={candidate._tag} className="text-foreground-light" />
{QUERY_SOURCE_LABELS[candidate._tag]}
</span>
{source._tag === candidate._tag && <Check size={14} />}
</DropdownMenuItem>
))}
<DropdownMenuSeparator />
{source._tag === 'database' ? (
<DatabaseParametersSubMenu
identifier={source.database_identifier ?? ref}
onIdentifierChange={(database_identifier) =>
onSourceChange({ _tag: 'database', database_identifier })
}
/>
) : (
<LogsTimeRangeSubMenu
range={source.time_range}
onRangeChange={(time_range) => onSourceChange({ _tag: 'logs', time_range })}
onOpenCustomRange={() => setIsCustomRangeOpen(true)}
onShowUpgrade={() => setShowUpgradePrompt(true)}
/>
)}
</DropdownMenuContent>
</DropdownMenu>
{source._tag === 'logs' && (
<>
<LogsCustomRangeDialog
open={isCustomRangeOpen}
onOpenChange={setIsCustomRangeOpen}
onApply={handleApplyCustomRange}
/>
<UpgradePrompt show={showUpgradePrompt} setShowUpgradePrompt={setShowUpgradePrompt} />
</>
)}
</>
)
}