Skip to content

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

Remove SQLEditorMenu component - this was a new component started at the beginning to refactor stuff but was dropped off, removed to prevent confusion

Joshen Lim353bf6a69db0
diff --git a/studio/components/layouts/SQLEditorLayout/SQLEditorLayout.tsx b/studio/components/layouts/SQLEditorLayout/SQLEditorLayout.tsx
index 2b0bbb61456..098176af0a1 100644
--- a/studio/components/layouts/SQLEditorLayout/SQLEditorLayout.tsx
+++ b/studio/components/layouts/SQLEditorLayout/SQLEditorLayout.tsx
@@ -1,11 +1,10 @@
-import { FC, ReactNode, useEffect, useState } from 'react'
+import { FC, ReactNode, useEffect } from 'react'
import { observer } from 'mobx-react-lite'
import { useStore } from 'hooks'
import Error from 'components/ui/Error'
import ProjectLayout from '../ProjectLayout/ProjectLayout'
-// import SQLEditorMenu from './SQLEditorMenu'
-import SQLEditorMenuOld from './SQLEditorMenuOld'
+import SQLEditorMenu from './SQLEditorMenu'
interface Props {
title: string
@@ -28,7 +27,7 @@ const SQLEditorLayout: FC<Props> = ({ title, children }) => {
isLoading={content.isLoading}
title={title || 'SQL'}
product="SQL Editor"
- productMenu={<SQLEditorMenuOld />}
+ productMenu={<SQLEditorMenu />}
>
{children}
</ProjectLayout>
diff --git a/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx b/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
index 61afcbe6c6a..780ea5a3d6e 100644
--- a/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
+++ b/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
@@ -1,217 +1,277 @@
-import { FC, useState } from 'react'
-import { observer } from 'mobx-react-lite'
-import Link from 'next/link'
+import { useState } from 'react'
import { useRouter } from 'next/router'
+import { observer } from 'mobx-react-lite'
import {
Button,
Dropdown,
- Typography,
Menu,
Input,
- IconX,
- IconPlus,
- IconEdit,
IconSearch,
+ IconPlus,
+ IconX,
+ IconLoader,
IconTrash,
IconChevronDown,
+ Loading,
+ Modal,
+ IconEdit2,
} from '@supabase/ui'
-import Divider from 'components/ui/Divider'
-import { partition } from 'lodash'
+import { IS_PLATFORM } from 'lib/constants'
import { useStore } from 'hooks'
+import { useProjectContentStore } from 'stores/projectContentStore'
+import { useSqlStore, TAB_TYPES } from 'localStores/sqlEditor/SqlEditorStore'
-interface Props {
- onAddQuery?: () => void
- onRenameQuery?: (query: any) => void
- onDeleteQuery?: (query: any) => void
-}
-
-const SqlEditorMenu: FC<Props> = ({
- onAddQuery = () => {},
- onRenameQuery = () => {},
- onDeleteQuery = () => {},
-}) => {
- const { ui, content } = useStore()
- const router = useRouter()
- const { queryId } = router.query
+import RenameQuery from 'components/to-be-cleaned/SqlEditor/RenameQuery'
+import { createSqlSnippet } from 'components/to-be-cleaned/SqlEditor/SqlEditor.utils'
+import ConfirmationModal from 'components/ui/ConfirmationModal'
+import ProductMenuItem from 'components/ui/ProductMenu/ProductMenuItem'
+
+const OpenQueryItem = observer(({ tabInfo }: { tabInfo: any }) => {
+ const sqlEditorStore: any = useSqlStore()
+ const { id, name } = tabInfo || {}
+ const active = sqlEditorStore.activeTab.id === id
- const queries = content.sqlSnippets()
- const [favouriteQueries, savedQueries] = partition(
- queries,
- (query: any) => query.content.favorite
+ return (
+ <ProductMenuItem
+ key={id}
+ isActive={active}
+ name={name}
+ action={active && <DropdownMenu tabInfo={tabInfo} />}
+ onClick={() => sqlEditorStore.selectTab(id)}
+ textClassName="w-44"
+ />
)
+})
+
+const DropdownMenu = observer(({ tabInfo }: { tabInfo: any }) => {
+ const router = useRouter()
+ const { ref } = router.query
+
+ const sqlEditorStore: any = useSqlStore()
+ const contentStore: any = useProjectContentStore(ref)
- const page = router.pathname.split('/')[4] as undefined | 'templates'
- const projectRef = ui.selectedProject?.ref
- const [searchText, setSearchText] = useState<string>('')
+ const [tabId, setTabId] = useState('')
+ const [renameModalOpen, setRenameModalOpen] = useState(false)
+ const [deleteModalOpen, setDeleteModalOpen] = useState(false)
+
+ const { id, name } = tabInfo || {}
+
+ function onCloseRenameModal() {
+ setRenameModalOpen(false)
+ }
+
+ function renameQuery(e: any) {
+ setTabId(id)
+ setRenameModalOpen(true)
+ }
+
+ function renderMenu() {
+ return (
+ <>
+ <Dropdown.Item onClick={renameQuery} icon={<IconEdit2 size="tiny" />}>
+ Rename query
+ </Dropdown.Item>
+ <Dropdown.Seperator />
+ <Dropdown.Item onClick={() => setDeleteModalOpen(true)} icon={<IconTrash size="tiny" />}>
+ Remove query
+ </Dropdown.Item>
+ </>
+ )
+ }
return (
- <div className="my-6 flex flex-col flex-grow">
- <div className="space-y-6">
- <div className="mx-4 space-y-1">
+ <div>
+ {IS_PLATFORM ? (
+ <Dropdown side="bottom" align="end" overlay={renderMenu()}>
<Button
- block
- icon={<IconPlus />}
+ as="span"
type="text"
- style={{ justifyContent: 'start' }}
- onClick={onAddQuery}
- >
- New query
- </Button>
- <Input
- layout="vertical"
- icon={<IconSearch size="tiny" />}
- className="sbui-input-no-border"
- placeholder="Search for a query"
- onChange={(e) => setSearchText(e.target.value)}
- value={searchText}
- size="tiny"
- actions={
- searchText && (
- <IconX
- size={'tiny'}
- className="cursor-pointer mx-1"
- onClick={() => setSearchText('')}
- />
- )
- }
+ icon={<IconChevronDown size={12} />}
+ style={{ padding: '3px' }}
/>
- </div>
-
- {/* Getting started templates */}
- <div className="mx-4 flex flex-col space-y-2">
- <div className="mx-4 space-y-1">
- <Typography.Text type="secondary" small>
- Getting started
- </Typography.Text>
- </div>
- <div className="space-y-1">
- <Link href={`/project/${projectRef}/sql/templates`}>
- <div>
- <Menu.Item rounded active={page === 'templates'}>
- <Typography.Text className="truncate" small>
- Query Templates
- </Typography.Text>
- </Menu.Item>
- </div>
- </Link>
- </div>
- </div>
-
- {/* Favourites */}
- {favouriteQueries.length > 0 && (
- <div className="mx-4 flex flex-col space-y-2">
- <div className="mx-4 space-y-1">
- <Typography.Text type="secondary" small>
- Favourites
- </Typography.Text>
- </div>
- <div className="space-y-1">
- {favouriteQueries.map((query) => {
- const id = query.id
- const isActive = queryId === id
- return (
- <Link key={id} href={`/project/${projectRef}/sql?queryId=${id}`}>
- <div>
- <Menu.Item rounded active={isActive}>
- <div className="flex justify-between">
- <Typography.Text className="truncate" small>
- {query.name}
- </Typography.Text>
- {isActive && (
- <Dropdown
- side="bottom"
- align="start"
- overlay={[
- <Dropdown.Item
- key="edit-table"
- icon={<IconEdit size="tiny" />}
- onClick={() => onRenameQuery(query)}
- >
- Rename query
- </Dropdown.Item>,
- <Divider key="divider" light />,
- <Dropdown.Item
- key="delete-table"
- icon={<IconTrash size="tiny" />}
- onClick={() => onDeleteQuery(query)}
- >
- Delete query
- </Dropdown.Item>,
- ]}
- >
- <Typography.Text>
- <IconChevronDown size={16} />
- </Typography.Text>
- </Dropdown>
- )}
- </div>
- </Menu.Item>
- </div>
- </Link>
+ </Dropdown>
+ ) : (
+ <Button as="span" type="text" style={{ padding: '3px' }} />
+ )}
+
+ <RenameQuery
+ // @ts-ignore -- @mildtomato not sure what is wrong here
+ visible={renameModalOpen}
+ onCancel={onCloseRenameModal}
+ tabId={tabId}
+ onComplete={onCloseRenameModal}
+ />
+
+ <ConfirmationModal
+ header="Confirm to remove"
+ buttonLabel="Confirm"
+ visible={deleteModalOpen}
+ onSelectConfirm={async () => {
+ await contentStore.del(id)
+ await contentStore.load()
+ sqlEditorStore.closeTab(id)
+ }}
+ onSelectCancel={() => setDeleteModalOpen(false)}
+ >
+ <Modal.Content>
+ <p className="text-sm text-scale-1100 py-4">{`Are you sure you want to remove '${name}' ?`}</p>
+ </Modal.Content>
+ </ConfirmationModal>
+ </div>
+ )
+})
+
+const SideBarContent = observer(() => {
+ const router = useRouter()
+ const { ref } = router.query
+
+ const { ui } = useStore()
+ const { profile: user } = ui
+
+ const sqlEditorStore: any = useSqlStore()
+ const contentStore: any = useProjectContentStore(ref)
+
+ const [filterString, setFilterString] = useState('')
+ const [loadingNewQuery, setLoadingNewQuery] = useState(false)
+
+ async function handleNewQuery() {
+ try {
+ setLoadingNewQuery(true)
+ // create new sql snippet, this also reloads the project_content store
+ // @ts-ignore
+ const snippet = await createSqlSnippet({ router })
+
+ // reload the local sqlEditorStore
+ await sqlEditorStore.loadRemotePersistentData(contentStore, user?.id)
+
+ // select tab with new snippet
+ if (snippet) sqlEditorStore.selectTab(snippet.id)
+ setLoadingNewQuery(false)
+ } catch (error: any) {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to create new query: ${error.message}`,
+ })
+ setLoadingNewQuery(false)
+ }
+ }
+
+ const getStartedTabs = (sqlEditorStore?.tabs ?? []).filter((tab: any) => {
+ return tab.type === TAB_TYPES.WELCOME
+ })
+
+ const favorites = (sqlEditorStore?.tabs ?? []).filter((tab: any) => {
+ return tab.type !== TAB_TYPES.WELCOME && tab.favorite
+ })
+
+ const queries = (sqlEditorStore?.tabs ?? []).filter((tab: any) => {
+ return tab.type !== TAB_TYPES.WELCOME && !tab.favorite
+ })
+
+ const favouriteTabs =
+ filterString.length === 0
+ ? favorites
+ : favorites.filter((tab: any) => tab.name.includes(filterString))
+
+ const queryTabs =
+ filterString.length === 0
+ ? queries
+ : queries.filter((tab: any) => tab.name.includes(filterString))
+
+ return (
+ <div className="mt-6">
+ <Menu type="pills">
+ {IS_PLATFORM && (
+ <div className="my-4 px-3 space-y-1 mx-3">
+ <Button
+ block
+ icon={<IconPlus />}
+ type="default"
+ style={{ justifyContent: 'start' }}
+ onClick={() => handleNewQuery()}
+ loading={loadingNewQuery}
+ >
+ New query
+ </Button>
+ <Input
+ icon={<IconSearch size="tiny" />}
+ placeholder="Search"
+ onChange={(e) => setFilterString(e.target.value)}
+ value={filterString}
+ size="tiny"
+ actions={
+ filterString && (
+ <IconX
+ size={'tiny'}
+ className="cursor-pointer mr-2"
+ onClick={() => setFilterString('')}
+ />
)
- })}
- </div>
+ }
+ />
</div>
)}
- {/* Saved SQL snippets */}
- <div className="mx-4 flex flex-col space-y-2">
- <div className="mx-4 space-y-1">
- <Typography.Text type="secondary" small>
- SQL snippets
- </Typography.Text>
+ {(sqlEditorStore?.tabs ?? []).length === 0 ? (
+ <div className="my-4 px-7 flex items-center space-x-2">
+ <IconLoader className="animate-spin" size={16} strokeWidth={2} />
+ <p className="text-sm">Loading SQL snippets</p>
</div>
- <div className="space-y-1">
- {savedQueries.map((query) => {
- const id = query.id
- const isActive = queryId === id
- return (
- <Link key={id} href={`/project/${projectRef}/sql?queryId=${id}`}>
- <div>
- <Menu.Item rounded active={isActive}>
- <div className="flex justify-between">
- <Typography.Text className="truncate" small>
- {query.name}
- </Typography.Text>
- {isActive && (
- <Dropdown
- side="bottom"
- align="start"
- overlay={[
- <Dropdown.Item
- key="edit-table"
- icon={<IconEdit size="tiny" />}
- onClick={() => onRenameQuery(query)}
- >
- Rename query
- </Dropdown.Item>,
- <Divider key="divider" light />,
- <Dropdown.Item
- key="delete-table"
- icon={<IconTrash size="tiny" />}
- onClick={() => onDeleteQuery(query)}
- >
- Delete query
- </Dropdown.Item>,
- ]}
- >
- <Typography.Text>
- <IconChevronDown size={16} />
- </Typography.Text>
- </Dropdown>
- )}
- </div>
- </Menu.Item>
+ ) : (
+ <div className="space-y-6">
+ {IS_PLATFORM && (
+ <div className="px-3">
+ <Menu.Group title="Getting started" />
+ {getStartedTabs.map((tab: any) => {
+ const { id, name } = tab || {}
+ return (
+ <ProductMenuItem
+ key={id}
+ name={name}
+ isActive={sqlEditorStore.activeTab.id === id}
+ onClick={() => sqlEditorStore.selectTab(id)}
+ />
+ )
+ })}
+ </div>
+ )}
+ <Loading active={contentStore.isInitialized}>
+ <div className="px-3 space-y-6">
+ {favouriteTabs.length >= 1 && (
+ <div className="editor-product-menu">
+ <Menu.Group title="Favorites" />
+ <div className="space-y-1">
+ {favouriteTabs.map((tabInfo: any) => {
+ const { id } = tabInfo || {}
+ return <OpenQueryItem key={id} tabInfo={tabInfo} />
+ })}
+ </div>
</div>
- </Link>
- )
- })}
+ )}
+ {queryTabs.length >= 1 && (
+ <div className="editor-product-menu">
+ <Menu.Group title="SQL snippets" />
+ <div className="space-y-1">
+ {queryTabs.map((tabInfo: any) => {
+ const { id } = tabInfo || {}
+ return <OpenQueryItem key={id} tabInfo={tabInfo} />
+ })}
+ </div>
+ </div>
+ )}
+ {filterString.length > 0 && favouriteTabs.length === 0 && queryTabs.length === 0 && (
+ <div className="px-4">
+ <p className="text-sm">No queries found</p>
+ </div>
+ )}
+ </div>
+ </Loading>
</div>
- </div>
- </div>
+ )}
+ </Menu>
</div>
)
-}
+})
-export default observer(SqlEditorMenu)
+export default SideBarContent
diff --git a/studio/components/layouts/SQLEditorLayout/SQLEditorMenuOld.tsx b/studio/components/layouts/SQLEditorLayout/SQLEditorMenuOld.tsx
deleted file mode 100644
index 780ea5a3d6e..00000000000
--- a/studio/components/layouts/SQLEditorLayout/SQLEditorMenuOld.tsx
+++ /dev/null
@@ -1,277 +0,0 @@
-import { useState } from 'react'
-import { useRouter } from 'next/router'
-import { observer } from 'mobx-react-lite'
-import {
- Button,
- Dropdown,
- Menu,
- Input,
- IconSearch,
- IconPlus,
- IconX,
- IconLoader,
- IconTrash,
- IconChevronDown,
- Loading,
- Modal,
- IconEdit2,
-} from '@supabase/ui'
-
-import { IS_PLATFORM } from 'lib/constants'
-import { useStore } from 'hooks'
-import { useProjectContentStore } from 'stores/projectContentStore'
-import { useSqlStore, TAB_TYPES } from 'localStores/sqlEditor/SqlEditorStore'
-
-import RenameQuery from 'components/to-be-cleaned/SqlEditor/RenameQuery'
-import { createSqlSnippet } from 'components/to-be-cleaned/SqlEditor/SqlEditor.utils'
-import ConfirmationModal from 'components/ui/ConfirmationModal'
-import ProductMenuItem from 'components/ui/ProductMenu/ProductMenuItem'
-
-const OpenQueryItem = observer(({ tabInfo }: { tabInfo: any }) => {
- const sqlEditorStore: any = useSqlStore()
- const { id, name } = tabInfo || {}
- const active = sqlEditorStore.activeTab.id === id
-
- return (
- <ProductMenuItem
- key={id}
- isActive={active}
- name={name}
- action={active && <DropdownMenu tabInfo={tabInfo} />}
- onClick={() => sqlEditorStore.selectTab(id)}
- textClassName="w-44"
- />
- )
-})
-
-const DropdownMenu = observer(({ tabInfo }: { tabInfo: any }) => {
- const router = useRouter()
- const { ref } = router.query
-
- const sqlEditorStore: any = useSqlStore()
- const contentStore: any = useProjectContentStore(ref)
-
- const [tabId, setTabId] = useState('')
- const [renameModalOpen, setRenameModalOpen] = useState(false)
- const [deleteModalOpen, setDeleteModalOpen] = useState(false)
-
- const { id, name } = tabInfo || {}
-
- function onCloseRenameModal() {
- setRenameModalOpen(false)
- }
-
- function renameQuery(e: any) {
- setTabId(id)
- setRenameModalOpen(true)
- }
-
- function renderMenu() {
- return (
- <>
- <Dropdown.Item onClick={renameQuery} icon={<IconEdit2 size="tiny" />}>
- Rename query
- </Dropdown.Item>
- <Dropdown.Seperator />
- <Dropdown.Item onClick={() => setDeleteModalOpen(true)} icon={<IconTrash size="tiny" />}>
- Remove query
- </Dropdown.Item>
- </>
- )
- }
-
- return (
- <div>
- {IS_PLATFORM ? (
- <Dropdown side="bottom" align="end" overlay={renderMenu()}>
- <Button
- as="span"
- type="text"
- icon={<IconChevronDown size={12} />}
- style={{ padding: '3px' }}
- />
- </Dropdown>
- ) : (
- <Button as="span" type="text" style={{ padding: '3px' }} />
- )}
-
- <RenameQuery
- // @ts-ignore -- @mildtomato not sure what is wrong here
- visible={renameModalOpen}
- onCancel={onCloseRenameModal}
- tabId={tabId}
- onComplete={onCloseRenameModal}
- />
-
- <ConfirmationModal
- header="Confirm to remove"
- buttonLabel="Confirm"
- visible={deleteModalOpen}
- onSelectConfirm={async () => {
- await contentStore.del(id)
- await contentStore.load()
- sqlEditorStore.closeTab(id)
- }}
- onSelectCancel={() => setDeleteModalOpen(false)}
- >
- <Modal.Content>
- <p className="text-sm text-scale-1100 py-4">{`Are you sure you want to remove '${name}' ?`}</p>
- </Modal.Content>
- </ConfirmationModal>
- </div>
- )
-})
-
-const SideBarContent = observer(() => {
- const router = useRouter()
- const { ref } = router.query
-
- const { ui } = useStore()
- const { profile: user } = ui
-
- const sqlEditorStore: any = useSqlStore()
- const contentStore: any = useProjectContentStore(ref)
-
- const [filterString, setFilterString] = useState('')
- const [loadingNewQuery, setLoadingNewQuery] = useState(false)
-
- async function handleNewQuery() {
- try {
- setLoadingNewQuery(true)
- // create new sql snippet, this also reloads the project_content store
- // @ts-ignore
- const snippet = await createSqlSnippet({ router })
-
- // reload the local sqlEditorStore
- await sqlEditorStore.loadRemotePersistentData(contentStore, user?.id)
-
- // select tab with new snippet
- if (snippet) sqlEditorStore.selectTab(snippet.id)
- setLoadingNewQuery(false)
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: `Failed to create new query: ${error.message}`,
- })
- setLoadingNewQuery(false)
- }
- }
-
- const getStartedTabs = (sqlEditorStore?.tabs ?? []).filter((tab: any) => {
- return tab.type === TAB_TYPES.WELCOME
- })
-
- const favorites = (sqlEditorStore?.tabs ?? []).filter((tab: any) => {
- return tab.type !== TAB_TYPES.WELCOME && tab.favorite
- })
-
- const queries = (sqlEditorStore?.tabs ?? []).filter((tab: any) => {
- return tab.type !== TAB_TYPES.WELCOME && !tab.favorite
- })
-
- const favouriteTabs =
- filterString.length === 0
- ? favorites
- : favorites.filter((tab: any) => tab.name.includes(filterString))
-
- const queryTabs =
- filterString.length === 0
- ? queries
- : queries.filter((tab: any) => tab.name.includes(filterString))
-
- return (
- <div className="mt-6">
- <Menu type="pills">
- {IS_PLATFORM && (
- <div className="my-4 px-3 space-y-1 mx-3">
- <Button
- block
- icon={<IconPlus />}
- type="default"
- style={{ justifyContent: 'start' }}
- onClick={() => handleNewQuery()}
- loading={loadingNewQuery}
- >
- New query
- </Button>
- <Input
- icon={<IconSearch size="tiny" />}
- placeholder="Search"
- onChange={(e) => setFilterString(e.target.value)}
- value={filterString}
- size="tiny"
- actions={
- filterString && (
- <IconX
- size={'tiny'}
- className="cursor-pointer mr-2"
- onClick={() => setFilterString('')}
- />
- )
- }
- />
- </div>
- )}
-
- {(sqlEditorStore?.tabs ?? []).length === 0 ? (
- <div className="my-4 px-7 flex items-center space-x-2">
- <IconLoader className="animate-spin" size={16} strokeWidth={2} />
- <p className="text-sm">Loading SQL snippets</p>
- </div>
- ) : (
- <div className="space-y-6">
- {IS_PLATFORM && (
- <div className="px-3">
- <Menu.Group title="Getting started" />
- {getStartedTabs.map((tab: any) => {
- const { id, name } = tab || {}
- return (
- <ProductMenuItem
- key={id}
- name={name}
- isActive={sqlEditorStore.activeTab.id === id}
- onClick={() => sqlEditorStore.selectTab(id)}
- />
- )
- })}
- </div>
- )}
- <Loading active={contentStore.isInitialized}>
- <div className="px-3 space-y-6">
- {favouriteTabs.length >= 1 && (
- <div className="editor-product-menu">
- <Menu.Group title="Favorites" />
- <div className="space-y-1">
- {favouriteTabs.map((tabInfo: any) => {
- const { id } = tabInfo || {}
- return <OpenQueryItem key={id} tabInfo={tabInfo} />
- })}
- </div>
- </div>
- )}
- {queryTabs.length >= 1 && (
- <div className="editor-product-menu">
- <Menu.Group title="SQL snippets" />
- <div className="space-y-1">
- {queryTabs.map((tabInfo: any) => {
- const { id } = tabInfo || {}
- return <OpenQueryItem key={id} tabInfo={tabInfo} />
- })}
- </div>
- </div>
- )}
- {filterString.length > 0 && favouriteTabs.length === 0 && queryTabs.length === 0 && (
- <div className="px-4">
- <p className="text-sm">No queries found</p>
- </div>
- )}
- </div>
- </Loading>
- </div>
- )}
- </Menu>
- </div>
- )
-})
-
-export default SideBarContent