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'; import DeleteSlackBotSettingsModal from './DeleteSlackBotSettingsModal'; 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 [isRegisterSlackCredentials, setIsRegisterSlackCredentials] = useState(false); const [slackWSNameInWithoutProxy, setSlackWSNameInWithoutProxy] = useState(null); const [isDeleteConfirmModalShown, setIsDeleteConfirmModalShown] = useState(false); const [slackAppIntegrations, setSlackAppIntegrations] = useState(); const [proxyServerUri, setProxyServerUri] = useState(); const [connectionStatuses, setConnectionStatuses] = useState(null); const fetchSlackIntegrationData = useCallback(async() => { try { const { data } = await appContainer.apiv3.get('/slack-integration-settings'); const { slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars, slackAppIntegrations, proxyServerUri, } = data.settings; if (data.connectionStatuses == null) { data.connectionStatuses = {}; } // if (data.connectionStatuses != null) { // TODO fix // const { workspaceName } = data.connectionStatuses[slackBotToken]; // setSlackWSNameInWithoutProxy(workspaceName); // setConnectionStatuses(data.connectionStatuses); // } setConnectionStatuses(data.connectionStatuses); setCurrentBotType(data.currentBotType); setSlackSigningSecret(slackSigningSecret); setSlackBotToken(slackBotToken); setSlackSigningSecretEnv(slackSigningSecretEnvVars); setSlackBotTokenEnv(slackBotTokenEnvVars); setSlackAppIntegrations(slackAppIntegrations); setProxyServerUri(proxyServerUri); } catch (err) { toastError(err); } }, [appContainer.apiv3]); const resetAllSettings = async() => { try { await appContainer.apiv3.delete('/slack-integration-settings/bot-type'); fetchSlackIntegrationData(); toastSuccess(t('admin:slack_integration.bot_all_reset_successful')); } catch (error) { toastError(error); } }; const resetWithOutSettings = async() => { try { await appContainer.apiv3.put('/slack-integration-settings/bot-type', { currentBotType: 'customBotWithoutProxy' }); fetchSlackIntegrationData(); toastSuccess(t('admin:slack_integration.bot_reset_successful')); } catch (error) { toastError(error); } }; const createSlackIntegrationData = async() => { try { await appContainer.apiv3.put('/slack-integration-settings/slack-app-integrations'); fetchSlackIntegrationData(); toastSuccess(t('admin:slack_integration.adding_slack_ws_integration_settings_successful')); } catch (error) { toastError(error); } }; useEffect(() => { fetchSlackIntegrationData(); }, [fetchSlackIntegrationData]); const changeCurrentBotSettings = async(botType) => { try { const res = await appContainer.apiv3.put('/slack-integration-settings/bot-type', { currentBotType: botType, }); setCurrentBotType(res.data.slackBotTypeParam.slackBotType); setSelectedBotType(null); setIsRegisterSlackCredentials(false); setSlackSigningSecret(null); setSlackBotToken(null); setSlackWSNameInWithoutProxy(null); } catch (err) { toastError(err); } }; const botTypeSelectHandler = async(botType) => { if (botType === currentBotType) { return; } if (currentBotType == null) { return changeCurrentBotSettings(botType); } setSelectedBotType(botType); }; const changeCurrentBotSettingsHandler = async() => { changeCurrentBotSettings(selectedBotType); toastSuccess(t('admin:slack_integration.bot_reset_successful')); }; const cancelBotChangeHandler = () => { setSelectedBotType(null); }; let settingsComponent = null; switch (currentBotType) { case 'officialBot': settingsComponent = ( ); break; case 'customBotWithoutProxy': settingsComponent = ( ); break; case 'customBotWithProxy': settingsComponent = ( ); break; } return ( <> setIsDeleteConfirmModalShown(false)} onClickDeleteButton={resetAllSettings} />

{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')}
{(currentBotType != null) && ( )}
{botTypes.map((botType) => { return (
); })}
{settingsComponent} ); }; const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]); SlackIntegration.propTypes = { appContainer: PropTypes.instanceOf(AppContainer).isRequired, }; export default SlackIntegrationWrapper;