ReviewWithAI.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // @ts-nocheck
  2. import { AiIconAnimation } from 'ui'
  3. import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
  4. import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
  5. import { Branch } from '@/data/branches/branches-query'
  6. import { useProjectDetailQuery } from '@/data/projects/project-detail-query'
  7. import { useTablesQuery } from '@/data/tables/tables-query'
  8. import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
  9. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  10. import { tablesToSQL } from '@/lib/helpers'
  11. import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
  12. import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
  13. interface ReviewWithAIProps {
  14. currentBranch?: Branch
  15. mainBranch?: Branch
  16. parentProjectRef?: string
  17. diffContent?: string
  18. disabled?: boolean
  19. }
  20. export const ReviewWithAI = ({
  21. currentBranch,
  22. mainBranch,
  23. parentProjectRef,
  24. diffContent,
  25. disabled = false,
  26. }: ReviewWithAIProps) => {
  27. const aiSnap = useAiAssistantStateSnapshot()
  28. const { openSidebar } = useSidebarManagerSnapshot()
  29. const { data: selectedOrg } = useSelectedOrganizationQuery()
  30. const { mutate: sendEvent } = useSendEventMutation()
  31. // Get parent project for production schema
  32. const { data: parentProject } = useProjectDetailQuery({ ref: parentProjectRef })
  33. // Fetch production schema tables
  34. const { data: productionTables } = useTablesQuery(
  35. {
  36. projectRef: parentProjectRef,
  37. connectionString: (parentProject as any)?.connectionString,
  38. schema: 'public',
  39. includeColumns: true,
  40. },
  41. { enabled: !!parentProjectRef && !!parentProject }
  42. )
  43. const handleReviewWithAssistant = () => {
  44. if (!currentBranch || !mainBranch) return
  45. // Track review with assistant button pressed
  46. sendEvent({
  47. action: 'branch_review_with_assistant_clicked',
  48. groups: {
  49. project: parentProjectRef ?? 'Unknown',
  50. organization: selectedOrg?.slug ?? 'Unknown',
  51. },
  52. })
  53. // Prepare diff content for the assistant
  54. const sqlSnippets = []
  55. // Add production schema SQL if available
  56. if (productionTables && productionTables.length > 0) {
  57. const productionSQL = tablesToSQL(productionTables)
  58. if (productionSQL.trim()) {
  59. sqlSnippets.push({
  60. label: 'Production Schema',
  61. content: 'CURRENT PRODUCTION SCHEMA:\n' + productionSQL,
  62. })
  63. }
  64. }
  65. // Add database diff content if available
  66. if (diffContent && diffContent.trim()) {
  67. sqlSnippets.push({
  68. label: 'Database Changes',
  69. content: '-- DATABASE CHANGES TO BE MERGED IN:\n' + diffContent,
  70. })
  71. }
  72. openSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
  73. aiSnap.newChat({
  74. name: `Review merge: ${currentBranch.name} → ${mainBranch.name}`,
  75. sqlSnippets: sqlSnippets.length > 0 ? sqlSnippets : undefined,
  76. initialInput: `I want to run the attached database changes on my production database branch as part of a branch merge from "${currentBranch.name}" into "${mainBranch.name || 'main'}". I've included the current production database schema as extra context. Please analyze the proposed schema changes and provide concise feedback on their impact on the production schema including any migration concerns and potential conflicts.`,
  77. suggestions: {
  78. title: `I can help you review the database schema changes from "${currentBranch.name}" to "${mainBranch.name}", here are some specific areas I can focus on:`,
  79. prompts: [
  80. {
  81. label: 'Schema Impact',
  82. description:
  83. 'Analyze the database schema changes and their potential impact on production...',
  84. },
  85. {
  86. label: 'Migration Safety',
  87. description: 'Review the migration safety and rollback strategies...',
  88. },
  89. {
  90. label: 'Performance',
  91. description: 'Analyze potential performance implications of these changes...',
  92. },
  93. {
  94. label: 'Data Integrity',
  95. description: 'Review constraints, indexes, and data integrity implications...',
  96. },
  97. ],
  98. },
  99. })
  100. }
  101. return (
  102. <ButtonTooltip
  103. type="default"
  104. disabled={disabled || !currentBranch || !mainBranch}
  105. className="px-1"
  106. onClick={handleReviewWithAssistant}
  107. tooltip={{
  108. content: {
  109. side: 'bottom',
  110. text: 'Ask Briven Assistant to review the merge request',
  111. },
  112. }}
  113. >
  114. <AiIconAnimation size={16} />
  115. <span className="sr-only">Review with Assistant</span>
  116. </ButtonTooltip>
  117. )
  118. }