import { useParams } from 'common' import { Edit } from 'lucide-react' import { useRouter } from 'next/router' import { ComponentProps } from 'react' import { cn, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, TooltipContent, } from 'ui' import { ButtonTooltip } from '../ButtonTooltip' import { useIsInlineEditorEnabled } from '@/components/interfaces/Account/Preferences/useDashboardSettings' import useNewQuery from '@/components/interfaces/SQLEditor/hooks' import { DiffType } from '@/components/interfaces/SQLEditor/SQLEditor.types' import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider' import { useSendEventMutation } from '@/data/telemetry/send-event-mutation' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { editorPanelState } from '@/state/editor-panel-state' import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state' import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor-v2' 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 { closeSidebar, openSidebar } = useSidebarManagerSnapshot() const isInSQLEditor = router.pathname.includes('/sql') const isInNewSnippet = router.pathname.endsWith('/sql') const isInlineEditorEnabled = useIsInlineEditorEnabled() const tooltip: { content: ComponentProps & { text: string } } = { content: { side: 'bottom', text: 'Edit in SQL Editor' }, } const { data: org } = useSelectedOrganizationQuery() const { mutate: sendEvent } = useSendEventMutation() if (id !== undefined) { return ( } tooltip={tooltip} onClick={() => { editorPanelState.setActiveSnippetId(id) openSidebar(SIDEBAR_KEYS.EDITOR_PANEL) }} /> ) } return !isInSQLEditor || isInNewSnippet ? ( } onClick={() => { if (isInlineEditorEnabled) { // This component needs to be updated to work with local EditorPanel state // For now, fall back to creating a new query if (sql) newQuery(sql, title) closeSidebar(SIDEBAR_KEYS.AI_ASSISTANT) } else { if (sql) newQuery(sql, title) } sendEvent({ action: 'assistant_edit_in_sql_editor_clicked', properties: { isInSQLEditor, isInNewSnippet, }, groups: { project: ref ?? 'Unknown', organization: org?.slug ?? 'Unknown' }, }) }} tooltip={tooltip} /> ) : ( } tooltip={!!sql ? tooltip : { content: { side: 'bottom', text: undefined } }} /> {!!sql && ( sqlEditorSnap.setDiffContent(sql, DiffType.Addition)}> Insert code sqlEditorSnap.setDiffContent(sql, DiffType.Modification)} > Replace code newQuery(sql, title)}> Create new snippet )} ) }