Files
supabase/apps/studio/state/sql-editor/sql-editor-session-state.ts
Saxon Fletcher cc6fe2100a refactor(studio): centralize query sources (#49027)
## Summary

- define application-owned database and logs source contracts, defaults,
validation, labels, and execution endpoints
- extract controlled database and logs parameter controls for reuse
outside SQL snippets
- adapt the SQL editor to the shared source model without changing
snippet behavior
- standardize source icons at 16px with a 2px stroke
- keep relative logs ranges aligned with the existing date picker units

## To test

1. Open an existing query in the SQL Editor and run it against the
database.
2. Switch the query source to Logs, change the time range, and confirm
the query still runs as expected.

## Why

Explorer queries and notebook query cells need to select an execution
source without coupling that source to SQL snippets. This provides the
shared registry and controlled UI foundation for those consumers.

## Impact

Existing SQL snippets retain their current database/logs routing and
session behavior. The registry documents the SQL editor legacy
database-selector adapter while new consumers own their identifier
inline. The shared Logs date picker remains unchanged; query ranges
support its existing minute, hour, and day units. This PR does not add
the Explorer query tab itself.

## Validation

- pnpm --filter studio typecheck
- focused Vitest coverage for the registry, canonical log-range
utilities, SQL execution adapters, source filtering, retention locking,
custom ranges, and preset selection
- pnpm --filter studio run lint:ratchet

Component and state tests cover this change per the Studio testing
guidance; no E2E test is added.

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

* **New Features**
  * Added a unified query-source menu for database queries and logs.
* Added custom log time-range selection with calendar support and
retention-aware upgrade prompts.
* Added consistent source icons and improved database selection
handling.
  * Added support for relative and absolute log time ranges.

* **Bug Fixes**
  * Improved log-range validation, defaults, and current-time handling.
* Updated query execution to use the correct source-specific endpoints.

* **Tests**
* Expanded coverage for query sources, log ranges, menus, and retention
behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-13 16:51:06 +07:00

74 lines
2.8 KiB
TypeScript

import { proxy, ref, snapshot, useSnapshot } from 'valtio'
import type { LogTimeRange } from '@/data/query-sources/query-source-registry'
/**
* Ephemeral, per-session SQL editor state that is NOT persisted: query results,
* the row limit, and the per-snippet logs time range. Kept separate from the
* snippet/folder store (which deals with persistence) because none of this is
* saved — it lives only for the current editing session.
*/
export const sqlEditorSessionState = proxy({
/**
* Query results, if any, keyed by snippet id. An array per id as we once
* experimented with a notebook-style multi-result UI; the shape is kept since
* a single query with multiple statements can return multiple results.
*/
results: {} as {
[snippetId: string]: {
rows: any[]
error?: any
autoLimit?: number
}[]
},
/**
* UI-imposed limit for the number of rows a query can return (a safeguard
* against accidentally taking down the database with a huge SELECT). Related
* to `autoLimit` in `results`; see `applyAutoLimit`.
*/
limit: 100,
setLimit: (value: number) => (sqlEditorSessionState.limit = value),
/**
* The logs time range for a logs snippet, keyed by snippet id. Session state —
* never written to snippet content — so it works on read-only shared snippets
* and resets on reload. An unset snippet has no entry; read sites fall back to
* `DEFAULT_LOG_TIME_RANGE`.
*/
logRange: {} as { [snippetId: string]: LogTimeRange },
setLogRange: (id: string, range: LogTimeRange) => {
sqlEditorSessionState.logRange[id] = range
},
addResult: (id: string, results: any[], autoLimit?: number) => {
// Use ref() to prevent Valtio from creating proxies for each row object.
// This is critical for large result sets - without ref(), Valtio wraps every
// row and nested property in a Proxy, causing massive memory overhead.
// Alright to use ref() in this case as the data is meant to be read-only and we
// don't need to track changes to the underlying data
sqlEditorSessionState.results[id] = [{ rows: ref(results), autoLimit }]
},
addResultError: (id: string, error: any, autoLimit?: number) => {
sqlEditorSessionState.results[id] = [{ rows: ref([]), error, autoLimit }]
},
resetResult: (id: string) => {
sqlEditorSessionState.results[id] = []
},
/** Drop all session state for a snippet (called when the snippet is removed). */
clearForSnippet: (id: string) => {
delete sqlEditorSessionState.results[id]
delete sqlEditorSessionState.logRange[id]
},
})
export const getSqlEditorSessionSnapshot = () => snapshot(sqlEditorSessionState)
export const useSqlEditorSessionSnapshot = (options?: Parameters<typeof useSnapshot>[1]) =>
useSnapshot(sqlEditorSessionState, options)