|
| 1 | +/** @jsxImportSource @emotion/react */ |
| 2 | +import { Box, xcss } from "@atlaskit/primitives"; |
| 3 | +import InfoIcon from "@atlaskit/icon/glyph/info"; |
| 4 | +import Button from "@atlaskit/button"; |
| 5 | +import { css } from "@emotion/react"; |
| 6 | +import { token } from "@atlaskit/tokens"; |
| 7 | +import Step from "../../../components/Step"; |
| 8 | +import { useEffect, useState } from "react"; |
| 9 | +import { AxiosError } from "axios"; |
| 10 | +import OAuthManager from "../../../services/oauth-manager"; |
| 11 | +import DeferralManager from "../../../services/deferral-manager"; |
| 12 | +import analyticsClient from "../../../analytics"; |
| 13 | +import { useNavigate } from "react-router-dom"; |
| 14 | +import { StateCallbacksForDefaultDeferredState } from "rest-interfaces"; |
| 15 | + |
| 16 | +const boxStyles = xcss({ |
| 17 | + borderBlockWidth: token("space.500"), |
| 18 | + borderStyle: "solid", |
| 19 | + borderColor: "color.border", |
| 20 | + borderRadius: "border.radius.050", |
| 21 | + borderWidth: "border.width", |
| 22 | + marginTop: token("space.200"), |
| 23 | + marginBottom: token("space.200"), |
| 24 | + display: "flex", |
| 25 | + alignItems: "center", |
| 26 | +}); |
| 27 | +const paragraphStyle = css` |
| 28 | + color: ${token("color.text.subtle")}; |
| 29 | + margin: ${token("space.0")}; |
| 30 | +`; |
| 31 | +const infoParaStyle = css` |
| 32 | + padding-left: 16px; |
| 33 | +`; |
| 34 | + |
| 35 | +const DefaultState = ({ |
| 36 | + orgName, |
| 37 | + jiraHost, |
| 38 | + requestId, |
| 39 | + callbacks, |
| 40 | +}: { |
| 41 | + orgName: string; |
| 42 | + jiraHost: string; |
| 43 | + requestId: string; |
| 44 | + callbacks: StateCallbacksForDefaultDeferredState; |
| 45 | +}) => { |
| 46 | + const navigate = useNavigate(); |
| 47 | + const { setIsLoading, setForbidden, onPopupBlocked } = callbacks; |
| 48 | + const [isLoggedIn, setIsLoggedIn] = useState(false); |
| 49 | + |
| 50 | + // Finish the OAuth dance if authenticated |
| 51 | + useEffect(() => { |
| 52 | + const handler = async (event: MessageEvent) => { |
| 53 | + if (event.data?.type === "oauth-callback" && event.data?.code) { |
| 54 | + const response: boolean | AxiosError = await OAuthManager.finishOAuthFlow(event.data?.code, event.data?.state); |
| 55 | + setIsLoading(false); |
| 56 | + if (response instanceof AxiosError) { |
| 57 | + return; |
| 58 | + } |
| 59 | + setIsLoggedIn(true); |
| 60 | + } |
| 61 | + }; |
| 62 | + window.addEventListener("message", handler); |
| 63 | + return () => { |
| 64 | + window.removeEventListener("message", handler); |
| 65 | + }; |
| 66 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 67 | + }, []); |
| 68 | + |
| 69 | + // Start the deferral connection if authenticated |
| 70 | + useEffect(() => { |
| 71 | + const connectDeferredOrgOrg = async () => { |
| 72 | + if (requestId) { |
| 73 | + setIsLoading(true); |
| 74 | + const status: boolean | AxiosError = await DeferralManager.connectOrgByDeferral(requestId); |
| 75 | + if (status instanceof AxiosError) { |
| 76 | + setForbidden(true); |
| 77 | + analyticsClient.sendScreenEvent({ name: "DeferredInstallationFailedScreen" }, { type: "cloud" }, requestId); |
| 78 | + } |
| 79 | + else if (status) { |
| 80 | + setForbidden(false); |
| 81 | + navigate("/spa/connected", { state: { orgLogin: orgName, requestId } }); |
| 82 | + } else { |
| 83 | + setForbidden(true); |
| 84 | + } |
| 85 | + setIsLoading(false); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + isLoggedIn && connectDeferredOrgOrg(); |
| 90 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 91 | + }, [ isLoggedIn ]); |
| 92 | + |
| 93 | + // Authenticate if no token/username is set |
| 94 | + const authenticate = async () => { |
| 95 | + setIsLoading(true); |
| 96 | + try { |
| 97 | + analyticsClient.sendUIEvent({ actionSubject: "signInAndConnectThroughDeferredInstallationStartScreen", action: "clicked"}, { type: "cloud" }, requestId); |
| 98 | + await OAuthManager.authenticateInGitHub({ |
| 99 | + onWinClosed: () => { |
| 100 | + setIsLoading(false); |
| 101 | + }, onPopupBlocked |
| 102 | + }); |
| 103 | + } catch (e) { |
| 104 | + // TODO: print alert error |
| 105 | + console.error("check error", e); |
| 106 | + } finally { |
| 107 | + setIsLoading(false); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + return ( |
| 112 | + <Step |
| 113 | + title={`Connect GitHub organization ${orgName} to Jira Software`} |
| 114 | + > |
| 115 | + <> |
| 116 | + <p css={paragraphStyle}> |
| 117 | + A Jira administrator has asked for approval to connect the |
| 118 | + GitHub organization {orgName} to the Jira site {jiraHost}. |
| 119 | + </p> |
| 120 | + <Box padding="space.200" xcss={boxStyles}> |
| 121 | + <InfoIcon label="differed-installation-info" size="small"/> |
| 122 | + <p css={[paragraphStyle, infoParaStyle]}> |
| 123 | + This will make all repositories in {orgName} available to all projects in {jiraHost}. Import work from those |
| 124 | + GitHub repositories into Jira. |
| 125 | + </p> |
| 126 | + </Box> |
| 127 | + <Button appearance="primary" onClick={authenticate}> |
| 128 | + Sign in & connect |
| 129 | + </Button> |
| 130 | + </> |
| 131 | + </Step> |
| 132 | + ); |
| 133 | +}; |
| 134 | + |
| 135 | +export default DefaultState; |
0 commit comments