EditQueryButton.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // @ts-nocheck
  2. import { useParams } from 'common'
  3. import { Edit } from 'lucide-react'
  4. import { useRouter } from 'next/router'
  5. import { ComponentProps } from 'react'
  6. import {
  7. cn,
  8. DropdownMenu,
  9. DropdownMenuContent,
  10. DropdownMenuItem,
  11. DropdownMenuTrigger,
  12. TooltipContent,
  13. } from 'ui'
  14. import { ButtonTooltip } from '../ButtonTooltip'
  15. import { useIsInlineEditorEnabled } from '@/components/interfaces/Account/Preferences/useDashboardSettings'
  16. import useNewQuery from '@/components/interfaces/SQLEditor/hooks'
  17. import { DiffType } from '@/components/interfaces/SQLEditor/SQLEditor.types'
  18. import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
  19. import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
  20. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  21. import { editorPanelState } from '@/state/editor-panel-state'
  22. import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
  23. import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor-v2'
  24. interface EditQueryButtonProps {
  25. id?: string
  26. title: string
  27. sql?: string
  28. className?: string
  29. type?: 'default' | 'text'
  30. }
  31. export const EditQueryButton = ({
  32. id,
  33. sql,
  34. title,
  35. className,
  36. type = 'text',
  37. }: EditQueryButtonProps) => {
  38. const router = useRouter()
  39. const { ref } = useParams()
  40. const { newQuery } = useNewQuery()
  41. const sqlEditorSnap = useSqlEditorV2StateSnapshot()
  42. const { closeSidebar, openSidebar } = useSidebarManagerSnapshot()
  43. const isInSQLEditor = router.pathname.includes('/sql')
  44. const isInNewSnippet = router.pathname.endsWith('/sql')
  45. const isInlineEditorEnabled = useIsInlineEditorEnabled()
  46. const tooltip: { content: ComponentProps<typeof TooltipContent> & { text: string } } = {
  47. content: { side: 'bottom', text: 'Edit in SQL Editor' },
  48. }
  49. const { data: org } = useSelectedOrganizationQuery()
  50. const { mutate: sendEvent } = useSendEventMutation()
  51. if (id !== undefined) {
  52. return (
  53. <ButtonTooltip
  54. type={type}
  55. size="tiny"
  56. className={cn('w-7 h-7', className)}
  57. icon={<Edit size={14} strokeWidth={1.5} />}
  58. tooltip={tooltip}
  59. onClick={() => {
  60. editorPanelState.setActiveSnippetId(id)
  61. openSidebar(SIDEBAR_KEYS.EDITOR_PANEL)
  62. }}
  63. />
  64. )
  65. }
  66. return !isInSQLEditor || isInNewSnippet ? (
  67. <ButtonTooltip
  68. type={type}
  69. size="tiny"
  70. className={cn('w-7 h-7', className)}
  71. icon={<Edit size={14} strokeWidth={1.5} />}
  72. onClick={() => {
  73. if (isInlineEditorEnabled) {
  74. // This component needs to be updated to work with local EditorPanel state
  75. // For now, fall back to creating a new query
  76. if (sql) newQuery(sql, title)
  77. closeSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
  78. } else {
  79. if (sql) newQuery(sql, title)
  80. }
  81. sendEvent({
  82. action: 'assistant_edit_in_sql_editor_clicked',
  83. properties: {
  84. isInSQLEditor,
  85. isInNewSnippet,
  86. },
  87. groups: { project: ref ?? 'Unknown', organization: org?.slug ?? 'Unknown' },
  88. })
  89. }}
  90. tooltip={tooltip}
  91. />
  92. ) : (
  93. <DropdownMenu>
  94. <DropdownMenuTrigger asChild>
  95. <ButtonTooltip
  96. type={type}
  97. size="tiny"
  98. disabled={!sql}
  99. className={cn('w-7 h-7', className)}
  100. icon={<Edit size={14} strokeWidth={1.5} />}
  101. tooltip={!!sql ? tooltip : { content: { side: 'bottom', text: undefined } }}
  102. />
  103. </DropdownMenuTrigger>
  104. {!!sql && (
  105. <DropdownMenuContent className="w-36">
  106. <DropdownMenuItem onClick={() => sqlEditorSnap.setDiffContent(sql, DiffType.Addition)}>
  107. Insert code
  108. </DropdownMenuItem>
  109. <DropdownMenuItem
  110. onClick={() => sqlEditorSnap.setDiffContent(sql, DiffType.Modification)}
  111. >
  112. Replace code
  113. </DropdownMenuItem>
  114. <DropdownMenuItem onClick={() => newQuery(sql, title)}>
  115. Create new snippet
  116. </DropdownMenuItem>
  117. </DropdownMenuContent>
  118. )}
  119. </DropdownMenu>
  120. )
  121. }