Skip to content

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

Joshenlim/fe 4378 update editorpanel save cta when explorer is enabled (#50324)

Joshen Limad5202d53d81
diff --git a/apps/studio/components/interfaces/Explorer/ExplorerQueryTab.tsx b/apps/studio/components/interfaces/Explorer/ExplorerQueryTab.tsx
index 9195aac3199..fd187c0c57e 100644
--- a/apps/studio/components/interfaces/Explorer/ExplorerQueryTab.tsx
+++ b/apps/studio/components/interfaces/Explorer/ExplorerQueryTab.tsx
@@ -1,40 +1,25 @@
import { type Hotkey } from '@tanstack/react-hotkeys'
-import { useDebounce } from '@uidotdev/usehooks'
import { LOCAL_STORAGE_KEYS, useParams } from 'common'
-import { AlignLeft, Check, Keyboard, Loader2, MoreVertical, Save, SquareCode } from 'lucide-react'
+import { AlignLeft, Check, Keyboard, Loader2, MoreVertical, SquareCode } from 'lucide-react'
import { useRouter } from 'next/router'
-import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
-import { toast } from 'sonner'
+import { useCallback, useContext, useEffect, useRef, useState } from 'react'
import {
Button,
- Command,
- CommandGroup,
- CommandInput,
- CommandItem,
- CommandList,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
- DropdownMenuSub,
- DropdownMenuSubContent,
- DropdownMenuSubTrigger,
DropdownMenuTrigger,
KeyboardShortcut,
} from 'ui'
-import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
import { ExplorerToolbarAction } from './ExplorerToolbar'
-import { useCreateNotebook } from './hooks'
import { QueryEditor, type ExplorerQueryModel, type QueryEditorHandle } from './QueryEditor'
+import { SaveQueryDropdown } from './SaveQueryDropdown'
import { type QueryDisplay, type QueryResult } from './types'
-import { createQueryCellSkeleton } from './utils'
-import { getNotebook } from '@/data/content/notebooks/notebook-query'
-import { useNotebooksInfiniteQuery } from '@/data/content/notebooks/notebooks-infinite-query'
import { toQuerySourceBinding } from '@/data/query-sources/query-source-registry'
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
import { explorerQueryState, useExplorerQueryStateSnapshot } from '@/state/explorer-query'
-import { useNotebooksStateSnapshot } from '@/state/notebooks/notebooks-state'
import { useControlledRoleImpersonationState } from '@/state/role-impersonation-state'
import { hotkeyToKeys } from '@/state/shortcuts/formatShortcut'
import { SHORTCUT_DEFINITIONS, SHORTCUT_IDS } from '@/state/shortcuts/registry'
@@ -47,9 +32,6 @@ export const ExplorerQueryTab = () => {
const tabs = useContext(TabsStateContext)
const querySnap = useExplorerQueryStateSnapshot()
- const { createNotebook } = useCreateNotebook()
- const notebooksSnap = useNotebooksStateSnapshot()
-
const [isIntellisenseEnabled, setIsIntellisenseEnabled] = useLocalStorageQuery(
LOCAL_STORAGE_KEYS.SQL_EDITOR_INTELLISENSE,
true
@@ -63,18 +45,6 @@ export const ExplorerQueryTab = () => {
const [restoredQueryKey, setRestoredQueryKey] = useState<string>()
const [showQuery, setShowQuery] = useState(true)
- const [search, setSearch] = useState('')
- const debouncedSearch = useDebounce(search, 500)
-
- const { data: notebooksData, isPending } = useNotebooksInfiniteQuery({
- projectRef: ref,
- limit: 100,
- name: search.length === 0 ? search : debouncedSearch,
- })
- const notebooks = useMemo(() => {
- const items = notebooksData?.pages.flatMap((page) => page.content) ?? []
- return items
- }, [notebooksData?.pages])
const stateDraft = id ? querySnap.drafts[id] : undefined
const draft = stateDraft?.projectRef === ref ? stateDraft : undefined
@@ -159,32 +129,6 @@ export const ExplorerQueryTab = () => {
})
}
- const onAddToNewNotebook = () => {
- createNotebook({
- cells: [createQueryCellSkeleton({ title: draft.name, sql: draft.uncheckedSql })],
- })
- }
-
- const onAddToExistingNotebook = async (notebookId: string) => {
- if (!ref) return
- try {
- if (!notebooksSnap.notebooks[notebookId]?.notebook.content) {
- const notebook = await getNotebook({ projectRef: ref, id: notebookId })
- notebooksSnap.setNotebook({ projectRef: ref, notebook })
- }
-
- notebooksSnap.insertCellAfter({
- id: notebookId,
- cell: createQueryCellSkeleton({ title: draft.name, sql: draft.uncheckedSql }),
- })
- notebooksSnap.requestScrollToBottom(notebookId)
-
- router.push(`/project/${ref}/explorer/notebook/${notebookId}`)
- } catch (error) {
- toast.error('Failed to add query to notebook')
- }
- }
-
return (
<QueryEditor
ref={queryEditorRef}
@@ -222,57 +166,7 @@ export const ExplorerQueryTab = () => {
}}
toolbarActions={
<>
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <ExplorerToolbarAction
- icon={<Save size={16} strokeWidth={2} />}
- tooltip="Save query"
- />
- </DropdownMenuTrigger>
- <DropdownMenuContent className="w-52" align="end">
- <DropdownMenuSub>
- <DropdownMenuSubTrigger>Add to existing notebook</DropdownMenuSubTrigger>
- <DropdownMenuSubContent className="p-0">
- <Command shouldFilter={false}>
- <CommandInput
- autoFocus
- placeholder="Search notebooks..."
- className="text-xs"
- value={search}
- onValueChange={setSearch}
- />
- <CommandList>
- <CommandGroup>
- {isPending ? (
- <div className="flex flex-col p-1 gap-y-1">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- </div>
- ) : !notebooks?.length ? (
- <p className="text-xs text-center text-foreground-lighter py-3">
- No notebooks found
- </p>
- ) : null}
- {notebooks?.map((notebook) => (
- <CommandItem
- key={notebook.id}
- value={notebook.id}
- className="cursor-pointer"
- onSelect={() => onAddToExistingNotebook(notebook.id)}
- >
- {notebook.name}
- </CommandItem>
- ))}
- </CommandGroup>
- </CommandList>
- </Command>
- </DropdownMenuSubContent>
- </DropdownMenuSub>
- <DropdownMenuItem onClick={onAddToNewNotebook}>
- Create a new notebook
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
+ <SaveQueryDropdown query={{ title: draft.name, sql: draft.uncheckedSql }} />
<DropdownMenu>
<DropdownMenuTrigger asChild>
<ExplorerToolbarAction icon={<MoreVertical size={16} strokeWidth={2} />} />
diff --git a/apps/studio/components/interfaces/Explorer/SaveQueryDropdown.tsx b/apps/studio/components/interfaces/Explorer/SaveQueryDropdown.tsx
new file mode 100644
index 00000000000..3c04204dc93
--- /dev/null
+++ b/apps/studio/components/interfaces/Explorer/SaveQueryDropdown.tsx
@@ -0,0 +1,132 @@
+import { useDebounce } from '@uidotdev/usehooks'
+import { useParams } from 'common'
+import { Save } from 'lucide-react'
+import { useRouter } from 'next/router'
+import { useMemo, useState, type PropsWithChildren } from 'react'
+import { toast } from 'sonner'
+import {
+ Command,
+ CommandGroup,
+ CommandInput,
+ CommandItem,
+ CommandList,
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuSub,
+ DropdownMenuSubContent,
+ DropdownMenuSubTrigger,
+ DropdownMenuTrigger,
+} from 'ui'
+import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
+
+import { ExplorerToolbarAction } from './ExplorerToolbar'
+import { useCreateNotebook } from './hooks'
+import { createQueryCellSkeleton } from './utils'
+import { getNotebook } from '@/data/content/notebooks/notebook-query'
+import { useNotebooksInfiniteQuery } from '@/data/content/notebooks/notebooks-infinite-query'
+import { useNotebooksStateSnapshot } from '@/state/notebooks/notebooks-state'
+
+interface SaveQueryDropdownProps {
+ query: { title: string; sql: string }
+}
+
+export const SaveQueryDropdown = ({
+ children,
+ query,
+}: PropsWithChildren<SaveQueryDropdownProps>) => {
+ const router = useRouter()
+ const { ref } = useParams()
+ const { createNotebook } = useCreateNotebook()
+ const notebooksSnap = useNotebooksStateSnapshot()
+
+ const [search, setSearch] = useState('')
+ const debouncedSearch = useDebounce(search, 500)
+
+ const { data: notebooksData, isPending } = useNotebooksInfiniteQuery({
+ projectRef: ref,
+ limit: 100,
+ name: search.length === 0 ? search : debouncedSearch,
+ })
+ const notebooks = useMemo(() => {
+ const items = notebooksData?.pages.flatMap((page) => page.content) ?? []
+ return items
+ }, [notebooksData?.pages])
+
+ const onAddToNewNotebook = () => {
+ createNotebook({
+ cells: [createQueryCellSkeleton(query)],
+ })
+ }
+
+ const onAddToExistingNotebook = async (notebookId: string) => {
+ if (!ref) return
+ try {
+ if (!notebooksSnap.notebooks[notebookId]?.notebook.content) {
+ const notebook = await getNotebook({ projectRef: ref, id: notebookId })
+ notebooksSnap.setNotebook({ projectRef: ref, notebook })
+ }
+
+ notebooksSnap.insertCellAfter({
+ id: notebookId,
+ cell: createQueryCellSkeleton(query),
+ })
+ notebooksSnap.requestScrollToBottom(notebookId)
+
+ router.push(`/project/${ref}/explorer/notebook/${notebookId}`)
+ } catch (error) {
+ toast.error('Failed to add query to notebook')
+ }
+ }
+
+ return (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ {children ?? (
+ <ExplorerToolbarAction icon={<Save size={16} strokeWidth={2} />} tooltip="Save query" />
+ )}
+ </DropdownMenuTrigger>
+ <DropdownMenuContent className="w-48" align="end">
+ <DropdownMenuSub>
+ <DropdownMenuSubTrigger>Add to existing notebook</DropdownMenuSubTrigger>
+ <DropdownMenuSubContent className="p-0">
+ <Command shouldFilter={false}>
+ <CommandInput
+ autoFocus
+ placeholder="Search notebooks..."
+ className="text-xs"
+ value={search}
+ onValueChange={setSearch}
+ />
+ <CommandList>
+ <CommandGroup>
+ {isPending ? (
+ <div className="flex flex-col p-1 gap-y-1">
+ <ShimmeringLoader />
+ <ShimmeringLoader className="w-3/4" />
+ </div>
+ ) : !notebooks?.length ? (
+ <p className="text-xs text-center text-foreground-lighter py-3">
+ No notebooks found
+ </p>
+ ) : null}
+ {notebooks?.map((notebook) => (
+ <CommandItem
+ key={notebook.id}
+ value={notebook.id}
+ className="cursor-pointer"
+ onSelect={() => onAddToExistingNotebook(notebook.id)}
+ >
+ {notebook.name}
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ </CommandList>
+ </Command>
+ </DropdownMenuSubContent>
+ </DropdownMenuSub>
+ <DropdownMenuItem onClick={onAddToNewNotebook}>Create a new notebook</DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )
+}
diff --git a/apps/studio/components/ui/EditorPanel/EditorPanel.tsx b/apps/studio/components/ui/EditorPanel/EditorPanel.tsx
index ae2385d04ae..af767e2deae 100644
--- a/apps/studio/components/ui/EditorPanel/EditorPanel.tsx
+++ b/apps/studio/components/ui/EditorPanel/EditorPanel.tsx
@@ -47,6 +47,7 @@ import { SaveSnippetDialog } from './SaveSnippetDialog'
import { useIsExplorerEnabled } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
import { isExplainQuery } from '@/components/interfaces/ExplainVisualizer/ExplainVisualizer.utils'
import { useCreateQuery } from '@/components/interfaces/Explorer/hooks'
+import { SaveQueryDropdown } from '@/components/interfaces/Explorer/SaveQueryDropdown'
import { generateSnippetTitle } from '@/components/interfaces/SQLEditor/SQLEditor.constants'
import { createSqlSnippetSkeletonV2 } from '@/components/interfaces/SQLEditor/SQLEditor.utils'
import { useAddDefinitions } from '@/components/interfaces/SQLEditor/useAddDefinitions'
@@ -324,7 +325,7 @@ export const EditorPanel = () => {
{isEditingTitle ? (
<input
ref={titleInputRef}
- aria-label="Snippet name"
+ aria-label="Query title"
value={titleInput}
onChange={(e) => setTitleInput(e.target.value)}
onBlur={commitRename}
@@ -373,7 +374,7 @@ export const EditorPanel = () => {
text: 'Open snippet',
},
}}
- ></ButtonTooltip>
+ />
</PopoverTrigger>
<PopoverContent align="end" className="w-[300px] p-0">
<Command shouldFilter={false}>
@@ -629,43 +630,53 @@ export const EditorPanel = () => {
</span>
</div>
)}
- <Button
- size="tiny"
- disabled={
- !currentValue ||
- isExecuting ||
- isUpserting ||
- (!!activeSnippet &&
- currentValue === originalSnippetRef.current?.sql &&
- activeSnippet.name === originalSnippetRef.current?.name)
- }
- onClick={() => {
- if (!ref || !profile || !project) return
- if (activeSnippet) {
- setSaveStatus('idle')
- upsertContent({
- projectRef: ref,
- payload: {
- id: activeSnippet.id,
- type: 'sql',
- name: activeSnippet.name,
- description: activeSnippet.description ?? '',
- visibility: activeSnippet.visibility ?? 'user',
- project_id: project.id,
- owner_id: profile.id,
- content: {
- ...activeSnippet.content,
- unchecked_sql: untrustedSql(currentValue),
- },
- },
- })
- } else {
- setIsSaveDialogOpen(true)
+
+ {isExplorerEnabled ? (
+ <SaveQueryDropdown
+ query={{ title: activeSnippet?.name ?? 'Run SQL', sql: currentValue }}
+ >
+ <Button size="tiny">Save</Button>
+ </SaveQueryDropdown>
+ ) : (
+ <Button
+ size="tiny"
+ disabled={
+ !currentValue ||
+ isExecuting ||
+ isUpserting ||
+ (!!activeSnippet &&
+ currentValue === originalSnippetRef.current?.sql &&
+ activeSnippet.name === originalSnippetRef.current?.name)
}
- }}
- >
- {activeSnippet ? 'Update snippet' : 'Save as snippet'}
- </Button>
+ onClick={() => {
+ if (!ref || !profile || !project) return
+ if (activeSnippet) {
+ setSaveStatus('idle')
+ upsertContent({
+ projectRef: ref,
+ payload: {
+ id: activeSnippet.id,
+ type: 'sql',
+ name: activeSnippet.name,
+ description: activeSnippet.description ?? '',
+ visibility: activeSnippet.visibility ?? 'user',
+ project_id: project.id,
+ owner_id: profile.id,
+ content: {
+ ...activeSnippet.content,
+ unchecked_sql: untrustedSql(currentValue),
+ },
+ },
+ })
+ } else {
+ setIsSaveDialogOpen(true)
+ }
+ }}
+ >
+ {activeSnippet ? 'Update snippet' : 'Save as snippet'}
+ </Button>
+ )}
+
<SqlRunButton
isDisabled={isExecuting}
isExecuting={isExecuting}