Skip to content

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

move templates in inline editor (#33759)

Saxon Fletchera149e38d54cf
diff --git a/apps/studio/components/ui/AIEditor/index.tsx b/apps/studio/components/ui/AIEditor/index.tsx
index 67917a2a45a..8e50a50bbef 100644
--- a/apps/studio/components/ui/AIEditor/index.tsx
+++ b/apps/studio/components/ui/AIEditor/index.tsx
@@ -217,6 +217,20 @@ const AIEditor = ({
setCurrentValue(value || defaultValue)
}, [value, defaultValue])
+ useEffect(() => {
+ if (initialPrompt) {
+ setPromptInput(initialPrompt)
+ setPromptState({
+ isOpen: Boolean(initialPrompt),
+ selection: '',
+ beforeSelection: '',
+ afterSelection: '',
+ startLineNumber: 0,
+ endLineNumber: 0,
+ })
+ }
+ }, [initialPrompt])
+
useEffect(() => {
if (!isDiffMode) {
setIsDiffEditorMounted(false)
diff --git a/apps/studio/components/ui/EditorPanel/EditorPanel.tsx b/apps/studio/components/ui/EditorPanel/EditorPanel.tsx
index e5a8daae887..1f133156f62 100644
--- a/apps/studio/components/ui/EditorPanel/EditorPanel.tsx
+++ b/apps/studio/components/ui/EditorPanel/EditorPanel.tsx
@@ -1,5 +1,5 @@
import { debounce } from 'lodash'
-import { Book, Save, X } from 'lucide-react'
+import { Book, Check, Save, X } from 'lucide-react'
import { useCallback, useEffect, useState } from 'react'
import { toast } from 'sonner'
@@ -19,7 +19,26 @@ import { uuidv4 } from 'lib/helpers'
import { useProfile } from 'lib/profile'
import { useAppStateSnapshot } from 'state/app-state'
import { useSqlEditorV2StateSnapshot } from 'state/sql-editor-v2'
-import { AiIconAnimation, Button, cn, Input_Shadcn_, SQL_ICON } from 'ui'
+import {
+ AiIconAnimation,
+ Button,
+ cn,
+ Input_Shadcn_,
+ SQL_ICON,
+ Popover_Shadcn_,
+ PopoverTrigger_Shadcn_,
+ PopoverContent_Shadcn_,
+ Command_Shadcn_,
+ CommandInput_Shadcn_,
+ CommandList_Shadcn_,
+ CommandEmpty_Shadcn_,
+ CommandGroup_Shadcn_,
+ CommandItem_Shadcn_,
+ HoverCard_Shadcn_,
+ HoverCardContent_Shadcn_,
+ HoverCardTrigger_Shadcn_,
+ CodeBlock,
+} from 'ui'
import { Admonition } from 'ui-patterns'
import { containsUnknownFunction, isReadOnlySelect } from '../AIAssistantPanel/AIAssistant.utils'
import AIEditor from '../AIEditor'
@@ -46,11 +65,10 @@ export const EditorPanel = ({ onChange }: EditorPanelProps) => {
const [results, setResults] = useState<undefined | any[]>(undefined)
const [showWarning, setShowWarning] = useState<'hasWriteOperation' | 'hasUnknownFunctions'>()
const [currentValue, setCurrentValue] = useState(editorPanel.initialValue || '')
- const [showTemplates, setShowTemplates] = useState(false)
- const [templateSearch, setTemplateSearch] = useState('')
const [savedCode, setSavedCode] = useState<string>('')
const [isPreviewingTemplate, setIsPreviewingTemplate] = useState(false)
const [showResults, setShowResults] = useState(true)
+ const [isTemplatesOpen, setIsTemplatesOpen] = useState(false)
const errorHeader = error?.formattedError?.split('\n')?.filter((x: string) => x.length > 0)?.[0]
const errorContent =
@@ -69,24 +87,6 @@ export const EditorPanel = ({ onChange }: EditorPanelProps) => {
},
})
- const handleChat = () => {
- setAiAssistantPanel({
- open: true,
- sqlSnippets: currentValue ? [currentValue] : [],
- initialInput: 'Help me understand and improve this SQL query...',
- suggestions: {
- title:
- 'I can help you understand and improve your SQL query. Here are a few example prompts to get you started:',
- prompts: [
- 'Explain what this query does...',
- 'Help me optimize this query...',
- 'Show me how to add more conditions...',
- 'Help me join this with another table...',
- ],
- },
- })
- }
-
const onExecuteSql = (skipValidation = false) => {
setError(undefined)
setShowWarning(undefined)
@@ -124,8 +124,7 @@ export const EditorPanel = ({ onChange }: EditorPanelProps) => {
const onSelectTemplate = (content: string) => {
handleChange(content)
- setSavedCode(content)
- setShowTemplates(false)
+ setIsTemplatesOpen(false)
}
// Create a debounced version of the revert code function
@@ -137,23 +136,6 @@ export const EditorPanel = ({ onChange }: EditorPanelProps) => {
[savedCode]
)
- const handleTemplateMouseEnter = (templateContent: string) => {
- // Cancel any pending revert
- debouncedRevertCode.cancel()
-
- if (!isPreviewingTemplate) {
- setSavedCode(currentValue)
- }
- setIsPreviewingTemplate(true)
- handleChange(templateContent)
- }
-
- const handleTemplateMouseLeave = () => {
- if (isPreviewingTemplate) {
- debouncedRevertCode()
- }
- }
-
// Cleanup debounce on unmount
useEffect(() => {
return () => {
@@ -180,15 +162,63 @@ export const EditorPanel = ({ onChange }: EditorPanelProps) => {
<div className="border-b flex shrink-0 items-center gap-x-3 px-5 h-[46px]">
<span className="text-sm flex-1">SQL Editor</span>
<div className="flex gap-2 items-center">
- <Button
- size="tiny"
- type="default"
- className="h-7"
- onClick={handleChat}
- icon={<AiIconAnimation size={16} />}
- >
- Chat
- </Button>
+ <Popover_Shadcn_ open={isTemplatesOpen} onOpenChange={setIsTemplatesOpen}>
+ <PopoverTrigger_Shadcn_ asChild>
+ <Button
+ size="tiny"
+ type="default"
+ role="combobox"
+ aria-expanded={isTemplatesOpen}
+ icon={<Book size={14} />}
+ >
+ Templates
+ </Button>
+ </PopoverTrigger_Shadcn_>
+ <PopoverContent_Shadcn_ align="end" className="w-[300px] p-0">
+ <Command_Shadcn_>
+ <CommandInput_Shadcn_ placeholder="Search templates..." />
+ <CommandList_Shadcn_>
+ <CommandEmpty_Shadcn_>No templates found.</CommandEmpty_Shadcn_>
+ <CommandGroup_Shadcn_>
+ {editorPanel.templates?.map((template) => (
+ <HoverCard_Shadcn_ key={template.name}>
+ <HoverCardTrigger_Shadcn_ asChild>
+ <CommandItem_Shadcn_
+ value={template.name}
+ onSelect={() => onSelectTemplate(template.content)}
+ className="cursor-pointer"
+ >
+ <div className="flex items-center gap-3">
+ <SQL_ICON
+ size={16}
+ className={cn(
+ 'w-5 h-5 flex-0 mr-2 transition-colors fill-foreground-muted'
+ )}
+ />
+ <div className="flex-1">
+ <h4 className="text-foreground flex-1">{template.name}</h4>
+ <p className="text-xs text-foreground-light">
+ {template.description}
+ </p>
+ </div>
+ </div>
+ </CommandItem_Shadcn_>
+ </HoverCardTrigger_Shadcn_>
+ <HoverCardContent_Shadcn_ side="left" className="w-[500px] p-0">
+ <CodeBlock
+ language="sql"
+ className="language-sql border-none"
+ hideLineNumbers
+ value={template.content}
+ />
+ </HoverCardContent_Shadcn_>
+ </HoverCard_Shadcn_>
+ ))}
+ </CommandGroup_Shadcn_>
+ </CommandList_Shadcn_>
+ </Command_Shadcn_>
+ </PopoverContent_Shadcn_>
+ </Popover_Shadcn_>
<ButtonTooltip
tooltip={{
content: {
@@ -334,58 +364,7 @@ export const EditorPanel = ({ onChange }: EditorPanelProps) => {
</div>
)}
- {showTemplates && editorPanel.templates && (
- <div className="bg-surface-100 border-t w-full flex flex-col max-h-80 h-full text-sm">
- <div className="px-4 py-3 border-b shrink-0">
- <Input_Shadcn_
- placeholder="Search templates..."
- value={templateSearch}
- onChange={(e) => setTemplateSearch(e.target.value)}
- />
- </div>
- <div className="overflow-auto flex-1 p-2">
- {editorPanel.templates
- ?.filter((template) => {
- const searchLower = templateSearch.toLowerCase()
- return (
- template.name.toLowerCase().includes(searchLower) ||
- template.description.toLowerCase().includes(searchLower)
- )
- })
- ?.map((template, i) => (
- <div
- key={i}
- className="cursor-pointer group rounded-lg flex items-center gap-4 px-4 py-3 hover:bg-surface-200"
- onClick={() => onSelectTemplate(template.content)}
- onMouseEnter={() => handleTemplateMouseEnter(template.content)}
- onMouseLeave={handleTemplateMouseLeave}
- >
- <SQL_ICON
- size={18}
- strokeWidth={1.5}
- className={cn(
- 'transition-colors fill-foreground-muted group-aria-selected:fill-foreground',
- 'w-5 h-5 shrink-0 grow-0 -ml-0.5'
- )}
- />
- <div>
- <p className="text-xs mb-1">{template.name}</p>
- <p className="text-xs text-foreground-light">{template.description}</p>
- </div>
- </div>
- ))}
- </div>
- </div>
- )}
- <div className="bg-surface-100 flex items-center gap-2 !justify-between px-5 py-4 w-full border-t shrink-0">
- <Button
- size="tiny"
- type="default"
- onClick={() => setShowTemplates(!showTemplates)}
- icon={<Book size={14} />}
- >
- {showTemplates ? 'Templates' : 'Templates'}
- </Button>
+ <div className="bg-surface-100 flex items-center gap-2 justify-end px-5 py-4 w-full border-t shrink-0">
<SqlRunButton isDisabled={isExecuting} isExecuting={isExecuting} onClick={onExecuteSql} />
</div>
</div>