Repository navigation
refactor(studio): extract useSnippetEditor from MonacoEditor - #47500
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
5 Skipped Deployments
|
|
This pull request has been ignored for the connected project Preview Branches by Supabase. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughSnippet edit status handling now lives in shared lifecycle/store logic, and ChangesSnippet edit lifecycle and editor wiring
Estimated code review effort: 3 (Moderate) | ~25 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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.
Inline comments:
In `@apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx`:
- Line 66: The Monaco save action is capturing the initial snippet value from
MonacoEditor and never sees updates after useSnippetEditor creates a
new/deep-linked snippet. Add a ref in MonacoEditor to hold the latest snippet,
keep it synchronized whenever snippet changes, and have the editor.addAction()
save handler read from that ref instead of the stale closure. Update the related
save logic around the Monaco action registration so Cmd/Ctrl+S works even when
snippet starts undefined.
In `@apps/studio/components/interfaces/SQLEditor/useSnippetEditor.ts`:
- Around line 37-40: The snippet editor currently skips empty strings in both
handleEditorChange and the debounced save path, so clearing text never persists
the deletion. Update useSnippetEditor so empty string values are treated as
valid content: only ignore undefined/absent editor values, and allow setSql to
run with an empty string for both the immediate change handler and the debounced
effect. Use the handleEditorChange and debouncedValue logic in useSnippetEditor
to locate the fix.
- Around line 41-49: The deep-link seeding in useSnippetEditor only runs on
initial mount, so a later-resolving route or data load can prevent snippet
creation and reroute. Update the seeding logic around handleEditorChange/content
to retry when content, ref, profile, and project become available, and guard it
with a seeded ref so it only runs once. Use the existing useEffect and
createSqlSnippetSkeletonV2/snapV2.addSnippet flow as the place to make this
reactive.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 32c52dff-abd1-4711-a44b-2935ec903adf
📒 Files selected for processing (2)
apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsxapps/studio/components/interfaces/SQLEditor/useSnippetEditor.ts
🎭 Playwright Test ResultsDetails
Flaky testsFeatures › queue-table-operations.spec.ts › Queue Table Operations › undo works for cell edits Skipped testsFeatures › auth-users.spec.ts › should show web3 users as enabled when the matching web3 provider is enabled |
There was a problem hiding this comment.
♻️ Duplicate comments (2)
apps/studio/components/interfaces/SQLEditor/useSnippetEditor.ts (2)
68-73: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy liftDeep-link seeding only fires on initial mount; can miss creation/routing if data resolves later.
useParams()/profile/projectmay still be loading on the first render, but this effect's[]deps mean it never retrieshandleEditorChange(content)once they resolve. A/sql/new?content=...visit can silently skip snippet creation and the redirect to the new snippet URL. This mirrors a previously raised concern that appears unaddressed in the current code — gate seeding with a "already seeded" ref and depend oncontent,ref,profile, andprojectso it can retry once those become available.🤖 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/useSnippetEditor.ts` around lines 68 - 73, The deep-link seeding in useSnippetEditor only runs once on mount, so it can miss snippet creation and redirect when profile/project or route params resolve later. Update the useEffect in useSnippetEditor to depend on content, profile, project, and the ref used for snippet creation/redirect, and add an “already seeded” ref guard so handleEditorChange(content) can retry until the required data is available without running twice.
37-39: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winEmpty-string edits are still dropped — clearing a snippet won't persist.
!valueshort-circuits for an empty string, so backspacing all content in the editor never reachessnapV2.setSql. This was flagged previously (marked addressed in a prior commit), but the current code still has the same guard. With the debounce now removed and edits written synchronously to the store, there's no other path left that could eventually flush an empty value — the store will keep the last non-empty SQL indefinitely even though the editor is empty.Proposed fix
function handleEditorChange(value: string | undefined) { tabsSnap.makeActiveTabPermanent() - if (!id || !value) return + if (!id || value === undefined) return🤖 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/useSnippetEditor.ts` around lines 37 - 39, The empty-string check in handleEditorChange is still preventing cleared snippets from being saved. Update the guard so only missing ids are rejected and an empty string can flow through to snapV2.setSql, allowing the editor state to persist when the snippet is fully erased. Keep the existing tabsSnap.makeActiveTabPermanent call, but make sure the value passed to setSql is preserved even when it is "".
🤖 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.
Duplicate comments:
In `@apps/studio/components/interfaces/SQLEditor/useSnippetEditor.ts`:
- Around line 68-73: The deep-link seeding in useSnippetEditor only runs once on
mount, so it can miss snippet creation and redirect when profile/project or
route params resolve later. Update the useEffect in useSnippetEditor to depend
on content, profile, project, and the ref used for snippet creation/redirect,
and add an “already seeded” ref guard so handleEditorChange(content) can retry
until the required data is available without running twice.
- Around line 37-39: The empty-string check in handleEditorChange is still
preventing cleared snippets from being saved. Update the guard so only missing
ids are rejected and an empty string can flow through to snapV2.setSql, allowing
the editor state to persist when the snippet is fully erased. Keep the existing
tabsSnap.makeActiveTabPermanent call, but make sure the value passed to setSql
is preserved even when it is "".
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 65563730-e218-4992-8021-ad5c37616ec5
📒 Files selected for processing (1)
apps/studio/components/interfaces/SQLEditor/useSnippetEditor.ts
Move the snippet editing lifecycle out of MonacoEditor into a co-located
useSnippetEditor hook: tracking the editor text, creating the snippet in the
store on first edit (and routing to its URL, replacing vs pushing for a
?content= deep link), persisting debounced changes via setSql, and seeding the
editor from the content param.
MonacoEditor keeps only the editor shell and Monaco action wiring, consuming
{ snippet, disableEdit, handleEditorChange } from the hook. Behaviour-preserving.
useSnippetEditor debounced editor changes by 1s before writing to the store, and the save mechanism already debounces persistence by 1s — two debounces in series, adding latency and splitting the "when to persist" policy across two layers. Write edits to the store synchronously on every change and let the save mechanism's debounce be the sole throttle. The store (and the snippet's dirty status) now reflects the latest edit immediately, and the save fires ~1s after the last keystroke instead of up to ~2s. Only the active snippet's own reactive consumers (a lightweight sidebar item) re-render per keystroke; Monaco is uncontrolled so it is unaffected. Removes the local value state, useDebounce, and the debounce effect.
…e signal setSql updated a snippet's content and queued it for saving but never touched status, so a just-edited but not-yet-saved snippet stayed 'saved' until the debounced save flipped it to 'saving'. During that window hasUnsavedChanges was false, so the beforeunload guard wouldn't warn if the tab was closed before the save fired. Add statusOnEdit and call it from setSql: a persisted, clean snippet becomes 'unsaved' immediately; never-persisted / in-flight / failed statuses pass through unchanged (preserving the persistence and progress axes). status is now the durable source of truth for unsaved changes, not the transient needsSaving queue. SavingIndicator needs no change (its isSaving/isSaveFailed predicates render nothing for 'unsaved', matching the prior no-indicator window). Adds statusOnEdit lifecycle tests.
61e7b9a to
8b9465a
Compare
The save-query action is registered once in handleEditorOnMount. For a new or deep-linked snippet, `snippet` is undefined at mount and only created on first edit; since MonacoEditor is keyed by a stable id it never remounts, so the action's closure kept `snippet` undefined and Cmd/Ctrl+S no-oped forever. Hold the latest snippet in a ref (matching the existing ref-sync pattern for the other Monaco actions) and read it in the save handler.
Braintrust eval report
|
What
PR 7 of the SQL editor state re-layering stack. Extracts the snippet editing lifecycle out of
MonacoEditorinto a co-locateduseSnippetEditorhook, and consolidates the edit debounce.useSnippetEditorowns:?content=deep link)setSql(withwasNeverPersisted→shouldInvalidate)contentparamcanEditSnippet)MonacoEditornow consumes{ snippet, disableEdit, handleEditorChange }and keeps only the editor shell + Monaco action wiring. It sheds therouter/profile/project/params/store/tabs hooks.handleEditorChangewas also flattened with an early return.Debounce consolidation
Previously there were two 1s debounces in series:
useSnippetEditordebounced editor changes before writing to the store, and the save mechanism (createSaveMechanism) already debounces persistence. That added latency (up to ~2s to save) and split the "when to persist" timing policy across two layers — at odds with PR 5's design where the scheduler/mechanism owns when and dirty state is meant to be immediate.This PR removes the editor-side debounce: edits write to the store synchronously on every change, and the save mechanism's 1s debounce is the sole throttle. Net effects:
defaultValue) so it is unaffectedNote: the double-debounce was legacy (the pre-refactor god store had the same
useDebounce(value, 1000)in MonacoEditor plus a debounced module-load subscribe).Notes
Validation
pnpm --filter studio typecheck✅pnpm exec vitest --run state/sql-editor/✅ (110 passed)Summary by CodeRabbit
New Features
Bug Fixes