2023-02-22 14:10:57 +00:00
|
|
|
import React, { useEffect } from "react";
|
2023-02-23 12:42:07 +00:00
|
|
|
|
2023-04-22 16:24:50 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-09-11 06:43:00 +00:00
|
|
|
// services
|
|
|
|
import appInstallationsService from "services/app-installations.service";
|
|
|
|
// ui
|
|
|
|
import { Spinner } from "components/ui";
|
2023-02-22 14:10:57 +00:00
|
|
|
|
2023-09-11 06:43:00 +00:00
|
|
|
const AppPostInstallation = () => {
|
|
|
|
const router = useRouter();
|
|
|
|
const { installation_id, setup_action, state, provider, code } = router.query;
|
2023-04-22 16:24:50 +00:00
|
|
|
|
2023-02-22 14:10:57 +00:00
|
|
|
useEffect(() => {
|
2023-04-22 16:24:50 +00:00
|
|
|
if (provider === "github" && state && installation_id) {
|
2023-09-11 06:43:00 +00:00
|
|
|
appInstallationsService
|
|
|
|
.addInstallationApp(state.toString(), provider, { installation_id })
|
2023-04-22 16:24:50 +00:00
|
|
|
.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-09-11 06:43:00 +00:00
|
|
|
appInstallationsService
|
|
|
|
.getSlackAuthDetails(code.toString())
|
2023-07-10 07:17:00 +00:00
|
|
|
.then((res) => {
|
2023-09-11 06:43:00 +00:00
|
|
|
const [workspaceSlug, projectId, integrationId] = state.toString().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-09-11 06:43:00 +00:00
|
|
|
appInstallationsService
|
|
|
|
.addInstallationApp(state.toString(), provider, payload)
|
2023-07-10 07:17:00 +00:00
|
|
|
.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,
|
|
|
|
};
|
2023-09-11 06:43:00 +00:00
|
|
|
appInstallationsService
|
2023-07-10 07:17:00 +00:00
|
|
|
.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 default AppPostInstallation;
|