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 { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, TooltipContent, } from 'ui' import { ButtonTooltip } from '../ButtonTooltip' import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization' interface EditQueryButtonProps { id?: string title: string sql?: string } export const EditQueryButton = ({ id, sql, title }: EditQueryButtonProps) => { const router = useRouter() const { ref } = useParams() const { newQuery } = useNewQuery() const sqlEditorSnap = useSqlEditorV2StateSnapshot() const isInSQLEditor = router.pathname.includes('/sql') const isInNewSnippet = router.pathname.endsWith('/sql') const tooltip: { content: ComponentProps & { 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 ( } tooltip={tooltip} > ) } return !isInSQLEditor || isInNewSnippet ? ( } onClick={() => { handleEditInSQLEditor() sendEvent({ action: TelemetryActions.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 sqlEditorSnap.setDiffContent(sql, DiffType.NewSnippet)}> Create new snippet )} ) }