GitHubStatus.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { useParams } from 'common'
  2. import { AlertCircle, ArrowUpRight, CheckCircle2 } from 'lucide-react'
  3. import Image from 'next/image'
  4. import Link from 'next/link'
  5. import { HoverCard, HoverCardContent, HoverCardTrigger } from 'ui'
  6. import { useBranchesQuery } from '@/data/branches/branches-query'
  7. import { useGitHubConnectionsQuery } from '@/data/integrations/github-connections-query'
  8. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  9. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  10. import { BASE_PATH } from '@/lib/constants'
  11. export const GitHubStatus = () => {
  12. const { ref: projectRef } = useParams()
  13. const { data: selectedProject } = useSelectedProjectQuery()
  14. const { data: selectedOrganization } = useSelectedOrganizationQuery()
  15. const parentProjectRef = selectedProject?.parent_project_ref || projectRef
  16. const { data: connections } = useGitHubConnectionsQuery(
  17. { organizationId: selectedOrganization?.id },
  18. { enabled: !!selectedOrganization?.id }
  19. )
  20. const { data: branches } = useBranchesQuery(
  21. { projectRef: parentProjectRef },
  22. { enabled: !!parentProjectRef }
  23. )
  24. const githubConnection = connections?.find(
  25. (connection) => connection.project.ref === parentProjectRef
  26. )
  27. const mainBranch = branches?.find((branch) => branch.is_default)
  28. const isConnected = Boolean(githubConnection)
  29. const hasGitBranchSync = Boolean(mainBranch?.git_branch?.trim())
  30. const hasAutomaticBranching = Boolean(githubConnection?.new_branch_per_pr)
  31. return (
  32. <HoverCard openDelay={300} closeDelay={100}>
  33. <HoverCardTrigger asChild>
  34. <Link
  35. href={`/project/${parentProjectRef}/settings/integrations`}
  36. className="block w-full transition truncate text-sm text-foreground-light hover:text-foreground"
  37. >
  38. <div className="w-full flex items-center justify-between">
  39. <h3 className="text-sm">GitHub Integration</h3>
  40. <ArrowUpRight strokeWidth={1} className="h-4 w-4" />
  41. </div>
  42. <p className="mt-0.5 text-xs text-foreground-lighter flex items-center gap-2">
  43. {isConnected ? (
  44. <>
  45. <Image
  46. className="dark:invert"
  47. src={`${BASE_PATH}/img/icons/github-icon.svg`}
  48. width={16}
  49. height={16}
  50. alt="GitHub"
  51. />
  52. {githubConnection?.repository.name}
  53. </>
  54. ) : (
  55. 'Not connected'
  56. )}
  57. </p>
  58. </Link>
  59. </HoverCardTrigger>
  60. <HoverCardContent side="right" align="start" className="w-80 p-4 space-y-2">
  61. <div className="flex items-center gap-2 text-sm">
  62. <Image
  63. className="dark:invert"
  64. src={`${BASE_PATH}/img/icons/github-icon.svg`}
  65. width={20}
  66. height={20}
  67. alt="GitHub"
  68. />
  69. <span className="truncate">
  70. {isConnected ? githubConnection?.repository.name : 'Not connected'}
  71. </span>
  72. </div>
  73. {isConnected ? (
  74. <div className="flex flex-col gap-2 text-xs text-foreground-light">
  75. <div className="flex items-center gap-2">
  76. {hasGitBranchSync ? (
  77. <CheckCircle2 size={12} className="text-brand-600" />
  78. ) : (
  79. <AlertCircle size={12} className="text-warning" />
  80. )}
  81. <span>
  82. {hasGitBranchSync
  83. ? `Syncing production (${mainBranch?.git_branch})`
  84. : 'Production sync disabled'}
  85. </span>
  86. </div>
  87. <div className="flex items-center gap-2">
  88. {hasAutomaticBranching ? (
  89. <CheckCircle2 size={12} className="text-brand-600" />
  90. ) : (
  91. <AlertCircle size={12} className="text-warning" />
  92. )}
  93. <span>
  94. {hasAutomaticBranching
  95. ? 'Automatically creating branches'
  96. : 'Automatic branching disabled'}
  97. </span>
  98. </div>
  99. </div>
  100. ) : (
  101. <p className="text-xs text-foreground-light">Not connected to any repository</p>
  102. )}
  103. </HoverCardContent>
  104. </HoverCard>
  105. )
  106. }