Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(studio): keep SQL editor source menu open when switching sources (#48715)

Charis216574678450
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/QuerySourceMenu/QuerySourceMenu.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/QuerySourceMenu/QuerySourceMenu.tsx
index 027ae76bd2c..9e08a904d50 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/QuerySourceMenu/QuerySourceMenu.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/QuerySourceMenu/QuerySourceMenu.tsx
@@ -116,7 +116,13 @@ export const QuerySourceMenu = ({ id, runSource, canCreateLogsSnippet }: QuerySo
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-60">
- <DropdownMenuItem className="justify-between" onClick={() => switchSource('database')}>
+ <DropdownMenuItem
+ className="justify-between"
+ onSelect={(e) => {
+ e.preventDefault()
+ switchSource('database')
+ }}
+ >
<span className="flex items-center gap-x-2">
<Database size={14} className="text-foreground-light" />
Database
@@ -124,7 +130,13 @@ export const QuerySourceMenu = ({ id, runSource, canCreateLogsSnippet }: QuerySo
{!isLogs && <Check size={14} />}
</DropdownMenuItem>
{(canCreateLogsSnippet || isLogs) && (
- <DropdownMenuItem className="justify-between" onClick={() => switchSource('logs')}>
+ <DropdownMenuItem
+ className="justify-between"
+ onSelect={(e) => {
+ e.preventDefault()
+ switchSource('logs')
+ }}
+ >
<span className="flex items-center gap-x-2">
<ScrollText size={14} className="text-foreground-light" />
Logs
diff --git a/apps/studio/tests/components/SQLEditor/QuerySourceMenu.test.tsx b/apps/studio/tests/components/SQLEditor/QuerySourceMenu.test.tsx
new file mode 100644
index 00000000000..6985f0ad822
--- /dev/null
+++ b/apps/studio/tests/components/SQLEditor/QuerySourceMenu.test.tsx
@@ -0,0 +1,71 @@
+import { screen } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { mockAnimationsApi } from 'jsdom-testing-mocks'
+import { describe, expect, it } from 'vitest'
+
+import { DEFAULT_LOG_DATE_RANGE } from '@/components/interfaces/SQLEditor/querySource'
+import { QuerySourceMenu } from '@/components/interfaces/SQLEditor/UtilityPanel/QuerySourceMenu/QuerySourceMenu'
+import { customRender } from '@/tests/lib/custom-render'
+import { addAPIMock } from '@/tests/lib/msw'
+
+// QuerySourceMenu renders a Radix dropdown (+ nested dialog), both of which use Web Animations.
+mockAnimationsApi()
+
+addAPIMock({
+ method: 'get',
+ path: '/platform/projects/:ref',
+ response: {
+ id: 1,
+ ref: 'default',
+ organization_id: 1,
+ name: 'Test Project',
+ status: 'ACTIVE_HEALTHY',
+ cloud_provider: 'AWS',
+ region: 'us-east-1',
+ db_host: 'db.default.supabase.co',
+ restUrl: 'https://default.supabase.co/rest/v1/',
+ inserted_at: '2024-01-01T00:00:00Z',
+ updated_at: '2024-01-01T00:00:00Z',
+ subscription_id: 'sub_123',
+ is_branch_enabled: false,
+ is_physical_backups_enabled: false,
+ high_availability: false,
+ integration_source: null,
+ connectionString: 'postgresql://postgres@localhost:5432/postgres',
+ is_hibernating: false,
+ },
+})
+
+describe('QuerySourceMenu', () => {
+ it('keeps the dropdown open across a source switch, so the new source’s controls appear without reopening it', async () => {
+ // Selecting a source doesn't mutate `runSource` in place — it navigates to a
+ // fresh tab, and the parent re-renders this component with the new source once
+ // the route lands. Rerendering with the switched-to prop below stands in for
+ // that navigation, so the test observes exactly what the user does: does the
+ // dropdown have to be reopened to see the newly-available controls?
+ const { rerender } = customRender(
+ <QuerySourceMenu id="new-snippet" runSource={{ type: 'database' }} canCreateLogsSnippet />
+ )
+
+ await userEvent.click(screen.getByRole('button', { name: 'Query source: Database' }))
+
+ expect(await screen.findByText('Run as')).toBeInTheDocument()
+ expect(screen.queryByText('Time range')).not.toBeInTheDocument()
+
+ await userEvent.click(screen.getByText('Logs'))
+
+ rerender(
+ <QuerySourceMenu
+ id="new-snippet"
+ runSource={{ type: 'logs', dateRange: DEFAULT_LOG_DATE_RANGE }}
+ canCreateLogsSnippet
+ />
+ )
+
+ // The dropdown never closed, so the logs-only "Time range" control is visible
+ // immediately, and the database-only controls are gone — without the user
+ // having to reopen the menu.
+ expect(screen.getByText('Time range')).toBeInTheDocument()
+ expect(screen.queryByText('Run as')).not.toBeInTheDocument()
+ })
+})