Files
supabase/apps/studio/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider.tsx
T
Francesco Sansalvadore 156f904018 feat: floating mobile toolbar (#43444)
- move navigation components to `apps/studio/components/layouts/Navigation`
- add [FloatingMobileToolbar](https://github.com/supabase/supabase/pull/43444/changes#diff-3dffe47fd51ca851d612d8728e03b2dc344ec213d4f3a46a824d3fa32a7cc851) as quick access to tools such as search, assistant, inline editor, etc - behind feature flag and feature preview (true by default as it's a bit
annoying to have to enable it all the time as previews are stored in
local-storage)
- fix sidebar panels closing on viewport resizing (regression from previous pr)

https://github.com/user-attachments/assets/d6881e3b-5128-4306-bb82-3ca39c755dba

<img width="986" height="697" alt="Screenshot 2026-03-12 at 12 40 11"
src="https://github.com/user-attachments/assets/da8511e2-7d01-4237-b814-596031c747c5"
/>
2026-03-13 12:13:07 +00:00

120 lines
4.3 KiB
TypeScript

import { LOCAL_STORAGE_KEYS } from 'common'
import { getSupportLinkQueryParams } from 'components/ui/HelpPanel/HelpPanel.utils'
import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
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 dynamic from 'next/dynamic'
import { useRouter } from 'next/router'
import { parseAsString, useQueryState } from 'nuqs'
import { useEffect, type PropsWithChildren } from 'react'
import { 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 { mutate: sendEvent } = useSendEventMutation()
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 sidebarURLParamRef = useLatest(sidebarURLParam)
const sidebarLocalStorageRef = useLatest(sidebarLocalStorage)
useRegisterSidebar(SIDEBAR_KEYS.AI_ASSISTANT, () => <AIAssistant />, {}, 'i', !!project)
useRegisterSidebar(SIDEBAR_KEYS.EDITOR_PANEL, () => <EditorPanel />, {}, 'e', !!project)
useRegisterSidebar(SIDEBAR_KEYS.ADVISOR_PANEL, () => <AdvisorPanel />, {}, undefined, true)
useRegisterSidebar(
SIDEBAR_KEYS.HELP_PANEL,
() => (
<HelpPanel
onClose={() => closeSidebar(SIDEBAR_KEYS.HELP_PANEL)}
projectRef={project?.ref}
supportLinkQueryParams={getSupportLinkQueryParams(
project,
org,
router.query.ref as string | undefined
)}
/>
),
{},
undefined,
true
)
useEffect(() => {
if (!!project) {
if (activeSidebar) {
// add event tracking
sendEvent({
action: 'sidebar_opened',
properties: {
sidebar: activeSidebar.id as (typeof SIDEBAR_KEYS)[keyof typeof SIDEBAR_KEYS],
},
groups: {
project: project?.ref ?? 'Unknown',
organization: org?.slug ?? 'Unknown',
},
})
setSidebarLocalStorage(activeSidebar.id)
} else {
setSidebarLocalStorage('')
setSidebarUrlParam(null)
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [activeSidebar])
// Handle toggling of sidebars on page init
// Prioritize URL params first, then local storage
useEffect(() => {
if (router.isReady && isLoadedLocalStorage) {
if (
typeof sidebarURLParamRef.current === 'string' &&
Object.values(SIDEBAR_KEYS).includes(
sidebarURLParamRef.current as (typeof SIDEBAR_KEYS)[keyof typeof SIDEBAR_KEYS]
)
) {
console.log('Open sidebar based on URL')
openSidebar(sidebarURLParamRef.current)
} 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])
return <>{children}</>
}