Skip to content

refactor(sql-editor): decompose into controller contexts + presentational components (decompose 6/6) - #47938

Merged
charislam merged 2 commits into
masterfrom
charislam/sql-editor-decompose-6-jsx-panes
Jul 15, 2026
Merged

charislam merged 2 commits into
masterfrom
charislam/sql-editor-decompose-6-jsx-panes

Conversation

@charislam

@charislam charislam commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

What

Final step (6 of 6) of the SQLEditor.tsx decomposition. Splits the two ResizablePanel bodies out of the SQLEditorContent monolith into presentational sub-components:

  • SQLEditorPane — the editor panel: loading state, DiffEditor + diff ask-AI widget, MonacoEditor + ask-AI widget. Reads the Monaco refs (editorRef/monacoRef/diffEditorRef) from SQLEditorContext, and receives the diff/prompt/ai controllers plus reactive state as props.
  • SQLEditorResults — the results panel: loading state + UtilityPanel.

SQLEditorContent is now a composition root: shared-ref provider, hook composition, and the run-query warning modal.

Result

SQLEditor.tsx goes from the original 1056-line monolith down to 259 lines (≈75% reduction). The remaining size over a bare ~140-line root is the run-query warning modal, kept inline deliberately: its handlers hold the acceptUntrustedSql promotion, which must stay at the explicit user-action boundary in the root rather than moving into a presentational pane.

Behavior-preserving

The moved JSX is byte-identical aside from prop threading. No logic, effects, dependency arrays, or eslint-disables changed. The two render-time ref reads — the editor placeholder (!promptState.isOpen && !editorRef.current?.getValue()) and the ask-AI widget gate (editorRef.current && promptState.isOpen && !isDiffOpen) — are preserved verbatim in SQLEditorPane, which re-renders whenever the prompt/diff props change, keeping those reads fresh (the guardrail from the plan).

Verification (all green):

  • SQLEditor.test.tsx characterization suite — 11/11 pass
  • tsc --noEmit — no new errors
  • eslint — clean (no new ratchet entries)
  • prettier — clean

Stack

Builds on decompose 5 (#47935). This is the last PR in the series — the decomposition is complete after this merges.

Summary by CodeRabbit

  • New Features
    • Added a resizable SQL editor layout with run warnings, query results, and an Explain view.
    • Introduced centralized SQL editor controllers and expanded AI-assisted prompt/diff workflows.
    • Added keyboard support for running and Explain analysis.
  • Bug Fixes
    • Restored editor focus after accepting or discarding AI changes.
    • Improved handling and validation of SQL Explain actions.
    • Preserved editor scroll position when switching snippets.
  • Refactor
    • Streamlined the SQLEditor into a composed layout and improved memoization to reduce unnecessary re-renders.

@charislam
charislam requested a review from a team as a code owner July 14, 2026 18:30
@vercel

vercel Bot commented Jul 14, 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 15, 2026 3:31pm
studio-staging Ready Ready Preview, Comment Jul 15, 2026 3:31pm
5 Skipped Deployments
Project Deployment Actions Updated (UTC)
studio Ignored Ignored Jul 15, 2026 3:31pm
design-system Skipped Skipped Jul 15, 2026 3:31pm
docs Skipped Skipped Jul 15, 2026 3:31pm
ui-library Skipped Skipped Jul 15, 2026 3:31pm
zone-www-dot-com Skipped Skipped Jul 15, 2026 3:31pm

Request Review

@supabase

supabase Bot commented Jul 14, 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 ↗︎.

@coderabbitai

coderabbitai Bot commented Jul 14, 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: ea46321b-04dd-4fb6-b8a0-7a68225d23e2

📥 Commits

Reviewing files that changed from the base of the PR and between a755f1f and d127921.

📒 Files selected for processing (1)
  • apps/studio/components/interfaces/SQLEditor/useEditorMount.ts

📝 Walkthrough

Walkthrough

The SQL editor is decomposed into controller contexts, editor and diff panels, and a layout containing toolbar, warning modal, and results views. Hook APIs are memoized, utility tabs are typed, and Explain shortcuts explicitly promote untrusted SQL before execution.

Changes

SQLEditor composition

Layer / File(s) Summary
Controller contexts and provider
apps/studio/components/interfaces/SQLEditor/SQLEditor.types.ts, .../SQLEditorControllers.tsx
Adds typed utility-tab state and controller contexts for snippet, assistant, execution, and editor UI behavior.
Editor interaction hooks
apps/studio/components/interfaces/SQLEditor/hooks.ts, .../useEditorMount.ts, .../useSqlEditorAi.ts, .../useSqlEditorExecution.ts, .../useSqlEditorExplain.ts, .../useSqlEditorShortcuts.ts
Memoizes hook APIs, restores editor focus after AI actions, memoizes editor mount handling, tightens utility-tab types, and routes Explain shortcuts through safe SQL promotion.
Editor, diff, and prompt panel
apps/studio/components/interfaces/SQLEditor/SQLEditorEditorPanel.tsx
Adds Monaco/diff rendering, AI prompt widgets, editor actions, selection handling, and loading presentation.
Layout and top-level composition
apps/studio/components/interfaces/SQLEditor/SQLEditorLayout.tsx, .../SQLEditor.tsx
Adds the warning modal, toolbar, resizable results layout, Explain-tab handling, and controller-backed top-level composition.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

Suggested labels: autofix

Suggested reviewers: awaseem

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is informative, but it does not follow the repository’s required template or include the requested sections. Rewrite the PR description using the template: add YES/NO, change type, current behavior with issue links, new behavior, and additional context/screenshots.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: decomposing SQLEditor into controller contexts and presentational components.
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-6-jsx-panes

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

github-actions Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

🎭 Playwright Test Results (next)

passed  238 passed
skipped  5 skipped

Details

stats  243 tests across 28 suites
duration  4 minutes, 11 seconds
commit  d127921

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

Base automatically changed from charislam/sql-editor-decompose-5-ai-shortcuts to master July 14, 2026 19:14
@charislam
charislam force-pushed the charislam/sql-editor-decompose-6-jsx-panes branch from d508635 to 5b7652d Compare July 15, 2026 12:49
…onal components (decompose 6/6)

Final decomposition step. Split the SQLEditorContent monolith into a controller
provider + a tree of small presentational components that consume focused
contexts, eliminating prop-drilling.

- SQLEditorControllers: owns all state hooks (snippet/diff/prompt/ai/execution/
  explain/mount) and distributes them via four memoized contexts
  (useSqlEditorSnippet / useSqlEditorAssistant / useSqlEditorRun /
  useSqlEditorUi). Underlying hook returns are memoized so the context values
  stay stable across unrelated re-renders.
- SQLEditorLayout: the shell — run-warning modal, toolbar, and the
  editor/results resizable split, each a small context consumer.
- SQLEditorEditorPanel: the editor pane composed of diff view, main view, and
  the two ask-AI widgets.
- SQLEditor: an 11-line provider composition root (was the 1056-line monolith).

Safe-SQL boundary: the provider exposes only the already-safe execute pipelines
plus readEditorSql (UntrustedSqlFragment). Every acceptUntrustedSql promotion
happens at the user-action site (toolbar/editor/results/modal handlers and the
explain shortcut handler), never in the provider — keeping promotion auditable.

Also: tighten the utility-tab type to a union, refocus the editor after
accepting/discarding an AI diff, and drop an unneeded exhaustive-deps disable.

Behavior-preserving; the Phase-1 characterization suite stays green (11/11).
@charislam
charislam force-pushed the charislam/sql-editor-decompose-6-jsx-panes branch from 5b7652d to a755f1f Compare July 15, 2026 15:07
@charislam charislam changed the title refactor(sql-editor): split editor/results into presentational panes (decompose 6/6) refactor(sql-editor): decompose into controller contexts + presentational components (decompose 6/6) Jul 15, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
apps/studio/components/interfaces/SQLEditor/useEditorMount.ts (1)

20-36: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Avoid depending on the reactive tabs snapshot here. tabs.tabsMap[tabId] is only needed when onMount runs, so keeping tabs in the dependency array will recreate the callback on unrelated tabs updates and churn uiValue for every useSqlEditorUi() consumer. Use a non-reactive snapshot getter for the lookup to keep onMount stable.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/studio/components/interfaces/SQLEditor/useEditorMount.ts` around lines
20 - 36, Update onMount to retrieve tabData through the available non-reactive
tabs snapshot getter instead of the reactive tabs.tabsMap reference. Remove tabs
from the useCallback dependency array while preserving the existing scroll
restoration behavior and stable callback identity.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@apps/studio/components/interfaces/SQLEditor/useEditorMount.ts`:
- Around line 20-36: Update onMount to retrieve tabData through the available
non-reactive tabs snapshot getter instead of the reactive tabs.tabsMap
reference. Remove tabs from the useCallback dependency array while preserving
the existing scroll restoration behavior and stable callback identity.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 420f8643-e0a0-4485-9f21-2703209129d0

📥 Commits

Reviewing files that changed from the base of the PR and between 5b7652d and a755f1f.

📒 Files selected for processing (11)
  • apps/studio/components/interfaces/SQLEditor/SQLEditor.tsx
  • apps/studio/components/interfaces/SQLEditor/SQLEditor.types.ts
  • apps/studio/components/interfaces/SQLEditor/SQLEditorControllers.tsx
  • apps/studio/components/interfaces/SQLEditor/SQLEditorEditorPanel.tsx
  • apps/studio/components/interfaces/SQLEditor/SQLEditorLayout.tsx
  • apps/studio/components/interfaces/SQLEditor/hooks.ts
  • apps/studio/components/interfaces/SQLEditor/useEditorMount.ts
  • apps/studio/components/interfaces/SQLEditor/useSqlEditorAi.ts
  • apps/studio/components/interfaces/SQLEditor/useSqlEditorExecution.ts
  • apps/studio/components/interfaces/SQLEditor/useSqlEditorExplain.ts
  • apps/studio/components/interfaces/SQLEditor/useSqlEditorShortcuts.ts

onMount read tabData from the reactive tabs snapshot with tabs in its dep
array, so the callback identity churned on every tab-state change. Read it
non-reactively from the stable TabsStateContext proxy via valtio snapshot()
instead, and drop tabs from the deps. Scroll restoration is unchanged.

@awaseem awaseem left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM tested on staging and self hosted

@charislam
charislam merged commit 9e80a15 into master Jul 15, 2026
37 checks passed
@charislam
charislam deleted the charislam/sql-editor-decompose-6-jsx-panes branch July 15, 2026 17:10
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