Skip to content

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

small refactor

Joshen Lim2250bbb12869
diff --git a/studio/components/interfaces/SQLEditor/MonacoEditor.tsx b/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
index f102bfe0ffa..5cbfc5b4b40 100644
--- a/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/MonacoEditor.tsx
@@ -49,7 +49,6 @@ const MonacoEditor = ({ id, isExecuting, executeQuery }: MonacoEditorProps) => {
contextMenuGroupId: 'operation',
contextMenuOrder: 0,
run: () => {
- // [Joshen TODO] If executing, prevent executing more
if (isExecuting) return
const selectedValue = (editorRef?.current)
diff --git a/studio/components/interfaces/SQLEditor/TabSqlQuery/FavouriteButton.tsx b/studio/components/interfaces/SQLEditor/TabSqlQuery/FavouriteButton.tsx
deleted file mode 100644
index eb9389602fc..00000000000
--- a/studio/components/interfaces/SQLEditor/TabSqlQuery/FavouriteButton.tsx
+++ /dev/null
@@ -1,146 +0,0 @@
-import { observer } from 'mobx-react-lite'
-import { useState } from 'react'
-import { Button, IconHeart } from 'ui'
-
-import * as Tooltip from '@radix-ui/react-tooltip'
-import { useStore } from 'hooks'
-import Telemetry from 'lib/telemetry'
-import { useProfileQuery } from 'data/profile/profile-query'
-import { useSqlStore } from 'localStores/sqlEditor/SqlEditorStore'
-
-const FavouriteButton = () => {
- const { ui, content: contentStore } = useStore()
- const { data: profile } = useProfileQuery()
-
- const sqlEditorStore: any = useSqlStore()
-
- const [loading, setLoading] = useState(false)
-
- const id = sqlEditorStore.activeTab.id
-
- /*
- * `content` column json structure
- */
- let contentPayload = {
- schema_version: '1.0',
- content_id: id,
- sql: sqlEditorStore.activeTab.query,
- }
-
- async function addToFavorite() {
- try {
- setLoading(true)
- /*
- * remote db handling
- */
- await contentStore.updateSql(id, {
- content: {
- ...contentPayload,
- favorite: true,
- },
- })
-
- /*
- * old localstorage handling
- */
- const { query, name, desc } = sqlEditorStore.activeTab || {}
- sqlEditorStore.addToFavorite(id, query, name, desc)
- Telemetry.sendEvent(
- { category: 'sql_editor', action: 'sql_favourited', label: name },
- ui.googleAnalyticsProps
- )
-
- /*
- * reload sql data in store and re-select tab
- */
- sqlEditorStore.loadTabs(sqlEditorStore.tabsFromContentStore(contentStore, profile?.id), false)
- sqlEditorStore.selectTab(id)
-
- setLoading(false)
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Failed to add to favourites: ${error.message}`,
- })
- setLoading(false)
- }
- }
-
- async function unFavorite() {
- const id = sqlEditorStore.activeTab.id
- try {
- setLoading(true)
- /*
- * remote db handling
- */
- await contentStore.updateSql(id, {
- content: {
- ...contentPayload,
- favorite: false,
- },
- })
-
- /*
- * old localstorage handling
- */
- const { name } = sqlEditorStore.activeTab || {}
- sqlEditorStore.unFavorite(id)
- Telemetry.sendEvent(
- { category: 'sql_editor', action: 'sql_unfavourited', label: name },
- ui.googleAnalyticsProps
- )
-
- /*
- * reload sql data in store and re-select tab
- */
- sqlEditorStore.loadTabs(sqlEditorStore.tabsFromContentStore(contentStore, profile?.id), false)
- sqlEditorStore.selectTab(id)
- setLoading(false)
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Failed to remove from favourites: ${error.message}`,
- })
- setLoading(false)
- }
- }
-
- const isFavorite = sqlEditorStore.activeTab.favorite
-
- return (
- <>
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Portal>
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'border border-scale-200',
- ].join(' ')}
- >
- <span className="text-xs text-scale-1200">
- {isFavorite ? 'Remove' : 'Add'} this query {isFavorite ? 'from' : 'to'} your
- Favourites
- </span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- <Tooltip.Trigger type="button">
- <Button
- type="text"
- size="tiny"
- shadow={false}
- onClick={isFavorite ? unFavorite : addToFavorite}
- loading={loading}
- icon={<IconHeart size="tiny" fill={isFavorite ? '#48bb78' : 'gray'} />}
- />
- </Tooltip.Trigger>
- </Tooltip.Root>
- </>
- )
-}
-
-export default observer(FavouriteButton)
diff --git a/studio/components/interfaces/SQLEditor/TabSqlQuery/SavingIndicator.tsx b/studio/components/interfaces/SQLEditor/TabSqlQuery/SavingIndicator.tsx
deleted file mode 100644
index 1108cefc45c..00000000000
--- a/studio/components/interfaces/SQLEditor/TabSqlQuery/SavingIndicator.tsx
+++ /dev/null
@@ -1,113 +0,0 @@
-import { FC, useState, useEffect } from 'react'
-import { toJS } from 'mobx'
-import { observer } from 'mobx-react-lite'
-import * as Tooltip from '@radix-ui/react-tooltip'
-import { Button, IconAlertCircle, IconCheck, IconLoader, IconRefreshCcw } from 'ui'
-
-import { useStore, usePrevious } from 'hooks'
-import { useSqlStore } from 'localStores/sqlEditor/SqlEditorStore'
-
-interface Props {
- updateSqlSnippet: (value: any) => void
-}
-
-const SavingIndicator: FC<Props> = ({ updateSqlSnippet }) => {
- const { content } = useStore()
- const sqlEditorStore: any = useSqlStore()
- const previousState = usePrevious(content.savingState)
- const [showSavedText, setShowSavedText] = useState(false)
-
- // @ts-ignore
- useEffect(() => {
- let cancel = false
-
- if (
- (previousState === 'CREATING' || previousState === 'UPDATING') &&
- content.savingState === 'IDLE'
- ) {
- setShowSavedText(true)
- setTimeout(() => {
- if (!cancel) setShowSavedText(false)
- }, 3000)
- }
-
- return () => (cancel = true)
- }, [content.savingState])
-
- const retry = () => {
- const [item] = content.list((item: any) => item.id === sqlEditorStore.selectedTabId)
-
- if (content.savingState === 'CREATING_FAILED' && item) {
- content.save(toJS(item))
- }
-
- if (content.savingState === 'UPDATING_FAILED' && item) {
- updateSqlSnippet(sqlEditorStore.activeTab.query)
- }
- }
-
- return (
- <div className="mx-2 flex items-center gap-2">
- {(content.savingState === 'CREATING_FAILED' || content.savingState === 'UPDATING_FAILED') && (
- <Button
- type="text"
- size="tiny"
- shadow={false}
- icon={<IconRefreshCcw className="text-gray-1100" size="tiny" strokeWidth={2} />}
- onClick={retry}
- >
- Retry
- </Button>
- )}
- {(content.savingState === 'CREATING' || content.savingState === 'UPDATING') && (
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger>
- <IconLoader className="animate-spin" size={14} strokeWidth={2} />
- </Tooltip.Trigger>
- <Tooltip.Portal>
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'border border-scale-200',
- ].join(' ')}
- >
- <span className="text-xs text-scale-1200">Saving changes...</span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- </Tooltip.Root>
- )}
- {(content.savingState === 'CREATING_FAILED' || content.savingState === 'UPDATING_FAILED') && (
- <IconAlertCircle className="text-red-900" size={14} strokeWidth={2} />
- )}
- {showSavedText && (
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger>
- <IconCheck className="text-brand-900" size={14} strokeWidth={3} />
- </Tooltip.Trigger>
- <Tooltip.Portal>
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'border border-scale-200 ',
- ].join(' ')}
- >
- <span className="text-xs text-scale-1200">All changes saved</span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- </Tooltip.Root>
- )}
- <span className="text-sm text-scale-1000">
- {content.savingState === 'CREATING_FAILED' && 'Failed to create'}
- {content.savingState === 'UPDATING_FAILED' && 'Failed to save'}
- </span>
- </div>
- )
-}
-
-export default observer(SavingIndicator)
diff --git a/studio/components/interfaces/SQLEditor/TabSqlQuery/SizeToggleButton.tsx b/studio/components/interfaces/SQLEditor/TabSqlQuery/SizeToggleButton.tsx
deleted file mode 100644
index f96f39d0018..00000000000
--- a/studio/components/interfaces/SQLEditor/TabSqlQuery/SizeToggleButton.tsx
+++ /dev/null
@@ -1,50 +0,0 @@
-import { observer } from 'mobx-react-lite'
-import { useSqlStore } from 'localStores/sqlEditor/SqlEditorStore'
-import { Button, IconChevronDown, IconChevronUp } from 'ui'
-
-const SizeToggleButton = () => {
- const sqlEditorStore: any = useSqlStore()
-
- function maximizeEditor() {
- sqlEditorStore.activeTab.collapseUtilityTab()
- }
-
- function restorePanelSize() {
- sqlEditorStore.activeTab.restorePanelSize()
- }
-
- return (
- <>
- {sqlEditorStore.activeTab.utilityTabHeight != 0 && (
- <Button
- type="text"
- size="tiny"
- shadow={false}
- onClick={maximizeEditor}
- icon={<IconChevronDown className="text-gray-1100" size="tiny" strokeWidth={2} />}
- // @ts-ignore
- tooltip={{
- title: 'Maximize editor',
- position: 'top',
- }}
- />
- )}
- {sqlEditorStore.activeTab.utilityTabHeight == 0 && (
- <Button
- type="text"
- size="tiny"
- shadow={false}
- onClick={restorePanelSize}
- icon={<IconChevronUp className="text-gray-1100" size="tiny" strokeWidth={2} />}
- // @ts-ignore
- tooltip={{
- title: 'Restore panel size',
- position: 'top',
- }}
- />
- )}
- </>
- )
-}
-
-export default observer(SizeToggleButton)
diff --git a/studio/components/interfaces/SQLEditor/TabSqlQuery/UtilityActions.tsx b/studio/components/interfaces/SQLEditor/TabSqlQuery/UtilityActions.tsx
deleted file mode 100644
index af809c2676a..00000000000
--- a/studio/components/interfaces/SQLEditor/TabSqlQuery/UtilityActions.tsx
+++ /dev/null
@@ -1,84 +0,0 @@
-import { Button, IconAlertCircle } from 'ui'
-import * as Tooltip from '@radix-ui/react-tooltip'
-import { PermissionAction } from '@supabase/shared-types/out/constants'
-
-import { useKeyboardShortcuts, checkPermissions } from 'hooks'
-import { IS_PLATFORM } from 'lib/constants'
-import { useProfileQuery } from 'data/profile/profile-query'
-import { useSqlStore } from 'localStores/sqlEditor/SqlEditorStore'
-import SavingIndicator from './SavingIndicator'
-import FavouriteButton from './FavouriteButton'
-import SizeToggleButton from './SizeToggleButton'
-
-export interface UtilityActionsProps {
- updateSqlSnippet: (value: any) => void
-}
-
-const UtilityActions = ({ updateSqlSnippet }: UtilityActionsProps) => {
- const { data: profile } = useProfileQuery()
- const sqlEditorStore: any = useSqlStore()
-
- const canCreateSQLSnippet = checkPermissions(PermissionAction.CREATE, 'user_content', {
- resource: { type: 'sql', owner_id: profile?.id },
- subject: { id: profile?.id },
- })
-
- useKeyboardShortcuts(
- {
- 'Command+Enter': (event: any) => {
- event.preventDefault()
- executeQuery()
- },
- },
- // @ts-ignore
- ['INPUT']
- )
-
- async function executeQuery() {
- if (sqlEditorStore.isExecuting) return
- await sqlEditorStore.startExecuting()
- }
-
- return (
- <>
- {!canCreateSQLSnippet && (
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger>
- <IconAlertCircle size={14} strokeWidth={2} />
- </Tooltip.Trigger>
- <Tooltip.Portal>
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'w-48 border border-scale-200',
- ].join(' ')}
- >
- <span className="text-xs text-scale-1200">
- Queries are not saved as you do not have sufficient permissions
- </span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- </Tooltip.Root>
- )}
- <SavingIndicator updateSqlSnippet={updateSqlSnippet} />
- {IS_PLATFORM && canCreateSQLSnippet && <FavouriteButton />}
- <SizeToggleButton />
- <Button
- onClick={executeQuery}
- disabled={sqlEditorStore.isExecuting}
- loading={sqlEditorStore.isExecuting}
- type="text"
- size="tiny"
- shadow={false}
- className="mx-2"
- >
- RUN
- </Button>
- </>
- )
-}
-
-export default UtilityActions
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/FavoriteButton.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/FavoriteButton.tsx
index 667b659f253..19941861f90 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/FavoriteButton.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/FavoriteButton.tsx
@@ -1,4 +1,4 @@
-import { Button, IconHeart } from '@supabase/ui'
+import { Button, IconHeart } from 'ui'
import { useQueryClient } from '@tanstack/react-query'
import { contentKeys } from 'data/content/keys'
import { Content, ContentData } from 'data/content/content-query'
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/ResultsDropdown.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/ResultsDropdown.tsx
index b59ec3b6c22..1fb97f2f787 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/ResultsDropdown.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/ResultsDropdown.tsx
@@ -1,4 +1,4 @@
-import { Button, Dropdown, IconChevronDown } from '@supabase/ui'
+import { Button, Dropdown, IconChevronDown } from 'ui'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { useStore } from 'hooks'
import { copyToClipboard } from 'lib/helpers'
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/SavingIndicator.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/SavingIndicator.tsx
index 7fcd47ad913..8dac349236f 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/SavingIndicator.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/SavingIndicator.tsx
@@ -1,5 +1,5 @@
import * as Tooltip from '@radix-ui/react-tooltip'
-import { Button, IconAlertCircle, IconCheck, IconLoader, IconRefreshCcw } from '@supabase/ui'
+import { Button, IconAlertCircle, IconCheck, IconLoader, IconRefreshCcw } from 'ui'
import { usePrevious } from 'hooks'
import { useEffect, useState } from 'react'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/SizeToggleButton.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/SizeToggleButton.tsx
new file mode 100644
index 00000000000..3420658af4a
--- /dev/null
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/SizeToggleButton.tsx
@@ -0,0 +1,73 @@
+import { Button, IconChevronDown, IconChevronUp } from 'ui'
+import { useSqlEditorStateSnapshot } from 'state/sql-editor'
+import * as Tooltip from '@radix-ui/react-tooltip'
+
+export interface SizeToggleButton {
+ id: string
+}
+
+const SizeToggleButton = ({ id }: SizeToggleButton) => {
+ const snap = useSqlEditorStateSnapshot()
+ const snippet = snap.snippets[id]
+ const isUtilityPanelCollapsed = (snippet?.splitSizes?.[1] ?? 0) === 0
+
+ const maximizeEditor = () => snap.collapseUtilityPanel(id)
+ const restorePanelSize = () => snap.restoreUtilityPanel(id)
+
+ if (!snippet) return null
+ return isUtilityPanelCollapsed ? (
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button
+ as="span"
+ type="text"
+ size="tiny"
+ shadow={false}
+ onClick={restorePanelSize}
+ icon={<IconChevronUp className="text-gray-1100" size="tiny" strokeWidth={2} />}
+ />
+ </Tooltip.Trigger>
+ <Tooltip.Portal>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
+ 'border border-scale-200',
+ ].join(' ')}
+ >
+ <span className="text-xs text-scale-1200">Show results</span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Portal>
+ </Tooltip.Root>
+ ) : (
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button
+ as="span"
+ type="text"
+ size="tiny"
+ shadow={false}
+ onClick={maximizeEditor}
+ icon={<IconChevronDown className="text-gray-1100" size="tiny" strokeWidth={2} />}
+ />
+ </Tooltip.Trigger>
+ <Tooltip.Portal>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
+ 'border border-scale-200',
+ ].join(' ')}
+ >
+ <span className="text-xs text-scale-1200">Collapse results</span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Portal>
+ </Tooltip.Root>
+ )
+}
+
+export default SizeToggleButton
diff --git a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
index 0ed82119aed..a0432b88978 100644
--- a/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
+++ b/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx
@@ -1,9 +1,8 @@
-import { Button, IconChevronDown, IconChevronUp } from '@supabase/ui'
+import { Button } from 'ui'
import { IS_PLATFORM } from 'lib/constants'
-import { useSqlEditorStateSnapshot } from 'state/sql-editor'
import FavoriteButton from './FavoriteButton'
import SavingIndicator from './SavingIndicator'
-import * as Tooltip from '@radix-ui/react-tooltip'
+import SizeToggleButton from './SizeToggleButton'
export type UtilityActionsProps = {
id: string
@@ -31,68 +30,5 @@ const UtilityActions = ({ id, isExecuting = false, executeQuery }: UtilityAction
</>
)
}
-const SizeToggleButton = ({ id }: { id: string }) => {
- const snap = useSqlEditorStateSnapshot()
- const snippet = snap.snippets[id]
- const isUtilityPanelCollapsed = (snippet?.splitSizes?.[1] ?? 0) === 0
-
- const maximizeEditor = () => snap.collapseUtilityPanel(id)
- const restorePanelSize = () => snap.restoreUtilityPanel(id)
-
- if (!snippet) return null
- return isUtilityPanelCollapsed ? (
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger>
- <Button
- as="span"
- type="text"
- size="tiny"
- shadow={false}
- onClick={restorePanelSize}
- icon={<IconChevronUp className="text-gray-1100" size="tiny" strokeWidth={2} />}
- />
- </Tooltip.Trigger>
- <Tooltip.Portal>
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'border border-scale-200',
- ].join(' ')}
- >
- <span className="text-xs text-scale-1200">Show results</span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- </Tooltip.Root>
- ) : (
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger>
- <Button
- as="span"
- type="text"
- size="tiny"
- shadow={false}
- onClick={maximizeEditor}
- icon={<IconChevronDown className="text-gray-1100" size="tiny" strokeWidth={2} />}
- />
- </Tooltip.Trigger>
- <Tooltip.Portal>
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'border border-scale-200',
- ].join(' ')}
- >
- <span className="text-xs text-scale-1200">Collapse results</span>
- </div>
- </Tooltip.Content>
- </Tooltip.Portal>
- </Tooltip.Root>
- )
-}
export default UtilityActions