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