Skip to content

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

feat(explorer): add path back to SQL Editor for snippet access (#49698)

Charis4b1f93bb9970
diff --git a/apps/studio/components/layouts/EditorNavigationButton.tsx b/apps/studio/components/layouts/EditorNavigationButton.tsx
new file mode 100644
index 00000000000..fa9464b7124
--- /dev/null
+++ b/apps/studio/components/layouts/EditorNavigationButton.tsx
@@ -0,0 +1,18 @@
+import { SqlEditor } from 'icons'
+import { ComponentProps } from 'react'
+
+import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
+
+export const EditorNavigationButton = ({
+ tooltip,
+ ...props
+}: { tooltip: string } & Omit<ComponentProps<typeof ButtonTooltip>, 'tooltip'>) => (
+ <ButtonTooltip
+ size="tiny"
+ variant="outline"
+ className="size-7 shrink-0 px-0"
+ icon={<SqlEditor size={14} strokeWidth={1.5} />}
+ tooltip={{ content: { side: 'bottom', text: tooltip } }}
+ {...props}
+ />
+)
diff --git a/apps/studio/components/layouts/ExplorerLayout/ExplorerLayout.tsx b/apps/studio/components/layouts/ExplorerLayout/ExplorerLayout.tsx
index fac0c33c638..2011c7b5ff9 100644
--- a/apps/studio/components/layouts/ExplorerLayout/ExplorerLayout.tsx
+++ b/apps/studio/components/layouts/ExplorerLayout/ExplorerLayout.tsx
@@ -1,5 +1,7 @@
+import { useParams } from 'common'
import { AnimatePresence, motion } from 'framer-motion'
import { Home, MessageCirclePlus, NotebookText, Plus, SquareCode } from 'lucide-react'
+import Link from 'next/link'
import { ComponentProps, ReactNode, useEffect, useEffectEvent, useState } from 'react'
import {
cn,
@@ -10,6 +12,7 @@ import {
TabsTrigger,
} from 'ui'
+import { EditorNavigationButton } from '../EditorNavigationButton'
import { ProjectLayoutWithAuth } from '../ProjectLayout'
import { EditorTabs } from '../Tabs/Tabs'
import { type ExplorerResourceType } from './ExplorerLayout.constants'
@@ -23,6 +26,7 @@ import {
useCreateNotebook,
useCreateQuery,
} from '@/components/interfaces/Explorer/hooks'
+import { useIsTemporarySqlEditorVisit } from '@/hooks/misc/useIsTemporarySqlEditorVisit'
import {
editorEntityTypes,
EXPLORER_HOME_TAB,
@@ -37,10 +41,17 @@ export interface ExplorerLayoutProps extends ComponentProps<typeof ProjectLayout
}
export const ExplorerLayout = ({ browserTitle, children, title }: ExplorerLayoutProps) => {
+ const { ref } = useParams()
const tabs = useTabsStateSnapshot()
const [section, setSection] = useState<ExplorerResourceType>()
+ const { setIsTemporary: setIsTemporarySqlEditorVisit } = useIsTemporarySqlEditorVisit(ref)
+
+ useEffect(() => {
+ if (ref) setIsTemporarySqlEditorVisit(false)
+ }, [ref, setIsTemporarySqlEditorVisit])
+
const activeTab = tabs.activeTab ? tabs.tabsMap[tabs.activeTab] : undefined
const isActiveExplorerTab =
activeTab !== undefined && editorEntityTypes.explorer.includes(activeTab.type)
@@ -56,6 +67,7 @@ export const ExplorerLayout = ({ browserTitle, children, title }: ExplorerLayout
<ProjectLayoutWithAuth
product="Explorer"
browserTitle={mergedBrowserTitle}
+ productMenuBadge={<BackToSqlEditorButton />}
productMenu={
<div className="relative h-full overflow-hidden">
<AnimatePresence mode="wait">
@@ -88,6 +100,26 @@ export const ExplorerLayout = ({ browserTitle, children, title }: ExplorerLayout
)
}
+const BackToSqlEditorButton = () => {
+ const { ref } = useParams()
+ const { setIsTemporary } = useIsTemporarySqlEditorVisit(ref)
+
+ if (!ref) return null
+
+ return (
+ <EditorNavigationButton
+ asChild
+ tooltip="Temporarily switch to SQL Editor to access your snippets"
+ >
+ <Link
+ href={`/project/${ref}/sql`}
+ aria-label="Switch to SQL Editor"
+ onClick={() => setIsTemporary(true)}
+ />
+ </EditorNavigationButton>
+ )
+}
+
const TabClassName =
'flex items-center justify-center min-w-(--header-height) min-h-(--header-height) hover:bg-surface-100 shrink-0'
diff --git a/apps/studio/components/layouts/Navigation/ProductMenuBar.tsx b/apps/studio/components/layouts/Navigation/ProductMenuBar.tsx
index 8c7f6070212..3c28f74dcc3 100644
--- a/apps/studio/components/layouts/Navigation/ProductMenuBar.tsx
+++ b/apps/studio/components/layouts/Navigation/ProductMenuBar.tsx
@@ -24,11 +24,9 @@ export const ProductMenuBar = ({
'hide-scrollbar bg-dash-sidebar border-default'
)}
>
- <div className="border-default flex min-h-(--header-height) items-center border-b px-6 justify-between">
- <div className="flex items-center gap-2">
- <h4 className="text-lg">{title}</h4>
- {titleBadge}
- </div>
+ <div className="border-default flex min-h-(--header-height) items-center gap-2 border-b px-6 justify-between">
+ <h4 className="text-lg truncate min-w-0 flex-1">{title}</h4>
+ {titleBadge}
</div>
<div className={cn('grow overflow-y-auto', className)}>{children}</div>
</div>
diff --git a/apps/studio/components/layouts/editors/EditorBaseLayout.tsx b/apps/studio/components/layouts/editors/EditorBaseLayout.tsx
index bf1722d1c78..78e3bfc2996 100644
--- a/apps/studio/components/layouts/editors/EditorBaseLayout.tsx
+++ b/apps/studio/components/layouts/editors/EditorBaseLayout.tsx
@@ -1,12 +1,14 @@
import { useParams } from 'common'
-import { usePathname } from 'next/navigation'
+import { usePathname, useRouter } from 'next/navigation'
import { ComponentProps, ReactNode } from 'react'
import { cn } from 'ui'
+import { EditorNavigationButton } from '../EditorNavigationButton'
import { ProjectLayoutWithAuth } from '../ProjectLayout'
import { CollapseButton } from '../Tabs/CollapseButton'
import { EditorTabs } from '../Tabs/Tabs'
import { useEditorType } from './EditorsLayout.hooks'
+import { useIsTemporarySqlEditorVisit } from '@/hooks/misc/useIsTemporarySqlEditorVisit'
import { useTabsStateSnapshot } from '@/state/tabs'
export interface ExplorerLayoutProps extends ComponentProps<typeof ProjectLayoutWithAuth> {
@@ -62,6 +64,7 @@ export const EditorBaseLayout = ({
resizableSidebar
product={product}
browserTitle={mergedBrowserTitle}
+ productMenuBadge={editor === 'sql' ? <BackToExplorerButton /> : undefined}
productMenuClassName={productMenuClassName}
productMenu={productMenu}
>
@@ -79,3 +82,21 @@ export const EditorBaseLayout = ({
</ProjectLayoutWithAuth>
)
}
+
+const BackToExplorerButton = () => {
+ const { ref } = useParams()
+ const router = useRouter()
+ const { isTemporary, setIsTemporary } = useIsTemporarySqlEditorVisit(ref)
+
+ if (!ref || !isTemporary) return null
+
+ return (
+ <EditorNavigationButton
+ tooltip="Back to Explorer"
+ onClick={() => {
+ setIsTemporary(false)
+ router.push(`/project/${ref}/explorer`)
+ }}
+ />
+ )
+}
diff --git a/apps/studio/hooks/misc/useIsTemporarySqlEditorVisit.ts b/apps/studio/hooks/misc/useIsTemporarySqlEditorVisit.ts
new file mode 100644
index 00000000000..309c78f85fa
--- /dev/null
+++ b/apps/studio/hooks/misc/useIsTemporarySqlEditorVisit.ts
@@ -0,0 +1,12 @@
+import { LOCAL_STORAGE_KEYS } from 'common'
+
+import { useLocalStorageQuery } from './useLocalStorage'
+
+export const useIsTemporarySqlEditorVisit = (ref: string | undefined) => {
+ const [isTemporary, setIsTemporary] = useLocalStorageQuery(
+ LOCAL_STORAGE_KEYS.SQL_EDITOR_TEMPORARY_FROM_EXPLORER(ref ?? ''),
+ false
+ )
+
+ return { isTemporary, setIsTemporary }
+}
diff --git a/packages/common/constants/local-storage.ts b/packages/common/constants/local-storage.ts
index 374cdc1d794..f5041ea185c 100644
--- a/packages/common/constants/local-storage.ts
+++ b/packages/common/constants/local-storage.ts
@@ -51,6 +51,9 @@ export const LOCAL_STORAGE_KEYS = {
SQL_EDITOR_SECTION_STATE: (ref: string) => `sql-editor-section-state-${ref}`,
SQL_EDITOR_SORT: (ref: string) => `sql-editor-sort-${ref}`,
SQL_EDITOR_MANUAL_SAVE_NOTICE_DISMISSED: 'sql-editor-manual-save-notice-dismissed',
+ // Set when a user follows the "temporarily switch to SQL Editor" link from Explorer;
+ // shows a way back and is cleared once they return to Explorer
+ SQL_EDITOR_TEMPORARY_FROM_EXPLORER: (ref: string) => `sql-editor-temporary-from-explorer-${ref}`,
EXPLORER_QUERY_DRAFTS: (ref: string) => `explorer-query-drafts-${ref}`,