Skip to content

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

feat(sql-editor): ai schema suggestion popover

Greg Richardsone722c30d2304
diff --git a/studio/components/interfaces/SQLEditor/AISchemaSuggestionPopover.tsx b/studio/components/interfaces/SQLEditor/AISchemaSuggestionPopover.tsx
new file mode 100644
index 00000000000..82812f0fed7
--- /dev/null
+++ b/studio/components/interfaces/SQLEditor/AISchemaSuggestionPopover.tsx
@@ -0,0 +1,101 @@
+import * as Popover from '@radix-ui/react-popover'
+import { motion } from 'framer-motion'
+import { useLocalStorageQuery, useSelectedOrganization, useSelectedProject } from 'hooks'
+import { IS_PLATFORM } from 'lib/constants'
+import { PropsWithChildren, useEffect, useState } from 'react'
+import { Button, IconInfo } from 'ui'
+
+export interface AISchemaSuggestionPopoverProps {
+ delay?: number
+ onClickSettings?: () => void
+}
+
+const AISchemaSuggestionPopover = ({
+ children,
+ delay = 300,
+ onClickSettings,
+}: PropsWithChildren<AISchemaSuggestionPopoverProps>) => {
+ const selectedOrganization = useSelectedOrganization()
+ const isOptedInToAI =
+ selectedOrganization?.opt_in_tags?.includes('AI_SQL_GENERATOR_OPT_IN') ?? false
+ const [isOptedInToAISchema] = useLocalStorageQuery('supabase_sql-editor-ai-schema', false)
+ const [isDelayComplete, setIsDelayComplete] = useState(false)
+ const [aiQueryCount] = useLocalStorageQuery('supabase_sql-editor-ai-query-count', 0)
+
+ const includeSchemaMetadata = (isOptedInToAI || !IS_PLATFORM) && isOptedInToAISchema
+
+ const [isSchemaSuggestionDismissed, setIsSchemaSuggestionDismissed] = useLocalStorageQuery(
+ 'supabase_sql-editor-ai-schema-suggestion-dismissed',
+ false
+ )
+
+ useEffect(() => {
+ const timeout = window.setTimeout(() => {
+ setIsDelayComplete(true)
+ }, delay)
+
+ return () => window.clearTimeout(timeout)
+ })
+
+ return (
+ <Popover.Root
+ open={
+ isDelayComplete &&
+ !includeSchemaMetadata &&
+ aiQueryCount >= 3 &&
+ !isSchemaSuggestionDismissed
+ }
+ >
+ <Popover.Anchor asChild>{children}</Popover.Anchor>
+
+ <Popover.Portal>
+ <Popover.Content side="bottom" sideOffset={5}>
+ <motion.div
+ variants={{
+ visible: {
+ opacity: 1,
+ y: 0,
+ },
+ hidden: {
+ opacity: 0,
+ y: -10,
+ },
+ }}
+ initial="hidden"
+ animate="visible"
+ >
+ <Popover.Arrow className="fill-scale-300 dark:fill-scale-500" />
+ <div className="flex flex-col gap-2 border border-scale-300 dark:border-scale-500 rounded-md p-4 bg-scale-300 shadow-xl">
+ <div className="flex flex-row items-center gap-4 max-w-md">
+ <IconInfo className="w-6 h-6" />
+ <p className="text-sm">
+ Generate more relevant queries by including database metadata in your requests.
+ </p>
+ </div>
+ <div className="flex flex-row gap-2 justify-end">
+ <Button
+ type="default"
+ onClick={() => {
+ setIsSchemaSuggestionDismissed(true)
+ }}
+ >
+ Dismiss
+ </Button>
+ <Button
+ onClick={() => {
+ setIsSchemaSuggestionDismissed(true)
+ onClickSettings?.()
+ }}
+ >
+ Open settings
+ </Button>
+ </div>
+ </div>
+ </motion.div>
+ </Popover.Content>
+ </Popover.Portal>
+ </Popover.Root>
+ )
+}
+
+export default AISchemaSuggestionPopover
diff --git a/studio/components/interfaces/SQLEditor/AISettingsModal.tsx b/studio/components/interfaces/SQLEditor/AISettingsModal.tsx
index defa6f52e73..61fb17d4ce0 100644
--- a/studio/components/interfaces/SQLEditor/AISettingsModal.tsx
+++ b/studio/components/interfaces/SQLEditor/AISettingsModal.tsx
@@ -27,7 +27,7 @@ const AISettingsModal = (props: ModalProps) => {
<p className="text-sm">Include database metadata in AI requests</p>
<p className="text-sm text-scale-1000">
Includes table names, column names and their corresponding data types in the request.
- This will generate snippets that are more relevant to your project.
+ This will generate queries that are more relevant to your project.
</p>
</div>
<Toggle
diff --git a/studio/components/interfaces/SQLEditor/SQLEditor.tsx b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
index c54bfff9e4a..14f12c9f462 100644
--- a/studio/components/interfaces/SQLEditor/SQLEditor.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLEditor.tsx
@@ -46,6 +46,7 @@ import {
IconX,
Input,
} from 'ui'
+import AISchemaSuggestionPopover from './AISchemaSuggestionPopover'
import AISettingsModal from './AISettingsModal'
import { sqlAiDisclaimerComment, untitledSnippetTitle } from './SQLEditor.constants'
import { IStandaloneCodeEditor, IStandaloneDiffEditor } from './SQLEditor.types'
@@ -143,6 +144,11 @@ const SQLEditor = () => {
selectedOrganization?.opt_in_tags?.includes('AI_SQL_GENERATOR_OPT_IN') ?? false
const [isOptedInToAISchema] = useLocalStorageQuery('supabase_sql-editor-ai-schema', false)
const [isAcceptDiffLoading, setIsAcceptDiffLoading] = useState(false)
+ const [, setAiQueryCount] = useLocalStorageQuery('supabase_sql-editor-ai-query-count', 0)
+ const [, setIsSchemaSuggestionDismissed] = useLocalStorageQuery(
+ 'supabase_sql-editor-ai-schema-suggestion-dismissed',
+ false
+ )
const includeSchemaMetadata = (isOptedInToAI || !IS_PLATFORM) && isOptedInToAISchema
@@ -416,247 +422,259 @@ const SQLEditor = () => {
}}
/>
<div className="flex h-full flex-col">
- <motion.div
- key="ask-ai-input-container"
- layoutId="ask-ai-input-container"
- variants={{
- visible: {
- borderRadius: 0,
- },
+ <AISchemaSuggestionPopover
+ onClickSettings={() => {
+ setIsAISettingsOpen(true)
}}
- initial="visible"
- animate="visible"
- className="w-full flex justify-center z-10 mt-0.5 bg-brand-400"
>
- <AnimatePresence initial={false} exitBeforeEnter>
- {!isEditSqlLoading ? (
- <motion.div
- key="ask-ai-input"
- className="w-full"
- variants={{
- visible: {
- opacity: 1,
- y: 0,
- },
- hidden: {
- opacity: 0,
- y: -25,
- },
- }}
- initial="hidden"
- animate="visible"
- exit="hidden"
- transition={{
- duration: 0.1,
- }}
- >
- <Input
- autoFocus
- size="xlarge"
- value={aiInput}
- onChange={(e) => setAiInput(e.currentTarget.value)}
- disabled={isDiffOpen}
- inputRef={inputRef}
- icon={
- <div className="h-full flex flex-row gap-3 items-center">
- <motion.div layoutId="ask-ai-input-icon" className="ml-1">
- <AiIcon className="w-4 h-4" />
- </motion.div>
-
- {debugSolution && (
- <div className="h-full w-full flex flex-row items-center overflow-y-hidden mr-[16.5rem] text-sm text-white">
- {debugSolution}
- </div>
- )}
- </div>
- }
- inputClassName="w-full !border-brand-900 border-none bg-transparent !shadow-none py-4 focus:!ring-0 placeholder:text-scale-900 pr-20"
- iconContainerClassName="transition text-scale-800 text-brand-900"
- placeholder={!debugSolution ? 'Ask Supabase AI to modify your query' : ''}
+ <motion.div
+ key="ask-ai-input-container"
+ layoutId="ask-ai-input-container"
+ variants={{
+ visible: {
+ borderRadius: 0,
+ },
+ }}
+ initial="visible"
+ animate="visible"
+ className="w-full flex justify-center z-10 mt-0.5 bg-brand-400"
+ >
+ <AnimatePresence initial={false} exitBeforeEnter>
+ {!isEditSqlLoading ? (
+ <motion.div
+ key="ask-ai-input"
className="w-full"
- actions={
- <div className="flex flex-row items-center gap-2 space-x-1 mr-6">
- {isDiffOpen ? (
- <>
- <div className="flex items-center">
+ variants={{
+ visible: {
+ opacity: 1,
+ y: 0,
+ },
+ hidden: {
+ opacity: 0,
+ y: -25,
+ },
+ }}
+ initial="hidden"
+ animate="visible"
+ exit="hidden"
+ transition={{
+ duration: 0.1,
+ }}
+ >
+ <Input
+ autoFocus
+ size="xlarge"
+ value={aiInput}
+ onChange={(e) => setAiInput(e.currentTarget.value)}
+ disabled={isDiffOpen}
+ inputRef={inputRef}
+ icon={
+ <div className="h-full flex flex-row gap-3 items-center">
+ <motion.div layoutId="ask-ai-input-icon" className="ml-1">
+ <AiIcon className="w-4 h-4" />
+ </motion.div>
+
+ {debugSolution && (
+ <div className="h-full w-full flex flex-row items-center overflow-y-hidden mr-[16.5rem] text-sm text-white">
+ {debugSolution}
+ </div>
+ )}
+ </div>
+ }
+ inputClassName="w-full !border-brand-900 border-none bg-transparent !shadow-none py-4 focus:!ring-0 placeholder:text-scale-900 pr-20"
+ iconContainerClassName="transition text-scale-800 text-brand-900"
+ placeholder={!debugSolution ? 'Ask Supabase AI to modify your query' : ''}
+ className="w-full"
+ actions={
+ <div className="flex flex-row items-center gap-2 space-x-1 mr-6">
+ {isDiffOpen ? (
+ <>
+ <div className="flex items-center">
+ <Button
+ className="rounded-r-none"
+ type="primary"
+ size="tiny"
+ icon={
+ !isAcceptDiffLoading ? (
+ <IconCheck />
+ ) : (
+ <IconLoader className="animate-spin" size={14} />
+ )
+ }
+ iconRight={
+ <div className="opacity-30">
+ <IconCornerDownLeft size={12} strokeWidth={1.5} />
+ </div>
+ }
+ onClick={acceptAiHandler}
+ >
+ {getDiffTypeButtonLabel(selectedDiffType)}
+ </Button>
+ <Dropdown
+ align="end"
+ side="bottom"
+ overlay={Object.values(DiffType)
+ .filter((diffType) => diffType !== selectedDiffType)
+ .map((diffType) => (
+ <Dropdown.Item
+ key={diffType}
+ onClick={() => {
+ setSelectedDiffType(diffType)
+ switch (diffType) {
+ case DiffType.Modification:
+ return compareAsModification()
+ case DiffType.Addition:
+ return compareAsAddition()
+ case DiffType.NewSnippet:
+ return compareAsNewSnippet()
+ default:
+ throw new Error(`Unknown diff type '${diffType}'`)
+ }
+ }}
+ >
+ {getDiffTypeDropdownLabel(diffType)}
+ </Dropdown.Item>
+ ))}
+ >
+ <Button
+ type="primary"
+ className="rounded-l-none border-l-0 px-[4px] py-[5px]"
+ icon={<IconChevronDown />}
+ />
+ </Dropdown>
+ </div>
<Button
- className="rounded-r-none"
- type="primary"
+ type="alternative"
size="tiny"
- icon={
- !isAcceptDiffLoading ? (
- <IconCheck />
- ) : (
- <IconLoader className="animate-spin" size={14} />
- )
- }
- iconRight={
- <div className="opacity-30">
- <IconCornerDownLeft size={12} strokeWidth={1.5} />
- </div>
- }
- onClick={acceptAiHandler}
+ icon={<IconX />}
+ iconRight={<span className="opacity-30">ESC</span>}
+ onClick={discardAiHandler}
>
- {getDiffTypeButtonLabel(selectedDiffType)}
+ Discard
</Button>
- <Dropdown
- align="end"
- side="bottom"
- overlay={Object.values(DiffType)
- .filter((diffType) => diffType !== selectedDiffType)
- .map((diffType) => (
- <Dropdown.Item
- key={diffType}
- onClick={() => {
- setSelectedDiffType(diffType)
- switch (diffType) {
- case DiffType.Modification:
- return compareAsModification()
- case DiffType.Addition:
- return compareAsAddition()
- case DiffType.NewSnippet:
- return compareAsNewSnippet()
- default:
- throw new Error(`Unknown diff type '${diffType}'`)
- }
- }}
- >
- {getDiffTypeDropdownLabel(diffType)}
- </Dropdown.Item>
- ))}
- >
- <Button
- type="primary"
- className="rounded-l-none border-l-0 px-[4px] py-[5px]"
- icon={<IconChevronDown />}
- />
- </Dropdown>
- </div>
- <Button
- type="alternative"
- size="tiny"
- icon={<IconX />}
- iconRight={<span className="opacity-30">ESC</span>}
- onClick={discardAiHandler}
- >
- Discard
- </Button>
- </>
- ) : (
- <>
- <IconCornerDownLeft size={16} strokeWidth={1.5} />
- <IconSettings
- className="cursor-pointer"
- onClick={() => setIsAISettingsOpen(true)}
- />
- </>
- )}
- </div>
- }
- onKeyPress={async (e) => {
- if (e.key === 'Enter') {
- try {
- const prompt = e.currentTarget.value
-
- if (!prompt) {
- return
- }
+ </>
+ ) : (
+ <>
+ <IconCornerDownLeft size={16} strokeWidth={1.5} />
+ <IconSettings
+ className="cursor-pointer"
+ onClick={() => {
+ setIsSchemaSuggestionDismissed(true)
+ setIsAISettingsOpen(true)
+ }}
+ />
+ </>
+ )}
+ </div>
+ }
+ onKeyPress={async (e) => {
+ if (e.key === 'Enter') {
+ try {
+ const prompt = e.currentTarget.value
- const sql = editorRef.current?.getValue()
+ if (!prompt) {
+ return
+ }
- if (!sql) {
- return
- }
+ const sql = editorRef.current?.getValue()
- const { sql: modifiedSql } = await editSql({
- prompt,
- sql: sql.replace(sqlAiDisclaimerComment, '').trim(),
- entityDefinitions,
- })
+ if (!sql) {
+ return
+ }
- const formattedSql =
- sqlAiDisclaimerComment +
- '\n\n' +
- format(modifiedSql, {
- language: 'postgresql',
- keywordCase: 'lower',
+ const { sql: modifiedSql } = await editSql({
+ prompt,
+ sql: sql.replace(sqlAiDisclaimerComment, '').trim(),
+ entityDefinitions,
})
- // TODO: show error
- if (formattedSql.trim() === sql.trim()) {
- ui.setNotification({
- category: 'error',
- message: 'Unable to edit SQL. Try adding more details to your prompt.',
- })
- return
- }
+ setAiQueryCount((count) => count + 1)
- setSqlDiff({
- original: sql,
- modified: formattedSql,
- })
- } catch (error: unknown) {
- if (isError(error)) {
- ui.setNotification({
- category: 'error',
- message: error.message,
+ const formattedSql =
+ sqlAiDisclaimerComment +
+ '\n\n' +
+ format(modifiedSql, {
+ language: 'postgresql',
+ keywordCase: 'lower',
+ })
+
+ // TODO: show error
+ if (formattedSql.trim() === sql.trim()) {
+ ui.setNotification({
+ category: 'error',
+ message:
+ 'Unable to edit SQL. Try adding more details to your prompt.',
+ })
+ return
+ }
+
+ setSqlDiff({
+ original: sql,
+ modified: formattedSql,
})
+ } catch (error: unknown) {
+ if (isError(error)) {
+ ui.setNotification({
+ category: 'error',
+ message: error.message,
+ })
+ }
}
}
- }
- }}
- />
- </motion.div>
- ) : (
- <motion.div
- key="ask-ai-loading"
- className="p-4 flex flex-row gap-2 items-center w-full"
- variants={{
- visible: {
- opacity: 1,
- y: 0,
- },
- hidden: {
- opacity: 0,
- y: 25,
- },
- }}
- transition={{
- duration: 0.2,
- }}
- initial="hidden"
- animate="visible"
- exit="hidden"
- >
+ }}
+ />
+ </motion.div>
+ ) : (
<motion.div
- className="text-brand-900"
- animate={{
- scale: [0.9, 1.1, 0.9],
- transition: {
- ease: 'linear',
- duration: 2,
- repeat: Infinity,
+ key="ask-ai-loading"
+ className="p-4 flex flex-row gap-2 items-center w-full"
+ variants={{
+ visible: {
+ opacity: 1,
+ y: 0,
},
- }}
- >
- <AiIcon className="w-4 h-4" />
- </motion.div>
- <motion.span
- animate={{
- opacity: ['0.5', '0.75', '0.5'],
- transition: {
- ease: 'linear',
- duration: 2,
- repeat: Infinity,
+ hidden: {
+ opacity: 0,
+ y: 25,
},
}}
+ transition={{
+ duration: 0.2,
+ }}
+ initial="hidden"
+ animate="visible"
+ exit="hidden"
>
- Thinking...
- </motion.span>
- </motion.div>
- )}
- </AnimatePresence>
- </motion.div>
+ <motion.div
+ className="text-brand-900"
+ animate={{
+ scale: [0.9, 1.1, 0.9],
+ transition: {
+ ease: 'linear',
+ duration: 2,
+ repeat: Infinity,
+ },
+ }}
+ >
+ <AiIcon className="w-4 h-4" />
+ </motion.div>
+ <motion.span
+ animate={{
+ opacity: ['0.5', '0.75', '0.5'],
+ transition: {
+ ease: 'linear',
+ duration: 2,
+ repeat: Infinity,
+ },
+ }}
+ >
+ Thinking...
+ </motion.span>
+ </motion.div>
+ )}
+ </AnimatePresence>
+ </motion.div>
+ </AISchemaSuggestionPopover>
<Split
style={{ height: '100%' }}
direction="vertical"
diff --git a/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx b/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
index 7cc55b11c52..563982c18bc 100644
--- a/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
+++ b/studio/components/interfaces/SQLEditor/SQLTemplates/SQLTemplates.tsx
@@ -2,7 +2,6 @@ import { PermissionAction } from '@supabase/shared-types/out/constants'
import { partition } from 'lodash'
import { observer } from 'mobx-react-lite'
-import * as Popover from '@radix-ui/react-popover'
import { useParams, useTelemetryProps } from 'common'
import {
SQL_TEMPLATES,
@@ -25,10 +24,11 @@ import { uuidv4 } from 'lib/helpers'
import { useProfile } from 'lib/profile'
import Telemetry from 'lib/telemetry'
import { useRouter } from 'next/router'
-import { useEffect, useState } from 'react'
+import { useState } from 'react'
import { format } from 'sql-formatter'
import { useSqlEditorStateSnapshot } from 'state/sql-editor'
-import { AiIcon, Button, IconCornerDownLeft, IconInfo, IconSettings, Input } from 'ui'
+import { AiIcon, IconCornerDownLeft, IconSettings, Input } from 'ui'
+import AISchemaSuggestionPopover from '../AISchemaSuggestionPopover'
import AISettingsModal from '../AISettingsModal'
import { createSqlSnippetSkeleton } from '../SQLEditor.utils'
import SQLCard from './SQLCard'
@@ -46,23 +46,11 @@ const SQLTemplates = observer(() => {
const isOptedInToAI =
selectedOrganization?.opt_in_tags?.includes('AI_SQL_GENERATOR_OPT_IN') ?? false
const [isOptedInToAISchema] = useLocalStorageQuery('supabase_sql-editor-ai-schema', false)
- const [aiQueryCount, setAiQueryCount] = useLocalStorageQuery(
- 'supabase_sql-editor-ai-query-count',
- 0
- )
- const [isSchemaSuggestionDismissed, setIsSchemaSuggestionDismissed] = useLocalStorageQuery(
- 'supabase_sql-editor-schema-suggestion-dismissed',
+ const [, setAiQueryCount] = useLocalStorageQuery('supabase_sql-editor-ai-query-count', 0)
+ const [, setIsSchemaSuggestionDismissed] = useLocalStorageQuery(
+ 'supabase_sql-editor-ai-schema-suggestion-dismissed',
false
)
- const [isDelayComplete, setIsDelayComplete] = useState(false)
-
- useEffect(() => {
- const timeout = window.setTimeout(() => {
- setIsDelayComplete(true)
- }, 500)
-
- return () => window.clearTimeout(timeout)
- })
const includeSchemaMetadata = (isOptedInToAI || !IS_PLATFORM) && isOptedInToAISchema
@@ -131,160 +119,107 @@ const SQLTemplates = observer(() => {
</motion.h1>
<div className="w-full flex justify-center">
{!isSqlGenerateLoading ? (
- <Popover.Root
- open={
- isDelayComplete &&
- !includeSchemaMetadata &&
- aiQueryCount >= 3 &&
- !isSchemaSuggestionDismissed
- }
+ <AISchemaSuggestionPopover
+ onClickSettings={() => {
+ setIsAISettingsOpen(true)
+ }}
>
- <Popover.Anchor asChild>
- <motion.div
- key="ask-ai-input-container"
- layoutId="ask-ai-input-container"
- className="w-full max-w-2xl border border-brand-900"
- variants={{
- visible: {
- y: 0,
- opacity: 1,
- borderRadius: 6,
- },
- hidden: {
- y: -50,
- opacity: 0,
- borderRadius: 6,
- },
+ <motion.div
+ key="ask-ai-input-container"
+ layoutId="ask-ai-input-container"
+ className="w-full max-w-2xl border border-brand-900"
+ variants={{
+ visible: {
+ y: 0,
+ opacity: 1,
+ borderRadius: 6,
+ },
+ hidden: {
+ y: -50,
+ opacity: 0,
+ borderRadius: 6,
+ },
+ }}
+ initial="hidden"
+ animate="visible"
+ >
+ <Input
+ size="xlarge"
+ inputRef={(inputElement: HTMLInputElement) => {
+ setTimeout(() => {
+ inputElement?.focus()
+ }, 200)
}}
- initial="hidden"
- animate="visible"
- >
- <Input
- size="xlarge"
- inputRef={(inputElement: HTMLInputElement) => {
- setTimeout(() => {
- inputElement?.focus()
- }, 200)
- }}
- icon={
- <motion.div
- key="ask-ai-input-icon"
- layoutId="ask-ai-input-icon"
- className="ml-1"
- variants={{
- visible: {
- scale: 1,
- },
- }}
- initial="visible"
- animate="visible"
- >
- <AiIcon className="w-4 h-4" />
- </motion.div>
- }
- inputClassName="w-full border-none py-4 focus:!ring-0 pr-20"
- iconContainerClassName="transition text-scale-800 text-brand-900"
- placeholder="Ask Supabase AI to build a query"
- className="w-full"
- onKeyPress={async (e) => {
- if (e.key === 'Enter') {
- try {
- const value = e.currentTarget.value
+ icon={
+ <motion.div
+ key="ask-ai-input-icon"
+ layoutId="ask-ai-input-icon"
+ className="ml-1"
+ variants={{
+ visible: {
+ scale: 1,
+ },
+ }}
+ initial="visible"
+ animate="visible"
+ >
+ <AiIcon className="w-4 h-4" />
+ </motion.div>
+ }
+ inputClassName="w-full border-none py-4 focus:!ring-0 pr-20"
+ iconContainerClassName="transition text-scale-800 text-brand-900"
+ placeholder="Ask Supabase AI to build a query"
+ className="w-full"
+ onKeyPress={async (e) => {
+ if (e.key === 'Enter') {
+ try {
+ const value = e.currentTarget.value
- if (!value) {
- return
- }
+ if (!value) {
+ return
+ }
- const { title, sql } = await generateSql({
- prompt: e.currentTarget.value,
- entityDefinitions,
- })
+ const { title, sql } = await generateSql({
+ prompt: e.currentTarget.value,
+ entityDefinitions,
+ })
- setAiQueryCount((count) => count + 1)
+ setAiQueryCount((count) => count + 1)
- const formattedSql =
- sqlAiDisclaimerComment +
- '\n\n' +
- format(sql, {
- language: 'postgresql',
- keywordCase: 'lower',
- })
+ const formattedSql =
+ sqlAiDisclaimerComment +
+ '\n\n' +
+ format(sql, {
+ language: 'postgresql',
+ keywordCase: 'lower',
+ })
- await handleNewQuery(formattedSql, title)
- } catch (error: unknown) {
- if (isError(error)) {
- ui.setNotification({
- category: 'error',
- message: error.message,
- })
- }
+ await handleNewQuery(formattedSql, title)
+ } catch (error: unknown) {
+ if (isError(error)) {
+ ui.setNotification({
+ category: 'error',
+ message: error.message,
+ })
}
}
- }}
- actions={
- <div className="flex items-center space-x-1 mr-4 gap-2">
- <IconCornerDownLeft size={16} strokeWidth={1.5} />
- <IconSettings
- className="cursor-pointer"
- onClick={() => {
- setIsSchemaSuggestionDismissed(true)
- setIsAISettingsOpen(true)
- }}
- />
- </div>
}
- />
- </motion.div>
- </Popover.Anchor>
-
- <Popover.Portal>
- <Popover.Content side="bottom" sideOffset={5}>
- <motion.div
- variants={{
- visible: {
- opacity: 1,
- y: 0,
- },
- hidden: {
- opacity: 0,
- y: -10,
- },
- }}
- initial="hidden"
- animate="visible"
- >
- <Popover.Arrow className="fill-scale-300 dark:fill-scale-500" />
- <div className="flex flex-col gap-2 border border-scale-300 dark:border-scale-500 rounded-md p-4 bg-scale-300 shadow-xl">
- <div className="flex flex-row items-center gap-4 max-w-md">
- <IconInfo className="w-6 h-6" />
- <p className="text-sm">
- Generate more relevant snippets by including database metadata in your
- requests.
- </p>
- </div>
- <div className="flex flex-row gap-2 justify-end">
- <Button
- type="default"
- onClick={() => {
- setIsSchemaSuggestionDismissed(true)
- }}
- >
- Dismiss
- </Button>
- <Button
- onClick={() => {
- setIsSchemaSuggestionDismissed(true)
- setIsAISettingsOpen(true)
- }}
- >
- Open settings
- </Button>
- </div>
+ }}
+ actions={
+ <div className="flex items-center space-x-1 mr-4 gap-2">
+ <IconCornerDownLeft size={16} strokeWidth={1.5} />
+ <IconSettings
+ className="cursor-pointer"
+ onClick={() => {
+ setIsSchemaSuggestionDismissed(true)
+ setIsAISettingsOpen(true)
+ }}
+ />
</div>
- </motion.div>
- </Popover.Content>
- </Popover.Portal>
- </Popover.Root>
+ }
+ />
+ </motion.div>
+ </AISchemaSuggestionPopover>
) : (
<motion.div
key="ask-ai-loading"
diff --git a/studio/hooks/misc/useLocalStorage.ts b/studio/hooks/misc/useLocalStorage.ts
index 54b1b6d6acf..954f928931b 100644
--- a/studio/hooks/misc/useLocalStorage.ts
+++ b/studio/hooks/misc/useLocalStorage.ts
@@ -57,12 +57,16 @@ export function useLocalStorageQuery<T>(key: string, initialValue: T) {
const { data: storedValue = initialValue } = useQuery(queryKey, () => {
if (typeof window === 'undefined') {
- return undefined
+ throw new Error('Window not defined')
}
const item = window.localStorage.getItem(key)
- return item ? (JSON.parse(item) as T) : undefined
+ if (!item) {
+ throw new Error(`No item exists for key '${key}'`)
+ }
+
+ return JSON.parse(item) as T
})
const setValue: Dispatch<SetStateAction<T>> = (value) => {