Skip to content

refactor(sql-editor): own shared editor refs in SQLEditorContext (decompose 3/N) - #47837

Merged
charislam merged 1 commit into
masterfrom
charislam/sql-editor-decompose-3-context
Jul 13, 2026
Merged

charislam merged 1 commit into
masterfrom
charislam/sql-editor-decompose-3-context

Conversation

@charislam

@charislam charislam commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

PR 3 in the SQLEditor decomposition stack. The keystone structural step: introduce a context provider that owns the shared, mutable Monaco state, so later PRs can extract logic hooks and presentational panes without threading the same three refs through every signature.

Behavior-preserving — the characterization suite (merged in PR 1) stays green with identical assertions.

What changed

New SQLEditorContext.tsx:

  • SQLEditorProvider owns the shared refs (editorRef, monacoRef, diffEditorRef, scrollTopRef), the run-refocus flag, and the error-highlight decorations.
  • useSQLEditorContext() guard hook (React 19 use()), mirroring the canonical PoliciesDataContext pattern.
  • Stable helpers: refocusEditor, clearPendingRunRefocus, markRefocusAfterRun, refocusEditorAfterRunIfNeeded, getEditorSql, clearHighlights, applyErrorHighlight.

SQLEditor.tsx:

  • Split into <SQLEditorProvider><SQLEditorContent/></SQLEditorProvider>.
  • SQLEditorContent reads refs/helpers from context instead of local useRef/useCallback.
  • lineHighlights moves from React state to a ref inside the provider (decorations are purely imperative; nothing renders off them). This also removes the stale-closure hazard the old executeQuery dep-array omission worked around.

The context value holds only stable identities (refs + useCallbacks, memoized once), so the provider never re-renders its consumers — it's a dependency-injection channel, not reactive state.

Verification

  • vitest — 156 pass (characterization + utils), assertions unchanged
  • pnpm --filter studio typecheck — clean for all SQL editor files (the two @sentry/tanstackstart-react errors are a pre-existing local-install gap on master, unrelated)
  • eslint — 0 errors
  • Equivalence: single useEffectEvent, drainDiffRequest driving-effect deps [diffRequest.pending, editorMountCount] byte-identical, all 8 eslint-disable dep arrays preserved

Summary by CodeRabbit

  • Bug Fixes
    • Improved SQL editor focus restoration after running or explaining queries.
    • Centralized run/explain highlighting so errors are highlighted consistently and old highlights are reliably cleared.
    • Ensured the SQL snippet used for run/explain is derived consistently from the current editor state.
  • Refactor
    • Restructured the SQL editor to use a shared internal context for editor UI, refs, and imperative highlight/focus helpers, keeping the external editor interface unchanged.

@charislam
charislam requested a review from a team as a code owner July 10, 2026 19:25
@supabase

supabase Bot commented Jul 10, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project xguihxuzqibwxjnimxev because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@vercel

vercel Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
studio-self-hosted Ready Ready Preview, Comment Jul 13, 2026 4:56pm
studio-staging Ready Ready Preview, Comment Jul 13, 2026 4:56pm
5 Skipped Deployments
Project Deployment Actions Updated (UTC)
studio Ignored Ignored Jul 13, 2026 4:56pm
design-system Skipped Skipped Jul 13, 2026 4:56pm
docs Skipped Skipped Jul 13, 2026 4:56pm
ui-library Skipped Skipped Jul 13, 2026 4:56pm
zone-www-dot-com Skipped Skipped Jul 13, 2026 4:56pm

Request Review

@coderabbitai

coderabbitai Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ea464d5e-37d4-49f5-ab22-01ff8ff290c7

📥 Commits

Reviewing files that changed from the base of the PR and between 1971ee5 and 7667bfb.

📒 Files selected for processing (2)
  • apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx
  • apps/studio/components/interfaces/SQLEditor/SQLEditorContext.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/studio/components/interfaces/SQLEditor/SQLEditorContext.tsx

📝 Walkthrough

Walkthrough

The SQL editor now uses SQLEditorProvider to centralize Monaco refs, SQL extraction, focus handling, and error highlights. SQLEditorContent consumes these helpers while execution, EXPLAIN, and warning-confirmation flows delegate refocus and decoration management to the context.

Changes

SQL editor context refactor

Layer / File(s) Summary
Context provider and editor helpers
apps/studio/components/interfaces/SQLEditor/SQLEditorContext.tsx, apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx
The provider owns editor refs, refocus state, SQL extraction, highlight clearing, and error decoration helpers, and wraps the exported editor.
Content execution and refocus flow
apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx
SQLEditorContent consumes context helpers for focus, SQL reads, execution and EXPLAIN highlight handling, error highlighting, run-confirmation refocus, and updated effect dependencies.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested reviewers: joshenlim, awaseem, ivasilov, pamelachia

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly reflects the main change: moving shared SQLEditor refs into SQLEditorContext during a refactor.
Description check ✅ Passed The description is substantially complete with summary, changes, and verification, though it doesn’t follow the template’s explicit section labels.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch charislam/sql-editor-decompose-3-context

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

🎭 Playwright Test Results (tanstack)

passed  238 passed
skipped  5 skipped

Details

stats  243 tests across 28 suites
duration  4 minutes, 21 seconds
commit  6418e82

Skipped tests

Features › auth-users.spec.ts › should show web3 users as enabled when the matching web3 provider is enabled
Features › sql-editor.spec.ts › SQL Editor › snippet favourite works as expected
Features › sql-editor.spec.ts › SQL Editor › share with team works as expected
Features › sql-editor.spec.ts › SQL Editor › folders works as expected
Features › sql-editor.spec.ts › SQL Editor › other SQL snippets actions work as expected

@github-actions

github-actions Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

🎭 Playwright Test Results (tanstack)

passed  238 passed
skipped  5 skipped

Details

stats  243 tests across 28 suites
duration  3 minutes, 17 seconds
commit  7667bfb

Skipped tests

Features › auth-users.spec.ts › should show web3 users as enabled when the matching web3 provider is enabled
Features › sql-editor.spec.ts › SQL Editor › snippet favourite works as expected
Features › sql-editor.spec.ts › SQL Editor › share with team works as expected
Features › sql-editor.spec.ts › SQL Editor › folders works as expected
Features › sql-editor.spec.ts › SQL Editor › other SQL snippets actions work as expected

@charislam
charislam force-pushed the charislam/sql-editor-decompose-3-context branch from 6418e82 to 1971ee5 Compare July 10, 2026 19:48
/**
* Dependency-injection channel for the SQL editor's mutable Monaco state.
*
* It owns the shared editor refs and the imperative helpers that read/write

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo no need for the comment

/** Refocus the editor iff a run-refocus was requested, then clear the flag. */
refocusEditorAfterRunIfNeeded: () => void
/**
* Resolve the SQL to act on from the editor (selection → full value →

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the comment here either

Introduces SQLEditorContext (provider + useSQLEditorContext guard hook) that owns the shared Monaco refs (editor/monaco/diff/scrollTop), the run-refocus flag, and the imperative highlight decorations, plus the stable helpers that read/write them: refocusEditor, clearPendingRunRefocus, markRefocusAfterRun, refocusEditorAfterRunIfNeeded, getEditorSql, clearHighlights, applyErrorHighlight.

SQLEditor is split into <SQLEditorProvider><SQLEditorContent/></SQLEditorProvider>; the body now reads refs/helpers from context instead of local useRef/useCallback. lineHighlights moves from React state to a ref inside the provider (decorations are only manipulated imperatively). The context value holds only stable identities, so the provider never re-renders consumers. No behavior change: characterization suite stays green; useEffectEvent/drainDiffRequest dep array and all eslint-disabled dep arrays preserved. PR 3/N of the SQLEditor decomposition stack.
@charislam
charislam force-pushed the charislam/sql-editor-decompose-3-context branch from 1971ee5 to 7667bfb Compare July 13, 2026 16:51
@charislam
charislam merged commit 9726940 into master Jul 13, 2026
37 checks passed
@charislam
charislam deleted the charislam/sql-editor-decompose-3-context branch July 13, 2026 17:02
@github-actions

github-actions Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Braintrust eval report

Assistant (master-1783962292)

Score Average Improvements Regressions
Tool Usage 70.6% (-4pp) - 1 🔴
Knowledge Usage 97.4% (+0pp) 1 🟢 1 🔴
SQL Identifier Quoting 100% (+0pp) - -
SQL Validity 100% (+0pp) - -
Time_to_first_token 2.38tok (-0.45tok) 23 🟢 6 🔴
Llm_calls 2.77 (-0.11) 7 🟢 12 🔴
Tool_calls 3.11 (+0.05) 10 🟢 8 🔴
Errors 5.24 (+0.03) 1 🟢 4 🔴
Llm_errors 0 (+0) - -
Tool_errors 0 (+0) - -
Prompt_tokens 23134.91tok (+3948.71tok) 6 🟢 19 🔴
Prompt_cached_tokens 7244.51tok (+306.02tok) 14 🟢 10 🔴
Prompt_cache_creation_tokens 0tok (+0tok) - -
Prompt_cache_creation_5m_tokens 0tok (+0tok) - -
Prompt_cache_creation_1h_tokens 0tok (+0tok) - -
Completion_tokens 640.75tok (+165.05tok) 6 🟢 23 🔴
Completion_reasoning_tokens 121.34tok (+29.92tok) 6 🟢 21 🔴
Total_tokens 23775.66tok (+4113.76tok) 8 🟢 21 🔴
Estimated_cost 0$ (+0$) 7 🟢 19 🔴
Duration 12.3s (-4.36s) 26 🟢 3 🔴
Llm_duration 9.52s (+0.88s) 12 🟢 17 🔴

joshenlim pushed a commit that referenced this pull request Jul 14, 2026
…ompose 3/N) (#47837)

## Summary

PR **3** in the SQLEditor decomposition stack. The keystone structural
step: introduce a context provider that owns the shared, mutable Monaco
state, so later PRs can extract logic hooks and presentational panes
without threading the same three refs through every signature.

Behavior-preserving — the characterization suite (merged in PR 1) stays
green with identical assertions.

## What changed

New `SQLEditorContext.tsx`:
- `SQLEditorProvider` owns the shared refs (`editorRef`, `monacoRef`,
`diffEditorRef`, `scrollTopRef`), the run-refocus flag, and the
error-highlight decorations.
- `useSQLEditorContext()` guard hook (React 19 `use()`), mirroring the
canonical `PoliciesDataContext` pattern.
- Stable helpers: `refocusEditor`, `clearPendingRunRefocus`,
`markRefocusAfterRun`, `refocusEditorAfterRunIfNeeded`, `getEditorSql`,
`clearHighlights`, `applyErrorHighlight`.

`SQLEditor.tsx`:
- Split into
`<SQLEditorProvider><SQLEditorContent/></SQLEditorProvider>`.
- `SQLEditorContent` reads refs/helpers from context instead of local
`useRef`/`useCallback`.
- `lineHighlights` moves from React state to a ref inside the provider
(decorations are purely imperative; nothing renders off them). This also
removes the stale-closure hazard the old `executeQuery` dep-array
omission worked around.

The context value holds only stable identities (refs + `useCallback`s,
memoized once), so the provider never re-renders its consumers — it's a
dependency-injection channel, not reactive state.

## Verification

- `vitest` — 156 pass (characterization + utils), assertions unchanged
- `pnpm --filter studio typecheck` — clean for all SQL editor files (the
two `@sentry/tanstackstart-react` errors are a pre-existing
local-install gap on master, unrelated)
- `eslint` — 0 errors
- Equivalence: single `useEffectEvent`, `drainDiffRequest`
driving-effect deps `[diffRequest.pending, editorMountCount]`
byte-identical, all 8 `eslint-disable` dep arrays preserved

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

* **Bug Fixes**
* Improved SQL editor focus restoration after running or explaining
queries.
* Centralized run/explain highlighting so errors are highlighted
consistently and old highlights are reliably cleared.
* Ensured the SQL snippet used for run/explain is derived consistently
from the current editor state.
* **Refactor**
* Restructured the SQL editor to use a shared internal context for
editor UI, refs, and imperative highlight/focus helpers, keeping the
external editor interface unchanged.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants