import React, { useState, useEffect } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // services import appinstallationsService from "services/app-installations.service"; // ui import { Loader } from "components/ui"; // hooks import useToast from "hooks/use-toast"; import useIntegrationPopup from "hooks/use-integration-popup"; // types import { IWorkspaceIntegration } from "types"; // fetch-keys import { SLACK_CHANNEL_INFO } from "constants/fetch-keys"; type Props = { integration: IWorkspaceIntegration; }; export const SelectChannel: React.FC = ({ integration }) => { const [deletingProjectSync, setDeletingProjectSync] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { startAuth } = useIntegrationPopup("slackChannel", integration.id); const { data: projectIntegration } = useSWR( workspaceSlug && projectId && integration.id ? SLACK_CHANNEL_INFO(workspaceSlug as string, projectId as string) : null, () => workspaceSlug && projectId && integration.id ? appinstallationsService.getSlackChannelDetail( workspaceSlug as string, projectId as string, integration.id as string ) : null ); useEffect(() => { if (projectIntegration?.length > 0) { setDeletingProjectSync(true); } if (projectIntegration?.length === 0) { setDeletingProjectSync(false); } }, [projectIntegration]); const handleDelete = async () => { if (projectIntegration.length === 0) return; mutate(SLACK_CHANNEL_INFO, (prevData: any) => { if (!prevData) return; return prevData.id !== integration.id; }).then(() => setDeletingProjectSync(false)); appinstallationsService .removeSlackChannel( workspaceSlug as string, projectId as string, integration.id as string, projectIntegration?.[0]?.id ) .catch((err) => console.log(err)); }; const handleAuth = async () => { await startAuth(); setDeletingProjectSync(true); }; return ( <> {projectIntegration ? ( ) : ( )} ); };