MergeRequest.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // @ts-nocheck
  2. import { LOCAL_STORAGE_KEYS, useParams } from 'common'
  3. import dayjs from 'dayjs'
  4. import { GitBranchIcon, GitMerge, MoreVertical, Shield } from 'lucide-react'
  5. import Link from 'next/link'
  6. import { useRouter } from 'next/router'
  7. import { useMemo } from 'react'
  8. import { toast } from 'sonner'
  9. import {
  10. Button,
  11. DropdownMenu,
  12. DropdownMenuContent,
  13. DropdownMenuItem,
  14. DropdownMenuTrigger,
  15. } from 'ui'
  16. import { TimestampInfo } from 'ui-patterns'
  17. import { useIsPgDeltaDiffEnabled } from '../App/FeaturePreview/FeaturePreviewContext'
  18. import { ReviewWithAI } from '../BranchManagement/ReviewWithAI'
  19. import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
  20. import { FeaturePreviewBadge } from '@/components/ui/FeaturePreviewBadge'
  21. import { useBranchUpdateMutation } from '@/data/branches/branch-update-mutation'
  22. import { useBranchesQuery } from '@/data/branches/branches-query'
  23. import { useProjectGitHubConnectionQuery } from '@/data/integrations/github-connections-query'
  24. import { useProjectDetailQuery } from '@/data/projects/project-detail-query'
  25. import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
  26. import { useBranchMergeDiff } from '@/hooks/branches/useBranchMergeDiff'
  27. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  28. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  29. export const MergeTitle = () => {
  30. const { ref } = useParams()
  31. const { data: project } = useSelectedProjectQuery()
  32. const pgDeltaDiffEnabled = useIsPgDeltaDiffEnabled()
  33. const parentProjectRef = project?.parent_project_ref
  34. const { data: branches } = useBranchesQuery(
  35. { projectRef: parentProjectRef },
  36. {
  37. refetchOnMount: 'always',
  38. refetchOnWindowFocus: true,
  39. staleTime: 0,
  40. }
  41. )
  42. const currentBranch = branches?.find((branch) => branch.project_ref === ref)
  43. const mainBranch = branches?.find((branch) => branch.is_default)
  44. return (
  45. <div className="flex items-center gap-x-4">
  46. <div className="flex items-center gap-x-2">
  47. <span>Merge</span>
  48. <code className="flex items-center text-code-inline gap-x-1.5 px-2 py-1 border border-border">
  49. <GitBranchIcon strokeWidth={1.5} size={14} className="text-foreground-lighter" />
  50. {currentBranch?.name}
  51. </code>
  52. <span>into</span>
  53. <Link href={`/project/${mainBranch?.project_ref}`} className="font-mono inline-flex gap-4">
  54. <code className="flex items-center text-code-inline font-mono gap-x-1.5 px-2 py-1 border border-border">
  55. <Shield strokeWidth={1.5} size={14} className="text-warning" />
  56. {mainBranch?.name || 'main'}
  57. </code>
  58. </Link>
  59. </div>
  60. {pgDeltaDiffEnabled && (
  61. <FeaturePreviewBadge featureKey={LOCAL_STORAGE_KEYS.UI_PREVIEW_PG_DELTA_DIFF} />
  62. )}
  63. </div>
  64. )
  65. }
  66. export const MergeSubtitle = () => {
  67. const { ref } = useParams()
  68. const { data: project } = useSelectedProjectQuery()
  69. const parentProjectRef = project?.parent_project_ref
  70. const { data: branches } = useBranchesQuery(
  71. { projectRef: parentProjectRef },
  72. {
  73. refetchOnMount: 'always',
  74. refetchOnWindowFocus: true,
  75. staleTime: 0,
  76. }
  77. )
  78. const currentBranch = branches?.find((branch) => branch.project_ref === ref)
  79. const subtitle = useMemo(() => {
  80. if (!currentBranch?.created_at) return 'Branch information unavailable'
  81. if (!currentBranch?.review_requested_at) {
  82. return 'Not ready for review'
  83. }
  84. const reviewRequestedTime = dayjs(currentBranch.review_requested_at).fromNow()
  85. return (
  86. <>
  87. Request opened{' '}
  88. <TimestampInfo
  89. className="text-sm"
  90. utcTimestamp={currentBranch.review_requested_at}
  91. label={reviewRequestedTime}
  92. />
  93. </>
  94. )
  95. }, [currentBranch?.created_at, currentBranch?.review_requested_at])
  96. return <p className="text-foreground-lighter text-sm">{subtitle}</p>
  97. }
  98. export const MergeActions = ({
  99. isWorkflowRunning,
  100. isSubmitting,
  101. onSelectMerge,
  102. }: {
  103. isWorkflowRunning: boolean
  104. isSubmitting: boolean
  105. onSelectMerge: () => void
  106. }) => {
  107. const router = useRouter()
  108. const { ref } = useParams()
  109. const { data: project } = useSelectedProjectQuery()
  110. const { data: selectedOrg } = useSelectedOrganizationQuery()
  111. const { mutate: sendEvent } = useSendEventMutation()
  112. const { mutate: updateBranch, isPending: isUpdating } = useBranchUpdateMutation({
  113. onError: (error) => {
  114. toast.error(`Failed to update branch: ${error.message}`)
  115. },
  116. })
  117. const parentProjectRef = project?.parent_project_ref
  118. const { data: parentProject } = useProjectDetailQuery({ ref: parentProjectRef })
  119. const { data: ghConnection } = useProjectGitHubConnectionQuery({ ref: parentProjectRef })
  120. const { data: branches } = useBranchesQuery(
  121. { projectRef: parentProjectRef },
  122. {
  123. refetchOnMount: 'always',
  124. refetchOnWindowFocus: true,
  125. staleTime: 0,
  126. }
  127. )
  128. const currentBranch = branches?.find((branch) => branch.project_ref === ref)
  129. const mainBranch = branches?.find((branch) => branch.is_default)
  130. const {
  131. diffContent,
  132. isBranchOutOfDateOverall,
  133. isLoading: isCombinedDiffLoading,
  134. hasChanges: combinedHasChanges,
  135. } = useBranchMergeDiff({
  136. currentBranchRef: ref,
  137. parentProjectRef,
  138. currentBranchConnectionString: project?.connectionString || undefined,
  139. parentBranchConnectionString: parentProject?.connectionString || undefined,
  140. currentBranchCreatedAt: currentBranch?.created_at,
  141. })
  142. const isMergeDisabled =
  143. !combinedHasChanges ||
  144. isCombinedDiffLoading ||
  145. isBranchOutOfDateOverall ||
  146. isWorkflowRunning ||
  147. (!!ghConnection && Boolean(mainBranch?.git_branch))
  148. return (
  149. <div className="flex items-end gap-2">
  150. <ReviewWithAI
  151. currentBranch={currentBranch}
  152. mainBranch={mainBranch}
  153. parentProjectRef={parentProjectRef}
  154. diffContent={diffContent}
  155. disabled={!currentBranch || !mainBranch || isCombinedDiffLoading}
  156. />
  157. {isMergeDisabled ? (
  158. <ButtonTooltip
  159. tooltip={{
  160. content: {
  161. side: 'bottom',
  162. text: !combinedHasChanges
  163. ? 'No changes to merge'
  164. : isWorkflowRunning
  165. ? 'Workflow is currently running'
  166. : !!ghConnection && Boolean(mainBranch?.git_branch)
  167. ? 'Deploy to production from GitHub is enabled'
  168. : 'Unable to merge at this time',
  169. },
  170. }}
  171. type="primary"
  172. loading={isSubmitting}
  173. disabled={isMergeDisabled}
  174. onClick={onSelectMerge}
  175. icon={<GitMerge size={16} strokeWidth={1.5} className="text-brand" />}
  176. >
  177. Merge branch
  178. </ButtonTooltip>
  179. ) : (
  180. <Button
  181. type="primary"
  182. loading={isSubmitting}
  183. onClick={onSelectMerge}
  184. icon={<GitMerge size={16} strokeWidth={1.5} className="text-brand" />}
  185. >
  186. Merge branch
  187. </Button>
  188. )}
  189. <DropdownMenu>
  190. <DropdownMenuTrigger asChild>
  191. <Button type="default" loading={isUpdating} className="px-1.5" icon={<MoreVertical />} />
  192. </DropdownMenuTrigger>
  193. <DropdownMenuContent side="bottom" align="end" className="w-52">
  194. <DropdownMenuItem
  195. className="gap-x-2"
  196. onClick={() => {
  197. if (!ref || !parentProjectRef) return
  198. updateBranch(
  199. {
  200. branchRef: ref,
  201. projectRef: parentProjectRef,
  202. requestReview: false,
  203. },
  204. {
  205. onSuccess: () => {
  206. toast.success('Successfully closed merge request')
  207. router.push(`/project/${project?.ref}/branches?tab=prs`)
  208. sendEvent({
  209. action: 'branch_close_merge_request_button_clicked',
  210. groups: {
  211. project: parentProjectRef ?? 'Unknown',
  212. organization: selectedOrg?.slug ?? 'Unknown',
  213. },
  214. })
  215. },
  216. }
  217. )
  218. }}
  219. >
  220. Close this merge request
  221. </DropdownMenuItem>
  222. </DropdownMenuContent>
  223. </DropdownMenu>
  224. </div>
  225. )
  226. }