github.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { LOCAL_STORAGE_KEYS } from 'common'
  2. import { makeRandomString } from './helpers'
  3. const GITHUB_INTEGRATION_APP_NAME =
  4. process.env.NEXT_PUBLIC_GITHUB_INTEGRATION_APP_NAME ||
  5. (process.env.NEXT_PUBLIC_IS_NIMBUS !== undefined
  6. ? 'briven-snap'
  7. : process.env.NEXT_PUBLIC_ENVIRONMENT === 'prod'
  8. ? `briven`
  9. : process.env.NEXT_PUBLIC_ENVIRONMENT === 'staging'
  10. ? `briven-staging`
  11. : `briven-local-testing`)
  12. const GITHUB_INTEGRATION_CLIENT_ID =
  13. process.env.NEXT_PUBLIC_GITHUB_INTEGRATION_CLIENT_ID ||
  14. (process.env.NEXT_PUBLIC_IS_NIMBUS !== undefined
  15. ? 'Iv23li2pAiqDGgaSrP8q'
  16. : process.env.NEXT_PUBLIC_ENVIRONMENT === 'prod'
  17. ? `Iv1.b91a6d8eaa272168`
  18. : process.env.NEXT_PUBLIC_ENVIRONMENT === 'staging'
  19. ? `Iv1.2681ab9a0360d8ad`
  20. : `Iv1.5022a3b44d150fbf`)
  21. const GITHUB_INTEGRATION_AUTHORIZATION_URL = `https://github.com/login/oauth/authorize?client_id=${GITHUB_INTEGRATION_CLIENT_ID}`
  22. export const GITHUB_INTEGRATION_INSTALLATION_URL = `https://github.com/apps/${GITHUB_INTEGRATION_APP_NAME}/installations/new`
  23. export const GITHUB_INTEGRATION_REVOKE_AUTHORIZATION_URL = `https://github.com/settings/connections/applications/${GITHUB_INTEGRATION_CLIENT_ID}`
  24. export function openInstallGitHubIntegrationWindow(
  25. type: 'install' | 'authorize',
  26. closeCallback?: () => void
  27. ) {
  28. const w = 600
  29. const h = 800
  30. const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screenX
  31. const dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screenY
  32. const width = window.innerWidth
  33. ? window.innerWidth
  34. : document.documentElement.clientWidth
  35. ? document.documentElement.clientWidth
  36. : screen.width
  37. const height = window.innerHeight
  38. ? window.innerHeight
  39. : document.documentElement.clientHeight
  40. ? document.documentElement.clientHeight
  41. : screen.height
  42. let windowUrl: string | undefined
  43. if (type === 'install') {
  44. windowUrl = GITHUB_INTEGRATION_INSTALLATION_URL
  45. } else {
  46. const state = makeRandomString(32)
  47. localStorage.setItem(LOCAL_STORAGE_KEYS.GITHUB_AUTHORIZATION_STATE, state)
  48. windowUrl = `${GITHUB_INTEGRATION_AUTHORIZATION_URL}&state=${state}&prompt=select_account`
  49. }
  50. const systemZoom = width / window.screen.availWidth
  51. const left = (width - w) / 2 / systemZoom + dualScreenLeft
  52. const top = (height - h) / 2 / systemZoom + dualScreenTop
  53. const newWindow = window.open(
  54. windowUrl,
  55. 'GitHub',
  56. `scrollbars=yes,resizable=no,status=no,location=no,toolbar=no,menubar=no,
  57. width=${w / systemZoom},
  58. height=${h / systemZoom},
  59. top=${top},
  60. left=${left}
  61. `
  62. )
  63. if (newWindow) {
  64. if (closeCallback) {
  65. // Poll to check if window is closed
  66. const checkClosed = setInterval(() => {
  67. if (newWindow.closed) {
  68. clearInterval(checkClosed)
  69. closeCallback()
  70. }
  71. }, 500) // Check every 500ms
  72. // Add a timeout to prevent infinite polling
  73. setTimeout(() => {
  74. clearInterval(checkClosed)
  75. }, 300000) // 5 minutes timeout
  76. }
  77. newWindow.focus()
  78. }
  79. }
  80. export const getGitHubProfileImgUrl = (username: string) => {
  81. return `https://github.com/${username}.png?size=96`
  82. }