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 [isRegisterSlackCredentials, setIsRegisterSlackCredentials] = useState(false);
const [isSendTestMessage, setIsSendTestMessage] = useState(false);
const [slackWSNameInWithoutProxy, setSlackWSNameInWithoutProxy] = useState(null);
const fetchSlackIntegrationData = useCallback(async() => {
try {
const { data } = await appContainer.apiv3.get('/slack-integration-settings');
const {
slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars,
} = data.settings;
if (data.connectionStatuses != null) {
const { workspaceName } = data.connectionStatuses[slackBotToken];
setSlackWSNameInWithoutProxy(workspaceName);
}
setCurrentBotType(data.currentBotType);
setSlackSigningSecret(slackSigningSecret);
setSlackBotToken(slackBotToken);
setSlackSigningSecretEnv(slackSigningSecretEnvVars);
setSlackBotTokenEnv(slackBotTokenEnvVars);
}
catch (err) {
toastError(err);
}
}, [appContainer.apiv3]);
const resetWithOutSettings = async() => {
try {
await appContainer.apiv3.delete('/slack-integration-settings/bot-type');
await appContainer.apiv3.put('/slack-integration-settings/bot-type', { currentBotType: 'customBotWithoutProxy' });
fetchSlackIntegrationData();
toastSuccess(t('admin:slack_integration.bot_reset_successful'));
}
catch (error) {
toastError(error);
}
};
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-settings/bot-type', {
currentBotType: selectedBotType,
});
setCurrentBotType(res.data.slackBotTypeParam.slackBotType);
setSelectedBotType(null);
setIsRegisterSlackCredentials(false);
setSlackSigningSecret(null);
setSlackBotToken(null);
setIsSendTestMessage(false);
setSlackWSNameInWithoutProxy(null);
toastSuccess(t('admin:slack_integration.bot_reset_successful'));
}
catch (err) {
toastError(err);
}
};
let settingsComponent = null;
switch (currentBotType) {
case 'officialBot':
settingsComponent =