Skip to content

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

Move some types and functions into utils and types files.

Ivan Vasilov55b61e8d0615
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
index 31021b8911e..2f98b98e936 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
@@ -1,4 +1,24 @@
import { useParams, useTelemetryProps } from 'common'
+import { AnimatePresence, motion } from 'framer-motion'
+import dynamic from 'next/dynamic'
+import { useRouter } from 'next/router'
+import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react'
+import Split from 'react-split'
+import { format } from 'sql-formatter'
+import {
+ AiIconAnimation,
+ Button,
+ cn,
+ Dropdown,
+ IconCheck,
+ IconChevronDown,
+ IconCornerDownLeft,
+ IconLoader,
+ IconSettings,
+ IconX,
+ Input_Shadcn_,
+} from 'ui'
+
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
import { useSqlEditMutation } from 'data/ai/sql-edit-mutation'
@@ -7,8 +27,6 @@ import { useSqlTitleGenerateMutation } from 'data/ai/sql-title-mutation'
import { SqlSnippet } from 'data/content/sql-snippets-query'
import { useEntityDefinitionsQuery } from 'data/database/entity-definitions-query'
import { useExecuteSqlMutation } from 'data/sql/execute-sql-mutation'
-import { isError } from 'data/utils/error-check'
-import { AnimatePresence, motion } from 'framer-motion'
import {
useLocalStorage,
useLocalStorageQuery,
@@ -21,34 +39,8 @@ import { IS_PLATFORM } from 'lib/constants'
import { uuidv4 } from 'lib/helpers'
import { useProfile } from 'lib/profile'
import Telemetry from 'lib/telemetry'
-import dynamic from 'next/dynamic'
-import { useRouter } from 'next/router'
-import {
- Dispatch,
- SetStateAction,
- createContext,
- useCallback,
- useContext,
- useEffect,
- useRef,
- useState,
-} from 'react'
-import Split from 'react-split'
-import { format } from 'sql-formatter'
+import { isError } from 'lodash'
import { getSqlEditorStateSnapshot, useSqlEditorStateSnapshot } from 'state/sql-editor'
-import {
- AiIconAnimation,
- Button,
- Dropdown,
- IconCheck,
- IconChevronDown,
- IconCornerDownLeft,
- IconLoader,
- IconSettings,
- IconX,
- Input_Shadcn_,
- cn,
-} from 'ui'
import AISchemaSuggestionPopover from './AISchemaSuggestionPopover'
import AISettingsModal from './AISettingsModal'
import {
@@ -56,8 +48,18 @@ import {
sqlAiDisclaimerComment,
untitledSnippetTitle,
} from './SQLEditor.constants'
-import { IStandaloneCodeEditor, IStandaloneDiffEditor } from './SQLEditor.types'
-import { createSqlSnippetSkeleton } from './SQLEditor.utils'
+import {
+ ContentDiff,
+ DiffType,
+ IStandaloneCodeEditor,
+ IStandaloneDiffEditor,
+ SQLEditorContextValues,
+} from './SQLEditor.types'
+import {
+ createSqlSnippetSkeleton,
+ getDiffTypeButtonLabel,
+ getDiffTypeDropdownLabel,
+} from './SQLEditor.utils'
import UtilityPanel from './UtilityPanel/UtilityPanel'
// Load the monaco editor client-side only (does not behave well server-side)
@@ -67,20 +69,6 @@ const DiffEditor = dynamic(
{ ssr: false }
)
-type ContentDiff = {
- original: string
- modified: string
-}
-
-type SQLEditorContextValues = {
- aiInput: string
- setAiInput: Dispatch<SetStateAction<string>>
- sqlDiff?: ContentDiff
- setSqlDiff: Dispatch<SetStateAction<ContentDiff | undefined>>
- debugSolution?: string
- setDebugSolution: Dispatch<SetStateAction<string | undefined>>
-}
-
const SQLEditorContext = createContext<SQLEditorContextValues | undefined>(undefined)
export function useSqlEditor() {
@@ -93,38 +81,6 @@ export function useSqlEditor() {
return values
}
-enum DiffType {
- Modification = 'modification',
- Addition = 'addition',
- NewSnippet = 'new-snippet',
-}
-
-function getDiffTypeButtonLabel(diffType: DiffType) {
- switch (diffType) {
- case DiffType.Modification:
- return 'Accept change'
- case DiffType.Addition:
- return 'Accept addition'
- case DiffType.NewSnippet:
- return 'Create new snippet'
- default:
- throw new Error(`Unknown diff type '${diffType}'`)
- }
-}
-
-function getDiffTypeDropdownLabel(diffType: DiffType) {
- switch (diffType) {
- case DiffType.Modification:
- return 'Compare as change'
- case DiffType.Addition:
- return 'Compare as addition'
- case DiffType.NewSnippet:
- return 'Compare as new snippet'
- default:
- throw new Error(`Unknown diff type '${diffType}'`)
- }
-}
-
const SQLEditor = () => {
const { ui } = useStore()
const { ref, id } = useParams()
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.types.ts b/studio/components/interfaces/SQLEditor/SQLEditor.types.ts
index 8883eb19037..a084da9cd23 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.types.ts
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.types.ts
@@ -1,4 +1,5 @@
import { DiffOnMount, OnMount } from '@monaco-editor/react'
+import { Dispatch, SetStateAction } from 'react'
export interface SQLTemplate {
id: number
@@ -10,3 +11,23 @@ export interface SQLTemplate {
export type IStandaloneCodeEditor = Parameters<OnMount>[0]
export type IStandaloneDiffEditor = Parameters<DiffOnMount>[0]
+
+export type ContentDiff = {
+ original: string
+ modified: string
+}
+
+export type SQLEditorContextValues = {
+ aiInput: string
+ setAiInput: Dispatch<SetStateAction<string>>
+ sqlDiff?: ContentDiff
+ setSqlDiff: Dispatch<SetStateAction<ContentDiff | undefined>>
+ debugSolution?: string
+ setDebugSolution: Dispatch<SetStateAction<string | undefined>>
+}
+
+export enum DiffType {
+ Modification = 'modification',
+ Addition = 'addition',
+ NewSnippet = 'new-snippet',
+}
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts b/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
index 2452670ac5f..577bf5057eb 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts
@@ -2,6 +2,7 @@
import MarkdownTable from 'markdown-table'
import { NEW_SQL_SNIPPET_SKELETON } from './SQLEditor.constants'
import { SqlSnippets, UserContent } from 'types'
+import { DiffType } from './SQLEditor.types'
export const getResultsMarkdown = (results: any[]) => {
const columns = Object.keys(results[0])
@@ -40,3 +41,29 @@ export const createSqlSnippetSkeleton = ({
},
}
}
+
+export function getDiffTypeButtonLabel(diffType: DiffType) {
+ switch (diffType) {
+ case DiffType.Modification:
+ return 'Accept change'
+ case DiffType.Addition:
+ return 'Accept addition'
+ case DiffType.NewSnippet:
+ return 'Create new snippet'
+ default:
+ throw new Error(`Unknown diff type '${diffType}'`)
+ }
+}
+
+export function getDiffTypeDropdownLabel(diffType: DiffType) {
+ switch (diffType) {
+ case DiffType.Modification:
+ return 'Compare as change'
+ case DiffType.Addition:
+ return 'Compare as addition'
+ case DiffType.NewSnippet:
+ return 'Compare as new snippet'
+ default:
+ throw new Error(`Unknown diff type '${diffType}'`)
+ }
+}