2023-02-22 14:10:57 +00:00
|
|
|
import React, { useEffect } from "react";
|
2023-02-23 12:42:07 +00:00
|
|
|
|
|
|
|
// services
|
|
|
|
import appinstallationsService from "services/app-installations.service";
|
2023-04-22 16:24:50 +00:00
|
|
|
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
|
2023-02-23 12:42:07 +00:00
|
|
|
// components
|
|
|
|
import { Spinner } from "components/ui";
|
2023-02-22 14:10:57 +00:00
|
|
|
|
2023-04-22 16:24:50 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-02-22 14:10:57 +00:00
|
|
|
interface IGithuPostInstallationProps {
|
|
|
|
installation_id: string;
|
|
|
|
setup_action: string;
|
|
|
|
state: string;
|
|
|
|
provider: string;
|
2023-04-22 16:24:50 +00:00
|
|
|
code: string;
|
2023-02-22 14:10:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-08 18:32:33 +00:00
|
|
|
// TODO:Change getServerSideProps to router.query
|
2023-02-22 14:10:57 +00:00
|
|
|
const AppPostInstallation = ({
|
|
|
|
installation_id,
|
|
|
|
setup_action,
|
|
|
|
state,
|
|
|
|
provider,
|
2023-04-22 16:24:50 +00:00
|
|
|
code,
|
2023-02-22 14:10:57 +00:00
|
|
|
}: IGithuPostInstallationProps) => {
|
2023-04-22 16:24:50 +00:00
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
2023-02-22 14:10:57 +00:00
|
|
|
useEffect(() => {
|
2023-04-22 16:24:50 +00:00
|
|
|
if (provider === "github" && state && installation_id) {
|
2023-02-22 14:10:57 +00:00
|
|
|
appinstallationsService
|
2023-04-22 16:24:50 +00:00
|
|
|
.addInstallationApp(state, provider, { installation_id })
|
|
|
|
.then(() => {
|
2023-02-22 14:10:57 +00:00
|
|
|
window.opener = null;
|
|
|
|
window.open("", "_self");
|
|
|
|
window.close();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
2023-04-22 16:24:50 +00:00
|
|
|
} else if (provider === "slack" && state && code) {
|
2023-07-10 07:17:00 +00:00
|
|
|
appinstallationsService
|
|
|
|
.getSlackAuthDetails(code)
|
|
|
|
.then((res) => {
|
|
|
|
const [workspaceSlug, projectId, integrationId] = state.split(",");
|
2023-04-22 16:24:50 +00:00
|
|
|
|
2023-07-10 07:17:00 +00:00
|
|
|
if (!projectId) {
|
|
|
|
const payload = {
|
|
|
|
metadata: {
|
|
|
|
...res,
|
|
|
|
},
|
|
|
|
};
|
2023-04-22 16:24:50 +00:00
|
|
|
|
2023-07-10 07:17:00 +00:00
|
|
|
appinstallationsService
|
|
|
|
.addInstallationApp(state, provider, payload)
|
|
|
|
.then((r) => {
|
|
|
|
window.opener = null;
|
|
|
|
window.open("", "_self");
|
|
|
|
window.close();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw err?.response;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const payload = {
|
|
|
|
access_token: res.access_token,
|
|
|
|
bot_user_id: res.bot_user_id,
|
|
|
|
webhook_url: res.incoming_webhook.url,
|
|
|
|
data: res,
|
|
|
|
team_id: res.team.id,
|
|
|
|
team_name: res.team.name,
|
|
|
|
scopes: res.scope,
|
|
|
|
};
|
|
|
|
appinstallationsService
|
|
|
|
.addSlackChannel(workspaceSlug, projectId, integrationId, payload)
|
|
|
|
.then((r) => {
|
|
|
|
window.opener = null;
|
|
|
|
window.open("", "_self");
|
|
|
|
window.close();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw err.response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
2023-02-22 14:10:57 +00:00
|
|
|
}
|
2023-04-22 16:24:50 +00:00
|
|
|
}, [state, installation_id, provider, code]);
|
2023-02-23 12:42:07 +00:00
|
|
|
|
|
|
|
return (
|
2023-07-10 07:17:00 +00:00
|
|
|
<div className="absolute top-0 left-0 z-50 flex h-full w-full flex-col items-center justify-center gap-y-3 bg-custom-background-80">
|
|
|
|
<h2 className="text-2xl text-custom-text-100">Installing. Please wait...</h2>
|
2023-02-23 12:42:07 +00:00
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
2023-02-22 14:10:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export async function getServerSideProps(context: any) {
|
|
|
|
return {
|
|
|
|
props: context.query,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AppPostInstallation;
|