IntegrationPanels.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // @ts-nocheck
  2. import dayjs from 'dayjs'
  3. import { ArrowRight, ExternalLink, Github } from 'lucide-react'
  4. import Image from 'next/legacy/image'
  5. import Link from 'next/link'
  6. import { forwardRef, HTMLAttributes, ReactNode, RefAttributes } from 'react'
  7. import { Badge, Button, cn } from 'ui'
  8. import { Markdown } from '@/components/interfaces/Markdown'
  9. import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
  10. import type {
  11. Integration,
  12. IntegrationProjectConnection,
  13. } from '@/data/integrations/integrations.types'
  14. import { useProjectDetailQuery } from '@/data/projects/project-detail-query'
  15. import { BASE_PATH } from '@/lib/constants'
  16. import { getIntegrationConfigurationUrl } from '@/lib/integration-utils'
  17. const ICON_STROKE_WIDTH = 2
  18. const ICON_SIZE = 14
  19. export interface IntegrationInstallationProps extends RefAttributes<HTMLLIElement> {
  20. title: string
  21. integration: Integration
  22. disabled?: boolean
  23. }
  24. type HandleIconType = Integration['integration']['name'] | 'Briven'
  25. const HandleIcon = ({ type, className }: { type: HandleIconType; className?: string }) => {
  26. switch (type) {
  27. case 'GitHub':
  28. return <Github strokeWidth={ICON_STROKE_WIDTH} size={ICON_SIZE} />
  29. break
  30. // case 'Netlify':
  31. // return <Square strokeWidth={ICON_STROKE_WIDTH} size={ICON_SIZE} />
  32. // break
  33. case 'Vercel':
  34. return (
  35. <svg
  36. xmlns="http://www.w3.org/2000/svg"
  37. fill="white"
  38. viewBox="0 0 512 512"
  39. className={cn('w-3.5', className)}
  40. >
  41. <path fillRule="evenodd" d="M256,48,496,464H16Z" />
  42. </svg>
  43. )
  44. break
  45. case 'Briven':
  46. return <img src={`${BASE_PATH}/img/briven-logo.svg`} alt="Briven" className="w-3.5"></img>
  47. break
  48. default:
  49. return <></>
  50. break
  51. }
  52. }
  53. const Avatar = ({ src }: { src: string | undefined }) => {
  54. return (
  55. <div className="relative border shadow-lg w-8 h-8 rounded-full overflow-hidden">
  56. <Image
  57. src={src || ''}
  58. width={30}
  59. height={30}
  60. layout="fill"
  61. alt="avatar"
  62. className="relative"
  63. />
  64. </div>
  65. )
  66. }
  67. export const IntegrationInstallation = forwardRef<HTMLLIElement, IntegrationInstallationProps>(
  68. ({ integration, disabled, ...props }, ref) => {
  69. const IntegrationIconBlock = () => {
  70. return (
  71. <div className="bg-black text-white w-8 h-8 rounded-sm flex items-center justify-center">
  72. <HandleIcon type={integration.integration.name} />
  73. </div>
  74. )
  75. }
  76. return (
  77. <li
  78. ref={ref}
  79. key={integration.id}
  80. className="bg-surface-100 border shadow-xs flex justify-between items-center px-8 py-4 rounded-lg"
  81. {...props}
  82. >
  83. <div className="flex gap-6 items-center">
  84. <div className="flex gap-3 items-center">
  85. <div className="flex -space-x-1">
  86. <IntegrationIconBlock />
  87. <Avatar src={integration?.metadata?.account.avatar} />
  88. </div>
  89. </div>
  90. <div className="flex flex-col gap-0">
  91. <div className="flex items-center gap-2">
  92. <span className="text-foreground text-sm font-medium">
  93. {integration.metadata?.account.name ||
  94. (integration.metadata !== undefined &&
  95. 'gitHubConnectionOwner' in integration.metadata &&
  96. integration.metadata?.gitHubConnectionOwner)}
  97. </span>
  98. <Badge>{integration.metadata?.account.type}</Badge>
  99. </div>
  100. <div className="flex flex-col gap-0">
  101. <span className="text-foreground-lighter text-xs">
  102. Created {dayjs(integration.inserted_at).fromNow()}
  103. </span>
  104. <span className="text-foreground-lighter text-xs">
  105. Added by {integration?.added_by?.primary_email}
  106. </span>
  107. </div>
  108. </div>
  109. </div>
  110. <Button asChild disabled={disabled} type="default" iconRight={<ExternalLink />}>
  111. {disabled ? (
  112. <p>Manage</p>
  113. ) : (
  114. <Link
  115. href={getIntegrationConfigurationUrl(integration)}
  116. target="_blank"
  117. rel="noopener noreferrer"
  118. >
  119. Manage
  120. </Link>
  121. )}
  122. </Button>
  123. </li>
  124. )
  125. }
  126. )
  127. export interface IntegrationConnectionProps extends HTMLAttributes<HTMLLIElement> {
  128. connection: IntegrationProjectConnection
  129. type: Integration['integration']['name']
  130. actions?: ReactNode
  131. showNode?: boolean
  132. orientation?: 'horizontal' | 'vertical'
  133. }
  134. export const IntegrationConnection = forwardRef<HTMLLIElement, IntegrationConnectionProps>(
  135. (
  136. { connection, type, actions, showNode = true, orientation = 'horizontal', className, ...props },
  137. ref
  138. ) => {
  139. const { data: project } = useProjectDetailQuery({ ref: connection.briven_project_ref })
  140. return (
  141. <li
  142. ref={ref}
  143. key={connection.id}
  144. {...props}
  145. className={cn(showNode && 'pl-8 ml-6 border-l border-muted', 'relative')}
  146. >
  147. {showNode && (
  148. <div className="absolute w-8 rounded-bl-full border-b border-l border-muted h-10 -left-px"></div>
  149. )}
  150. <div
  151. className={cn(
  152. orientation === 'horizontal'
  153. ? 'flex items-center justify-between gap-2'
  154. : 'flex flex-col gap-3',
  155. 'bg-surface-100 border shadow-xs px-6 py-4 rounded-lg',
  156. className
  157. )}
  158. >
  159. <div className="flex flex-col gap-1 min-w-0">
  160. <div className="flex items-center gap-2">
  161. <div className="shrink-0 flex gap-x-2 items-center max-w-40 ">
  162. <HandleIcon type={'Briven'} />
  163. <span title={project?.name} className="text-sm truncate">
  164. {project?.name}
  165. </span>
  166. </div>
  167. <ArrowRight
  168. size={14}
  169. className="shrink-0 text-foreground-lighter"
  170. strokeWidth={1.5}
  171. />
  172. <div className="flex-1 min-w-0 flex gap-2 items-center">
  173. {!connection?.metadata?.framework ? (
  174. <div className="bg-black text-white w-4 h-4 rounded-sm flex items-center justify-center">
  175. <HandleIcon type={type} className={'w-2.5!'} />
  176. </div>
  177. ) : (
  178. <img
  179. src={`${BASE_PATH}/img/icons/frameworks/${connection.metadata.framework}.svg`}
  180. width={21}
  181. height={21}
  182. alt={`icon`}
  183. />
  184. )}
  185. {type === 'GitHub' ? (
  186. <a
  187. title={connection.metadata.name}
  188. href={`https://github.com/${connection.metadata?.name}`}
  189. className="text-sm truncate"
  190. target="_blank"
  191. rel="noreferrer"
  192. >
  193. {connection.metadata?.name}
  194. </a>
  195. ) : (
  196. <span title={connection.metadata.name} className="text-sm truncate">
  197. {connection.metadata?.name}
  198. </span>
  199. )}
  200. </div>
  201. </div>
  202. <div className="flex flex-col gap-0">
  203. <span className="text-foreground-lighter text-xs">
  204. Connected {dayjs(connection?.inserted_at).fromNow()}
  205. </span>
  206. <span className="text-foreground-lighter text-xs">
  207. Added by {connection?.added_by?.primary_email}
  208. </span>
  209. </div>
  210. </div>
  211. <div className="shrink-0">{actions}</div>
  212. </div>
  213. </li>
  214. )
  215. }
  216. )
  217. export const IntegrationConnectionOption = forwardRef<HTMLLIElement, IntegrationConnectionProps>(
  218. ({ connection, type, ...props }, ref) => {
  219. const { data: project } = useProjectDetailQuery({ ref: connection.briven_project_ref })
  220. return (
  221. <li
  222. ref={ref}
  223. key={connection.id}
  224. {...props}
  225. className={cn(
  226. 'bg-surface-100 border shadow-xs flex justify-between items-center px-8 py-4 rounded-lg'
  227. )}
  228. >
  229. <div className="flex flex-col gap-1">
  230. <div className="flex gap-2 items-center">
  231. <HandleIcon type={'Briven'} />
  232. <span className="text-sm">{project?.name}</span>
  233. <ArrowRight size={14} className="text-foreground-lighter" strokeWidth={1.5} />
  234. <HandleIcon type={type} />
  235. <span className="text-sm">{connection.metadata.name}</span>
  236. </div>
  237. <span className="text-foreground-lighter text-xs">
  238. Connected {dayjs(connection.inserted_at).fromNow()}
  239. </span>
  240. </div>
  241. <Button type="default">Connect</Button>
  242. </li>
  243. )
  244. }
  245. )
  246. export const EmptyIntegrationConnection = forwardRef<
  247. HTMLDivElement,
  248. HTMLAttributes<HTMLDivElement> & {
  249. showNode?: boolean
  250. onClick: () => void
  251. disabled?: boolean
  252. }
  253. >(({ className, showNode = true, onClick, disabled, ...props }, ref) => {
  254. return (
  255. <div
  256. ref={ref}
  257. {...props}
  258. className={cn(
  259. showNode && 'ml-6 pl-8 mt-4 border-l',
  260. 'relative pb-2',
  261. 'last:border-l-transparent',
  262. className
  263. )}
  264. >
  265. {showNode && (
  266. <div className="absolute w-8 rounded-bl-full border-b border-l border-muted h-14 -top-4 -left-px"></div>
  267. )}
  268. <div
  269. className={cn(
  270. 'w-full',
  271. 'border border-dashed bg-surface-100 border-overlay',
  272. 'flex h-20 px-10 rounded-lg justify-center items-center'
  273. )}
  274. >
  275. <ButtonTooltip
  276. type="default"
  277. disabled={disabled}
  278. onClick={() => onClick()}
  279. tooltip={{
  280. content: {
  281. side: 'bottom',
  282. text: disabled ? 'Additional permissions required to add connection' : undefined,
  283. },
  284. }}
  285. >
  286. Add new project connection
  287. </ButtonTooltip>
  288. </div>
  289. </div>
  290. )
  291. })
  292. interface IntegrationConnectionHeader extends React.HTMLAttributes<HTMLDivElement> {
  293. name?: string
  294. markdown?: string
  295. showNode?: boolean
  296. }
  297. export const IntegrationConnectionHeader = forwardRef<HTMLDivElement, IntegrationConnectionHeader>(
  298. ({ className, markdown = '', showNode = true, ...props }, ref) => {
  299. return (
  300. <div
  301. {...props}
  302. ref={ref}
  303. className={cn(
  304. showNode && 'border-l border-muted ml-6 pl-8',
  305. 'py-4 prose text-sm',
  306. className
  307. )}
  308. >
  309. {props.title && <h5 className="text-foreground">{props.title}</h5>}
  310. <Markdown content={markdown} />
  311. </div>
  312. )
  313. }
  314. )
  315. IntegrationInstallation.displayName = 'IntegrationInstallation'
  316. IntegrationConnection.displayName = 'IntegrationConnection'
  317. IntegrationConnectionHeader.displayName = 'IntegrationConnectionHeader'
  318. EmptyIntegrationConnection.displayName = 'EmptyIntegrationConnection'
  319. IntegrationConnectionOption.displayName = 'IntegrationConnectionOption'