2023-04-18 05:24:45 +00:00
|
|
|
import { useRef, useState } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-04-21 16:51:25 +00:00
|
|
|
const useIntegrationPopup = (provider: string | undefined, stateParams?: string) => {
|
2023-04-18 05:24:45 +00:00
|
|
|
const [authLoader, setAuthLoader] = useState(false);
|
|
|
|
|
|
|
|
const router = useRouter();
|
2023-04-21 08:06:35 +00:00
|
|
|
const { workspaceSlug, projectId } = router.query;
|
2023-04-18 05:24:45 +00:00
|
|
|
|
|
|
|
const providerUrls: { [key: string]: string } = {
|
|
|
|
github: `https://github.com/apps/${
|
|
|
|
process.env.NEXT_PUBLIC_GITHUB_APP_NAME
|
2023-04-21 08:06:35 +00:00
|
|
|
}/installations/new?state=${workspaceSlug?.toString()}`,
|
|
|
|
slack: `https://slack.com/oauth/v2/authorize?scope=chat%3Awrite%2Cim%3Ahistory%2Cim%3Awrite%2Clinks%3Aread%2Clinks%3Awrite%2Cusers%3Aread%2Cusers%3Aread.email&user_scope=&&client_id=${
|
|
|
|
process.env.NEXT_PUBLIC_SLACK_CLIENT_ID
|
2023-04-21 16:51:25 +00:00
|
|
|
}&state=${workspaceSlug?.toString()}`,
|
2023-04-21 08:06:35 +00:00
|
|
|
slackChannel: `https://slack.com/oauth/v2/authorize?scope=incoming-webhook&client_id=${
|
|
|
|
process.env.NEXT_PUBLIC_SLACK_CLIENT_ID
|
2023-04-21 16:51:25 +00:00
|
|
|
}&state=${workspaceSlug?.toString()},${projectId?.toString()}${
|
|
|
|
stateParams ? "," + stateParams : ""
|
|
|
|
}`,
|
2023-04-18 05:24:45 +00:00
|
|
|
};
|
|
|
|
const popup = useRef<any>();
|
|
|
|
|
|
|
|
const checkPopup = () => {
|
|
|
|
const check = setInterval(() => {
|
|
|
|
if (!popup || popup.current.closed || popup.current.closed === undefined) {
|
|
|
|
clearInterval(check);
|
|
|
|
setAuthLoader(false);
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
const openPopup = () => {
|
|
|
|
if (!provider) return;
|
|
|
|
|
|
|
|
const width = 600,
|
|
|
|
height = 600;
|
|
|
|
const left = window.innerWidth / 2 - width / 2;
|
|
|
|
const top = window.innerHeight / 2 - height / 2;
|
|
|
|
const url = providerUrls[provider];
|
|
|
|
|
|
|
|
return window.open(url, "", `width=${width}, height=${height}, top=${top}, left=${left}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
const startAuth = () => {
|
|
|
|
popup.current = openPopup();
|
|
|
|
checkPopup();
|
|
|
|
setAuthLoader(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
startAuth,
|
|
|
|
isConnecting: authLoader,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useIntegrationPopup;
|