1
- import { FC } from "react"
2
- import * as TypesGen from "api/typesGenerated"
1
+ import Button from "@material-ui/core/Button"
3
2
import { makeStyles } from "@material-ui/core/styles"
4
- import { useLocation } from "react-router-dom"
3
+ import { SvgIconProps } from "@material-ui/core/SvgIcon"
4
+ import Tooltip from "@material-ui/core/Tooltip"
5
5
import GitHub from "@material-ui/icons/GitHub"
6
- import { GitlabIcon } from "components/Icons/GitlabIcon "
6
+ import * as TypesGen from "api/typesGenerated "
7
7
import { AzureDevOpsIcon } from "components/Icons/AzureDevOpsIcon"
8
8
import { BitbucketIcon } from "components/Icons/BitbucketIcon"
9
+ import { GitlabIcon } from "components/Icons/GitlabIcon"
9
10
import { Typography } from "components/Typography/Typography"
10
- import Link from "@material-ui/core/Link"
11
- import Button from "@material-ui/core/Button"
12
- import { Tooltip } from "@material-ui/core"
11
+ import { FC } from "react"
13
12
14
13
export interface GitAuthProps {
15
14
type : TypesGen . GitProvider
@@ -25,43 +24,70 @@ export const GitAuth: FC<GitAuthProps> = ({
25
24
const styles = useStyles ( )
26
25
27
26
let prettyName : string
28
- let icon : JSX . Element
27
+ let Icon : ( props : SvgIconProps ) => JSX . Element
29
28
switch ( type ) {
30
29
case "azure-devops" :
31
30
prettyName = "Azure DevOps"
32
- icon = < AzureDevOpsIcon />
31
+ Icon = AzureDevOpsIcon
33
32
break
34
33
case "bitbucket" :
35
34
prettyName = "Bitbucket"
36
- icon = < BitbucketIcon />
35
+ Icon = BitbucketIcon
37
36
break
38
37
case "github" :
39
38
prettyName = "GitHub"
40
- icon = < GitHub />
39
+ Icon = GitHub
41
40
break
42
41
case "gitlab" :
43
42
prettyName = "GitLab"
44
- icon = < GitlabIcon />
43
+ Icon = GitlabIcon
45
44
break
45
+ default :
46
+ throw new Error ( "invalid git provider: " + type )
46
47
}
47
- const location = useLocation ( )
48
- const redirectURL = `${ authenticateURL } ?redirect=${ location . pathname } `
48
+ // The Git auth page uses a BroadcastChannel to notify listening
49
+ // pages for Git auth updates if the "notify" query parameter is specified.
50
+ const redirectURL = `${ authenticateURL } ?redirect=${ encodeURIComponent (
51
+ `/gitauth?notify` ,
52
+ ) } `
49
53
50
54
return (
51
- < Tooltip title = { authenticated ? "You're already authenticated!" : "" } >
52
- < a href = { redirectURL } className = { styles . link } >
53
- < Button disabled = { authenticated } >
54
- < div className = { styles . root } >
55
- { icon }
56
- { authenticated && < Typography variant = "body2" > Authenticated with { prettyName } </ Typography > }
57
- { ! authenticated && (
58
- < Typography variant = "body2" > Authenticate with { prettyName } </ Typography >
59
- ) }
60
- </ div >
61
- </ Button >
62
- </ a >
55
+ < Tooltip
56
+ title = {
57
+ authenticated ? "You're already authenticated! No action needed." : ``
58
+ }
59
+ >
60
+ < a
61
+ href = { redirectURL }
62
+ className = { styles . link }
63
+ onClick = { ( event ) => {
64
+ event . preventDefault ( )
65
+ // If the user is already authenticated, we don't want to redirect them
66
+ if ( authenticated || authenticateURL === "" ) {
67
+ return
68
+ }
69
+ window . open ( redirectURL , "_blank" , "width=900,height=600" )
70
+ } }
71
+ >
72
+ < Button className = { styles . button } disabled = { authenticated } fullWidth >
73
+ < div className = { styles . root } >
74
+ < Icon className = { styles . icon } />
75
+ < div className = { styles . text } >
76
+ < Typography variant = "body2" >
77
+ { authenticated
78
+ ? `You're authenticated with ${ prettyName } !`
79
+ : `Click to login with ${ prettyName } !` }
80
+ </ Typography >
81
+ { ! authenticated && (
82
+ < Typography variant = "caption" color = "textSecondary" >
83
+ { "You'll be redirected back here once authenticated." }
84
+ </ Typography >
85
+ ) }
86
+ </ div >
87
+ </ div >
88
+ </ Button >
89
+ </ a >
63
90
</ Tooltip >
64
-
65
91
)
66
92
}
67
93
@@ -70,8 +96,22 @@ const useStyles = makeStyles(() => ({
70
96
textDecoration : "none" ,
71
97
} ,
72
98
root : {
99
+ padding : 4 ,
73
100
display : "flex" ,
74
- gap : 16 ,
101
+ gap : 12 ,
75
102
alignItems : "center" ,
103
+ textAlign : "left" ,
104
+ } ,
105
+ button : {
106
+ height : "unset" ,
107
+ } ,
108
+ text : {
109
+ display : "flex" ,
110
+ flexDirection : "column" ,
111
+ gap : 4 ,
112
+ } ,
113
+ icon : {
114
+ width : 32 ,
115
+ height : 32 ,
76
116
} ,
77
117
} ) )
0 commit comments