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 AccessTokenSettings from './AccessTokenSettings'; import OfficialBotSettings from './OfficialBotSettings'; import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings'; import CustomBotWithProxySettings from './CustomBotWithProxySettings'; import ConfirmBotChangeModal from './ConfirmBotChangeModal'; const SlackIntegration = (props) => { const { appContainer } = props; const { t } = useTranslation(); const [currentBotType, setCurrentBotType] = useState(null); const [selectedBotType, setSelectedBotType] = useState(null); const [accessToken, setAccessToken] = useState(''); const fetchData = useCallback(async() => { try { const response = await appContainer.apiv3.get('slack-integration/'); const { currentBotType, accessToken } = response.data.slackBotSettingParams; setCurrentBotType(currentBotType); setAccessToken(accessToken); } catch (err) { toastError(err); } }, [appContainer.apiv3]); useEffect(() => { fetchData(); }, [fetchData]); 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')); } catch (err) { toastError(err); } }; const generateTokenHandler = async() => { try { await appContainer.apiv3.put('slack-integration/access-token'); fetchData(); } catch (err) { toastError(err); } }; const discardTokenHandler = async() => { try { await appContainer.apiv3.delete('slack-integration/access-token'); fetchData(); toastSuccess(t('admin:slack_integration.bot_reset_successful')); } catch (err) { toastError(err); } }; let settingsComponent = null; switch (currentBotType) { case 'official-bot': settingsComponent = ; break; case 'custom-bot-without-proxy': settingsComponent = ( ); break; case 'custom-bot-with-proxy': settingsComponent = ; break; } const showBotTypeLevel = (level) => { return {t(`admin:slack_integration.selecting_bot_types.${level}`)}; }; const showBotTypeLabel = (label) => { return {t(`admin:slack_integration.selecting_bot_types.${label}`)}; }; const showBotTypeDiscription = (desc) => { return {t(`admin:slack_integration.selecting_bot_types.${desc}`)}; }; return ( <>

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

{t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
handleBotTypeSelect('official-bot')} role="button" >

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

{showBotTypeLevel('for_beginners')}
{showBotTypeLabel('set_up')} {showBotTypeDiscription('easy')}
{showBotTypeLabel('multiple_workspaces_integration')} {showBotTypeDiscription('possible')}
{showBotTypeLabel('security_control')} {showBotTypeDiscription('impossible')}

handleBotTypeSelect('custom-bot-without-proxy')} role="button" >

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

{showBotTypeLevel('for_intermediate')}
{showBotTypeLabel('set_up')} {showBotTypeDiscription('normal')}
{showBotTypeLabel('multiple_workspaces_integration')} {showBotTypeDiscription('impossible')}
{showBotTypeLabel('security_control')} {showBotTypeDiscription('possible')}

handleBotTypeSelect('custom-bot-with-proxy')} role="button" >

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

{showBotTypeLevel('for_advanced')}
{showBotTypeLabel('set_up')} {showBotTypeDiscription('hard')}
{showBotTypeLabel('multiple_workspaces_integration')} {showBotTypeDiscription('possible')}
{showBotTypeLabel('security_control')} {showBotTypeDiscription('impossible')}

{settingsComponent} ); }; const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]); SlackIntegration.propTypes = { appContainer: PropTypes.instanceOf(AppContainer).isRequired, }; export default SlackIntegrationWrapper;