| export const onInitialize = async ({ |
| accessToken, |
| onComplete, |
| }: { |
| accessToken: string; |
| onComplete?: () => void; |
| }) => { |
| const apiUrl = process.env.NEXT_PUBLIC_API_URL || ""; |
| const response = await fetch(`${apiUrl}/apps/slack/install-url`, { |
| headers: { |
| Authorization: `Bearer ${accessToken}`, |
| }, |
| }).then((res) => { |
| if (!res.ok) { |
| throw new Error(`Failed to get install URL: ${res.statusText}`); |
| } |
| return res.json(); |
| }); |
|
|
| const { url } = response; |
|
|
| const width = 600; |
| const height = 800; |
| const left = window.screenX + (window.outerWidth - width) / 2; |
| const top = window.screenY + (window.outerHeight - height) / 2.5; |
|
|
| const popup = window.open( |
| url, |
| "", |
| `toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${width}, height=${height}, top=${top}, left=${left}`, |
| ); |
|
|
| |
| if (!popup) { |
| window.location.href = url; |
| return; |
| } |
|
|
| const listener = (e: MessageEvent) => { |
| |
| if (e.data === "app_oauth_completed") { |
| window.removeEventListener("message", listener); |
|
|
| |
| |
| |
|
|
| |
| if (onComplete) { |
| onComplete(); |
| } else { |
| |
| window.location.reload(); |
| } |
| } |
| }; |
|
|
| window.addEventListener("message", listener); |
|
|
| |
| const checkInterval = setInterval(() => { |
| if (popup?.closed) { |
| clearInterval(checkInterval); |
| window.removeEventListener("message", listener); |
| |
| if (onComplete) { |
| onComplete(); |
| } |
| } |
| }, 500); |
|
|
| |
| setTimeout( |
| () => { |
| clearInterval(checkInterval); |
| window.removeEventListener("message", listener); |
| }, |
| 5 * 60 * 1000, |
| ); |
| }; |
|
|