Files
supabase/apps/studio/components/ui/QueryBlock/EditQueryButton.tsx
Saxon Fletcher 28fe5811de Inline editor feature preview (#33541)
* add edge function knowledge

* deploy edge function mutaton

* add edge function block with deploy

* update function url

* update tools

* editor sql

* add templates

* use monaco editor

* hook up templates

* fixes

* add behind feature flag

* preview docs

* move editor

* initialPrompt

* consolidate widgets

* remove generic editor

* move logic

* fix ts

* include schema metadata

* Fix width of save snippet button

* Set tooltip position

* Use inlineLink component

* Remove unnecessary z index

* Lint

* Lint import statements in EditorPanel

* fixes

* fix keyboard shortcut

* show/hide results and invalidation

* fix focus issues in widget

* fixes

* fix ts

* Support cmd enter shortcut to run query in InlineEditor

* Update InlineEditorPreview to use admonition

* prompt updates

* add discussion url to preview

* schemas array

* One last clean up

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-02-19 14:58:58 +10:00

136 lines
3.9 KiB
TypeScript

import { Edit } from 'lucide-react'
import { useRouter } from 'next/router'
import { useParams } from 'common'
import { TelemetryActions } from 'common/telemetry-constants'
import { DiffType } from 'components/interfaces/SQLEditor/SQLEditor.types'
import useNewQuery from 'components/interfaces/SQLEditor/hooks'
import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
import Link from 'next/link'
import { ComponentProps } from 'react'
import { useSqlEditorV2StateSnapshot } from 'state/sql-editor-v2'
import {
cn,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
TooltipContent,
} from 'ui'
import { ButtonTooltip } from '../ButtonTooltip'
import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization'
import { useAppStateSnapshot } from 'state/app-state'
import { useIsInlineEditorEnabled } from 'components/interfaces/App/FeaturePreview/FeaturePreviewContext'
interface EditQueryButtonProps {
id?: string
title: string
sql?: string
className?: string
type?: 'default' | 'text'
}
export const EditQueryButton = ({
id,
sql,
title,
className,
type = 'text',
}: EditQueryButtonProps) => {
const router = useRouter()
const { ref } = useParams()
const { newQuery } = useNewQuery()
const sqlEditorSnap = useSqlEditorV2StateSnapshot()
const { setEditorPanel } = useAppStateSnapshot()
const isInSQLEditor = router.pathname.includes('/sql')
const isInNewSnippet = router.pathname.endsWith('/sql')
const isInlineEditorEnabled = useIsInlineEditorEnabled()
const tooltip: { content: ComponentProps<typeof TooltipContent> & { text: string } } = {
content: { side: 'bottom', text: 'Edit in SQL Editor' },
}
const org = useSelectedOrganization()
const { mutate: sendEvent } = useSendEventMutation()
const handleEditInSQLEditor = () => {
if (sql) {
if (isInSQLEditor) {
sqlEditorSnap.setDiffContent(sql, DiffType.Addition)
} else {
newQuery(sql, title)
}
}
}
if (id !== undefined) {
return (
<ButtonTooltip
asChild
type={type}
size="tiny"
className={cn('w-7 h-7', className)}
icon={<Edit size={14} />}
tooltip={tooltip}
>
<Link href={`/project/${ref}/sql/${id}`} />
</ButtonTooltip>
)
}
return !isInSQLEditor || isInNewSnippet ? (
<ButtonTooltip
type={type}
size="tiny"
className={cn('w-7 h-7', className)}
icon={<Edit size={14} />}
onClick={() => {
if (isInlineEditorEnabled) {
setEditorPanel({
open: true,
initialValue: sql,
})
} else {
handleEditInSQLEditor()
}
sendEvent({
action: TelemetryActions.ASSISTANT_EDIT_IN_SQL_EDITOR_CLICKED,
properties: {
isInSQLEditor,
isInNewSnippet,
},
groups: { project: ref ?? 'Unknown', organization: org?.slug ?? 'Unknown' },
})
}}
tooltip={tooltip}
/>
) : (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<ButtonTooltip
type={type}
size="tiny"
disabled={!sql}
className={cn('w-7 h-7', className)}
icon={<Edit size={14} />}
tooltip={!!sql ? tooltip : { content: { side: 'bottom', text: undefined } }}
/>
</DropdownMenuTrigger>
{!!sql && (
<DropdownMenuContent className="w-36">
<DropdownMenuItem onClick={() => sqlEditorSnap.setDiffContent(sql, DiffType.Addition)}>
Insert code
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => sqlEditorSnap.setDiffContent(sql, DiffType.Modification)}
>
Replace code
</DropdownMenuItem>
<DropdownMenuItem onClick={() => sqlEditorSnap.setDiffContent(sql, DiffType.NewSnippet)}>
Create new snippet
</DropdownMenuItem>
</DropdownMenuContent>
)}
</DropdownMenu>
)
}