mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 16:09:06 +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>
144 lines
4.9 KiB
TypeScript
144 lines
4.9 KiB
TypeScript
import { LOCAL_STORAGE_KEYS } from 'common'
|
|
import dynamic from 'next/dynamic'
|
|
import { useRouter } from 'next/router'
|
|
import { parseAsString, useQueryState } from 'nuqs'
|
|
import { useEffect, useEffectEvent, useRef, type PropsWithChildren } from 'react'
|
|
|
|
import { getSupportLinkQueryParams } from '@/components/ui/HelpPanel/HelpPanel.utils'
|
|
import { useLatest } from '@/hooks/misc/useLatest'
|
|
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
|
|
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
|
|
import { useShortcut } from '@/state/shortcuts/useShortcut'
|
|
import {
|
|
sidebarManagerState,
|
|
useRegisterSidebar,
|
|
useSidebarManagerSnapshot,
|
|
} from '@/state/sidebar-manager-state'
|
|
|
|
const AdvisorPanel = dynamic(() =>
|
|
import('@/components/ui/AdvisorPanel/AdvisorPanel').then((m) => m.AdvisorPanel)
|
|
)
|
|
const AIAssistant = dynamic(() =>
|
|
import('@/components/ui/AIAssistantPanel/AIAssistant').then((m) => m.AIAssistant)
|
|
)
|
|
const EditorPanel = dynamic(() =>
|
|
import('@/components/ui/EditorPanel/EditorPanel').then((m) => m.EditorPanel)
|
|
)
|
|
const HelpPanel = dynamic(() =>
|
|
import('@/components/ui/HelpPanel/HelpPanel').then((m) => m.HelpPanel)
|
|
)
|
|
|
|
export const SIDEBAR_KEYS = {
|
|
AI_ASSISTANT: 'ai-assistant',
|
|
EDITOR_PANEL: 'editor-panel',
|
|
ADVISOR_PANEL: 'advisor-panel',
|
|
HELP_PANEL: 'help-panel',
|
|
} as const
|
|
|
|
export type TYPEOF_SIDEBAR_KEYS = (typeof SIDEBAR_KEYS)[keyof typeof SIDEBAR_KEYS]
|
|
|
|
export const LayoutSidebarProvider = ({ children }: PropsWithChildren) => {
|
|
const router = useRouter()
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const { data: org } = useSelectedOrganizationQuery()
|
|
const track = useTrack()
|
|
const { openSidebar, closeSidebar, activeSidebar } = useSidebarManagerSnapshot()
|
|
|
|
const [sidebarURLParam, setSidebarUrlParam] = useQueryState('sidebar', parseAsString)
|
|
const [sidebarLocalStorage, setSidebarLocalStorage, { isSuccess: isLoadedLocalStorage }] =
|
|
useLocalStorageQuery(LOCAL_STORAGE_KEYS.LAST_OPENED_SIDE_BAR(project?.ref ?? ''), '')
|
|
|
|
const supportLinkQueryParams = getSupportLinkQueryParams(
|
|
project,
|
|
org,
|
|
router.query.ref as string | undefined
|
|
)
|
|
|
|
const sidebarLocalStorageRef = useLatest(sidebarLocalStorage)
|
|
|
|
useRegisterSidebar(SIDEBAR_KEYS.AI_ASSISTANT, () => <AIAssistant />, {}, !!project)
|
|
useRegisterSidebar(SIDEBAR_KEYS.EDITOR_PANEL, () => <EditorPanel />, {}, !!project)
|
|
useRegisterSidebar(SIDEBAR_KEYS.ADVISOR_PANEL, () => <AdvisorPanel />, {}, true)
|
|
useRegisterSidebar(
|
|
SIDEBAR_KEYS.HELP_PANEL,
|
|
() => (
|
|
<HelpPanel
|
|
onClose={() => closeSidebar(SIDEBAR_KEYS.HELP_PANEL)}
|
|
projectRef={project?.ref}
|
|
supportLinkQueryParams={supportLinkQueryParams}
|
|
/>
|
|
),
|
|
{},
|
|
true
|
|
)
|
|
|
|
useShortcut(SHORTCUT_IDS.AI_ASSISTANT_TOGGLE, () =>
|
|
sidebarManagerState.toggleSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
|
|
)
|
|
useShortcut(SHORTCUT_IDS.INLINE_EDITOR_TOGGLE, () =>
|
|
sidebarManagerState.toggleSidebar(SIDEBAR_KEYS.EDITOR_PANEL)
|
|
)
|
|
|
|
const onSidebarChanged = useEffectEvent(
|
|
(sidebarId: (typeof SIDEBAR_KEYS)[keyof typeof SIDEBAR_KEYS]) => {
|
|
track('sidebar_opened', { sidebar: sidebarId })
|
|
}
|
|
)
|
|
|
|
const previousSidebarIdRef = useRef<string | undefined>(undefined)
|
|
|
|
useEffect(() => {
|
|
const previousSidebarId = previousSidebarIdRef.current
|
|
previousSidebarIdRef.current = activeSidebar?.id
|
|
|
|
// unregistering (e.g. project switch) isn't a user close
|
|
const isClosedByUser =
|
|
!activeSidebar &&
|
|
previousSidebarId !== undefined &&
|
|
previousSidebarId in sidebarManagerState.sidebars
|
|
if (isClosedByUser) {
|
|
setSidebarUrlParam(null)
|
|
}
|
|
|
|
if (!project) {
|
|
return
|
|
}
|
|
|
|
if (activeSidebar) {
|
|
onSidebarChanged(activeSidebar.id as (typeof SIDEBAR_KEYS)[keyof typeof SIDEBAR_KEYS])
|
|
setSidebarLocalStorage(activeSidebar.id)
|
|
return
|
|
}
|
|
|
|
setSidebarLocalStorage('')
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [activeSidebar])
|
|
|
|
// Prioritize URL params first, then local storage
|
|
useEffect(() => {
|
|
if (router.isReady && isLoadedLocalStorage) {
|
|
if (
|
|
typeof sidebarURLParam === 'string' &&
|
|
Object.values(SIDEBAR_KEYS).includes(
|
|
sidebarURLParam as (typeof SIDEBAR_KEYS)[keyof typeof SIDEBAR_KEYS]
|
|
)
|
|
) {
|
|
openSidebar(sidebarURLParam)
|
|
} else if (
|
|
!!sidebarLocalStorageRef.current &&
|
|
Object.values(SIDEBAR_KEYS).includes(
|
|
sidebarLocalStorageRef.current as (typeof SIDEBAR_KEYS)[keyof typeof SIDEBAR_KEYS]
|
|
)
|
|
) {
|
|
openSidebar(sidebarLocalStorageRef.current)
|
|
}
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [router.isReady, isLoadedLocalStorage, sidebarURLParam])
|
|
|
|
return <>{children}</>
|
|
}
|