mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
Second of a stack. **Stacked on #49069** — review that one first; this PR's diff only makes sense on top of it. Base will retarget to `master` automatically when #49069 merges. Net `-72` lines. No behavior change beyond the one noted at the bottom. ## The problem The query-source registry carried its own `LogTimeRange` type and `logTimeRangeSchema`, which had drifted from the notebook wire schema's copy in four ways: | | wire schema | registry | |---|---|---| | discriminant | `_tag: 'relative_time_range'` | `type: 'relative'` | | absolute bounds | `start` / `end` | `from` / `to` | | relative units | minute…year | minute, hour, day | | validation | none | positive int, end-after-start | Two definitions of one concept, neither convertible to the other without a lossy mapping — and the notebook query cell was papering over it by discarding a log cell's persisted range and substituting a default. ## What changed #49069 moved the validations onto the wire schema's `timeRangeSchema` and exported it. This PR deletes the registry's copy and points every consumer at `TimeRange`. The registry keeps what is genuinely runtime: endpoints, labels, availability, defaults. The field renames ripple mechanically through the logs date-picker helpers, the time-range submenu, `useLogsCustomRange`, the SQL editor's session state, and their tests. Coverage for the absolute-range and unit rules moved to `notebook-schema.test.ts` in #49069, alongside the schema that now owns them. `ExplorerQuerySourceMenu` also drops its hand-rolled custom-range construction in favor of `customDateRangeToLogTimeRange`, which already existed and does the same clamping. ## One behavior change `logTimeRangeToDatePickerValue` now renders a range whose unit has no picker preset (week, month, year — allowed by the wire schema, not offered in the UI) as a resolved absolute range, instead of trying and failing to build a helper for it. Previously unreachable, since the registry's narrower unit set made those ranges unrepresentable. ## Verification Typecheck, Prettier, and the lint ratchet clean. 401 tests pass across the notebook schema, query sources, the logs source components, the SQL editor, and the Explorer surfaces. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved log time-range handling across Explorer and SQL Editor. * Custom date ranges now display and resolve correctly, including clamping invalid ranges. * Unsupported relative time units are converted to compatible absolute date-picker values. * **Refactor** * Standardized log queries on a shared time-range format for more consistent validation and behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
import { proxy, ref, snapshot, useSnapshot } from 'valtio'
|
|
|
|
import type { TimeRange } from '@/data/content/notebooks/notebook-schema'
|
|
|
|
/**
|
|
* 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]: TimeRange },
|
|
|
|
setLogRange: (id: string, range: TimeRange) => {
|
|
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)
|