import React, { useState, useEffect, useCallback } from 'react'; import PropTypes from 'prop-types'; import { useTranslation } from 'react-i18next'; import AppContainer from '../../../services/AppContainer'; import { withUnstatedContainers } from '../../UnstatedUtils'; import { toastSuccess, toastError } from '../../../util/apiNotification'; import OfficialBotSettings from './OfficialBotSettings'; import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings'; import CustomBotWithProxySettings from './CustomBotWithProxySettings'; import ConfirmBotChangeModal from './ConfirmBotChangeModal'; import BotTypeCard from './BotTypeCard'; const botTypes = ['officialBot', 'customBotWithoutProxy', 'customBotWithProxy']; const SlackIntegration = (props) => { const { appContainer } = props; const { t } = useTranslation(); const [currentBotType, setCurrentBotType] = useState(null); const [selectedBotType, setSelectedBotType] = useState(null); const [slackSigningSecret, setSlackSigningSecret] = useState(null); const [slackBotToken, setSlackBotToken] = useState(null); const [slackSigningSecretEnv, setSlackSigningSecretEnv] = useState(''); const [slackBotTokenEnv, setSlackBotTokenEnv] = useState(''); const [isConnectedToSlack, setIsConnectedToSlack] = useState(false); const [isRegisterSlackCredentials, setIsRegisterSlackCredentials] = useState(false); const [isSendTestMessage, setIsSendTestMessage] = useState(false); const [isSetupSlackBot, setIsSetupSlackBot] = useState(false); const [slackWSNameInWithoutProxy, setSlackWSNameInWithoutProxy] = useState(null); const fetchSlackWorkSpaceNameInWithoutProxy = useCallback(async() => { if (!isConnectedToSlack) { return setSlackWSNameInWithoutProxy(null); } try { const res = await appContainer.apiv3.get('/slack-integration/custom-bot-without-proxy/slack-workspace-name'); setSlackWSNameInWithoutProxy(res.data.slackWorkSpaceName); } catch (err) { toastError(err); } }, [appContainer.apiv3, isConnectedToSlack]); const fetchSlackIntegrationData = useCallback(async() => { try { const response = await appContainer.apiv3.get('slack-integration/'); const { currentBotType, customBotWithoutProxySettings } = response.data.slackBotSettingParams; const { slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars, isSetupSlackBot, isConnectedToSlack, } = customBotWithoutProxySettings; setCurrentBotType(currentBotType); setSlackSigningSecret(slackSigningSecret); setSlackBotToken(slackBotToken); setSlackSigningSecretEnv(slackSigningSecretEnvVars); setSlackBotTokenEnv(slackBotTokenEnvVars); setIsSetupSlackBot(isSetupSlackBot); setIsConnectedToSlack(isConnectedToSlack); fetchSlackWorkSpaceNameInWithoutProxy(); if (isConnectedToSlack) { setIsRegisterSlackCredentials(true); } else { setIsRegisterSlackCredentials(false); setIsSendTestMessage(false); } } catch (err) { toastError(err); } }, [appContainer.apiv3, fetchSlackWorkSpaceNameInWithoutProxy]); useEffect(() => { fetchSlackIntegrationData(); }, [fetchSlackIntegrationData]); const handleBotTypeSelect = (clickedBotType) => { if (clickedBotType === currentBotType) { return; } if (currentBotType === null) { setCurrentBotType(clickedBotType); return; } setSelectedBotType(clickedBotType); }; const cancelBotChangeHandler = () => { setSelectedBotType(null); }; const changeCurrentBotSettingsHandler = async() => { try { const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', { slackSigningSecret: '', slackBotToken: '', currentBotType: selectedBotType, }); setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType); setSelectedBotType(null); toastSuccess(t('admin:slack_integration.bot_reset_successful')); setIsRegisterSlackCredentials(false); setIsConnectedToSlack(false); setSlackSigningSecret(null); setSlackBotToken(null); setIsConnectedToSlack(false); setIsSendTestMessage(false); setSlackWSNameInWithoutProxy(null); } catch (err) { toastError(err); } }; let settingsComponent = null; switch (currentBotType) { case 'officialBot': settingsComponent = ; break; case 'customBotWithoutProxy': settingsComponent = ( ); break; case 'customBotWithProxy': settingsComponent = ; break; } return ( <>

{t('admin:slack_integration.selecting_bot_types.slack_bot')} {/* TODO: add an appropriate link by GW-5614 */} {t('admin:slack_integration.selecting_bot_types.detailed_explanation')}

{t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
{botTypes.map((botType) => { return (
); })}
{settingsComponent} ); }; const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]); SlackIntegration.propTypes = { appContainer: PropTypes.instanceOf(AppContainer).isRequired, }; export default SlackIntegrationWrapper;