mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +00:00
<!-- CURSOR_AGENT_PR_BODY_BEGIN --> A full page load with `?sidebar=` loses the param on TanStack Studio, so the side panel never opens. `LayoutSidebarProvider` treats the initial empty `activeSidebar` as a close and calls `setSidebarUrlParam(null)` while the URL still has `sidebar`; that queued nuqs replace flushes late and wins over the effect that opens the panel. Reproduces on master (TanStack only). Found while testing #51442. This only clears `?sidebar=` after a sidebar has actually been opened and then closed during the visit. **Changed:** - `apps/studio/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider.tsx` / `hasOpenedSidebarRef` – skip `setSidebarUrlParam(null)` until a sidebar has been opened this visit, including when it opened before the project finished loading. Closing still clears the param with a replace. If #51442 lands on the same line, combine the guards: `hasOpenedSidebarRef.current && sidebarURLParam !== null`. **Added:** - `LayoutSidebarProvider.test.tsx` – initial mount with `?sidebar=advisor-panel` and `?sidebar=ai-assistant` does not clear the param and opens the sidebar; open-then-close clears it with a replace; a panel that opens before the project loads still clears on close. These fail without the fix. Checks: `pnpm --filter studio typecheck`, `pnpm --filter studio run lint:ratchet`, `pnpm knip --workspace apps/studio`, Prettier, and the sidebar vitest files. ## To test (Next and TanStack) - Full load `/project/<ref>/editor/<id>?schema=shop&sidebar=advisor-panel` – param stays, Advisor panel opens, `schema=shop` stays - Full load `/project/<ref>?sidebar=ai-assistant` – param stays, Assistant panel opens - Close the panel – `?sidebar=` is removed with a replace; Back returns to the previous page and Forward returns here - Open a panel from its icon – URL unchanged - Full load without `?sidebar=` – no panel, and the URL is not rewritten - Switch between Advisor and Assistant – other params stay; closing clears `?sidebar=` - Change the table editor schema, add a table filter, and open an auth provider panel – those params update as before, and Back/Forward still move through them <!-- CURSOR_AGENT_PR_BODY_END --> <div><a href="https://cursor.com/agents/bc-6a57b74c-4b55-576d-b656-f20a2799f9e4?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a> <a href="https://cursor.com/background-agent?bcId=bc-6a57b74c-4b55-576d-b656-f20a2799f9e4&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a> </div> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
246 lines
7.3 KiB
TypeScript
246 lines
7.3 KiB
TypeScript
import { QueryClient } from '@tanstack/react-query'
|
|
import { act, screen, waitFor } from '@testing-library/react'
|
|
import { platformComponents as components } from 'api-types'
|
|
import { HttpResponse } from 'msw'
|
|
import { parseAsString, useQueryState } from 'nuqs'
|
|
import type { UrlUpdateEvent } from 'nuqs/adapters/testing'
|
|
import { afterEach, beforeEach, describe, expect, test } from 'vitest'
|
|
|
|
import { LayoutSidebarProvider, SIDEBAR_KEYS } from './LayoutSidebarProvider'
|
|
import { projectKeys } from '@/data/projects/keys'
|
|
import { sidebarManagerState } from '@/state/sidebar-manager-state'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
import { routerMock } from '@/tests/lib/route-mock'
|
|
|
|
type ProjectDetailResponse = components['schemas']['ProjectDetailResponse_Output']
|
|
|
|
const PROJECT_REF = 'default'
|
|
|
|
const PROJECT: ProjectDetailResponse = {
|
|
cloud_provider: 'AWS',
|
|
connectionString: `postgresql://postgres:password@db.${PROJECT_REF}.supabase.co:5432/postgres`,
|
|
db_host: `db.${PROJECT_REF}.supabase.co`,
|
|
high_availability: false,
|
|
id: 1,
|
|
infra_compute_size: 'micro',
|
|
inserted_at: '2026-01-01T00:00:00.000Z',
|
|
integration_source: null,
|
|
is_branch_enabled: false,
|
|
is_physical_backups_enabled: false,
|
|
name: 'Production',
|
|
organization_id: 1,
|
|
ref: PROJECT_REF,
|
|
region: 'us-east-1',
|
|
restUrl: `https://${PROJECT_REF}.supabase.co`,
|
|
status: 'ACTIVE_HEALTHY',
|
|
subscription_id: 'subscription-1',
|
|
updated_at: '2026-01-01T00:00:00.000Z',
|
|
}
|
|
|
|
const resetSidebarManagerState = () => {
|
|
Object.keys(sidebarManagerState.sidebars).forEach((id) => {
|
|
sidebarManagerState.unregisterSidebar(id)
|
|
})
|
|
sidebarManagerState.closeActive()
|
|
}
|
|
|
|
const SidebarParamProbe = () => {
|
|
const [sidebar] = useQueryState('sidebar', parseAsString)
|
|
return <div data-testid="sidebar-param">{sidebar ?? ''}</div>
|
|
}
|
|
|
|
const createQueryClient = () =>
|
|
new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false },
|
|
mutations: { retry: false },
|
|
},
|
|
})
|
|
|
|
const renderWithSidebarParam = (
|
|
sidebarId: string,
|
|
{
|
|
project,
|
|
projectResponse,
|
|
}: {
|
|
project?: ProjectDetailResponse
|
|
projectResponse?: () => Promise<Response> | Response
|
|
} = {}
|
|
) => {
|
|
const urlUpdates: UrlUpdateEvent[] = []
|
|
const queryClient = createQueryClient()
|
|
if (project) {
|
|
queryClient.setQueryData(projectKeys.detail(PROJECT_REF), project)
|
|
}
|
|
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref',
|
|
response: projectResponse ?? (() => HttpResponse.json<ProjectDetailResponse>(PROJECT)),
|
|
})
|
|
|
|
customRender(
|
|
<LayoutSidebarProvider>
|
|
<SidebarParamProbe />
|
|
</LayoutSidebarProvider>,
|
|
{
|
|
queryClient,
|
|
nuqs: {
|
|
hasMemory: true,
|
|
// the testing adapter otherwise resets the nuqs queue on every render, which drops the write under test.
|
|
resetUrlUpdateQueueOnMount: false,
|
|
searchParams: `?sidebar=${sidebarId}`,
|
|
onUrlUpdate: (event) => {
|
|
urlUpdates.push(event)
|
|
},
|
|
},
|
|
}
|
|
)
|
|
|
|
return { queryClient, urlUpdates }
|
|
}
|
|
|
|
describe('LayoutSidebarProvider sidebar deep link', () => {
|
|
let releaseProject = () => {}
|
|
|
|
beforeEach(() => {
|
|
resetSidebarManagerState()
|
|
routerMock.setCurrentUrl(`/project/${PROJECT_REF}`)
|
|
localStorage.clear()
|
|
releaseProject = () => {}
|
|
})
|
|
|
|
afterEach(() => {
|
|
releaseProject()
|
|
localStorage.clear()
|
|
})
|
|
|
|
test.each([SIDEBAR_KEYS.ADVISOR_PANEL, SIDEBAR_KEYS.AI_ASSISTANT])(
|
|
'does not clear ?sidebar=%s on initial mount, and clears it after the sidebar closes',
|
|
async (sidebarId) => {
|
|
const { urlUpdates } = renderWithSidebarParam(sidebarId, { project: PROJECT })
|
|
|
|
await waitFor(() => {
|
|
expect(sidebarManagerState.activeSidebar?.id).toBe(sidebarId)
|
|
})
|
|
|
|
// nuqs flushes on a later tick even when throttling is disabled
|
|
await act(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
})
|
|
|
|
expect(screen.getByTestId('sidebar-param').textContent).toBe(sidebarId)
|
|
expect(urlUpdates).toEqual([])
|
|
|
|
act(() => {
|
|
sidebarManagerState.closeActive()
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(urlUpdates).toEqual([
|
|
expect.objectContaining({
|
|
queryString: '',
|
|
options: expect.objectContaining({ history: 'replace' }),
|
|
}),
|
|
])
|
|
})
|
|
expect(screen.getByTestId('sidebar-param').textContent).toBe('')
|
|
expect(urlUpdates[0]?.searchParams.has('sidebar')).toBe(false)
|
|
}
|
|
)
|
|
|
|
test('does not clear ?sidebar= when the open sidebar unregisters', async () => {
|
|
const { urlUpdates } = renderWithSidebarParam(SIDEBAR_KEYS.AI_ASSISTANT, { project: PROJECT })
|
|
|
|
await waitFor(() => {
|
|
expect(sidebarManagerState.activeSidebar?.id).toBe(SIDEBAR_KEYS.AI_ASSISTANT)
|
|
})
|
|
|
|
act(() => {
|
|
sidebarManagerState.unregisterSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
|
|
})
|
|
|
|
await act(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
})
|
|
|
|
expect(sidebarManagerState.activeSidebar).toBeUndefined()
|
|
expect(screen.getByTestId('sidebar-param').textContent).toBe(SIDEBAR_KEYS.AI_ASSISTANT)
|
|
expect(urlUpdates).toEqual([])
|
|
})
|
|
|
|
test('clears ?sidebar= when the sidebar closes before the project loads', async () => {
|
|
const projectReleased = new Promise<void>((resolve) => {
|
|
releaseProject = resolve
|
|
})
|
|
|
|
const { queryClient, urlUpdates } = renderWithSidebarParam(SIDEBAR_KEYS.ADVISOR_PANEL, {
|
|
projectResponse: async () => {
|
|
await projectReleased
|
|
return HttpResponse.json<ProjectDetailResponse>(PROJECT)
|
|
},
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(sidebarManagerState.activeSidebar?.id).toBe(SIDEBAR_KEYS.ADVISOR_PANEL)
|
|
})
|
|
expect(queryClient.getQueryData(projectKeys.detail(PROJECT_REF))).toBeUndefined()
|
|
|
|
act(() => {
|
|
sidebarManagerState.closeActive()
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(urlUpdates).toEqual([
|
|
expect.objectContaining({
|
|
queryString: '',
|
|
options: expect.objectContaining({ history: 'replace' }),
|
|
}),
|
|
])
|
|
})
|
|
expect(screen.getByTestId('sidebar-param').textContent).toBe('')
|
|
})
|
|
|
|
// guards opened-sidebar tracking being recorded above the !project guard. a later close still has to clear ?sidebar=.
|
|
test('clears ?sidebar= on close when the panel opened before the project loaded', async () => {
|
|
const projectReleased = new Promise<void>((resolve) => {
|
|
releaseProject = resolve
|
|
})
|
|
|
|
const { queryClient, urlUpdates } = renderWithSidebarParam(SIDEBAR_KEYS.ADVISOR_PANEL, {
|
|
projectResponse: async () => {
|
|
await projectReleased
|
|
return HttpResponse.json<ProjectDetailResponse>(PROJECT)
|
|
},
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(sidebarManagerState.activeSidebar?.id).toBe(SIDEBAR_KEYS.ADVISOR_PANEL)
|
|
})
|
|
|
|
await act(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
})
|
|
expect(urlUpdates).toEqual([])
|
|
|
|
releaseProject()
|
|
await waitFor(() => {
|
|
expect(queryClient.getQueryData(projectKeys.detail(PROJECT_REF))).toBeTruthy()
|
|
})
|
|
|
|
act(() => {
|
|
sidebarManagerState.closeActive()
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(urlUpdates).toEqual([
|
|
expect.objectContaining({
|
|
queryString: '',
|
|
options: expect.objectContaining({ history: 'replace' }),
|
|
}),
|
|
])
|
|
})
|
|
})
|
|
})
|