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