import { useState, useEffect } from "react"; import { observer } from "mobx-react-lite"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // hooks import { Loader } from "@plane/ui"; import { SLACK_CHANNEL_INFO } from "constants/fetch-keys"; import { useApplication } from "hooks/store"; import useIntegrationPopup from "hooks/use-integration-popup"; // services import { AppInstallationService } from "services/app_installation.service"; // ui // types import { IWorkspaceIntegration, ISlackIntegration } from "@plane/types"; // fetch-keys type Props = { integration: IWorkspaceIntegration; }; const appInstallationService = new AppInstallationService(); export const SelectChannel: React.FC = observer(({ integration }) => { // store hooks const { config: { envConfig }, } = useApplication(); // states const [slackChannelAvailabilityToggle, setSlackChannelAvailabilityToggle] = useState(false); const [slackChannel, setSlackChannel] = useState(null); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { startAuth } = useIntegrationPopup({ provider: "slackChannel", stateParams: integration.id, github_app_name: envConfig?.github_client_id || "", slack_client_id: envConfig?.slack_client_id || "", }); const { data: projectIntegration } = useSWR( workspaceSlug && projectId && integration.id ? SLACK_CHANNEL_INFO(workspaceSlug as string, projectId as string) : null, () => workspaceSlug && projectId && integration.id ? appInstallationService.getSlackChannelDetail( workspaceSlug as string, projectId as string, integration.id as string ) : null ); useEffect(() => { if (projectId && projectIntegration && projectIntegration.length > 0) { const projectSlackIntegrationCheck: ISlackIntegration | undefined = projectIntegration.find( (_slack: ISlackIntegration) => _slack.project === projectId ); if (projectSlackIntegrationCheck) { setSlackChannel(() => projectSlackIntegrationCheck); setSlackChannelAvailabilityToggle(true); } } }, [projectIntegration, projectId]); const handleDelete = async () => { if (!workspaceSlug || !projectId) return; if (projectIntegration.length === 0) return; mutate(SLACK_CHANNEL_INFO(workspaceSlug?.toString(), projectId?.toString()), (prevData: any) => { if (!prevData) return; return prevData.id !== integration.id; }).then(() => { setSlackChannelAvailabilityToggle(false); setSlackChannel(null); }); appInstallationService .removeSlackChannel(workspaceSlug as string, projectId as string, integration.id as string, slackChannel?.id) .catch((err) => console.error(err)); }; const handleAuth = async () => { await startAuth(); }; return ( <> {projectIntegration ? ( ) : ( )} ); });