SlackIntegration.jsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import React, { useState, useEffect, useCallback } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { useTranslation } from 'react-i18next';
  4. import AppContainer from '../../../services/AppContainer';
  5. import { withUnstatedContainers } from '../../UnstatedUtils';
  6. import { toastSuccess, toastError } from '../../../util/apiNotification';
  7. import OfficialBotSettings from './OfficialBotSettings';
  8. import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings';
  9. import CustomBotWithProxySettings from './CustomBotWithProxySettings';
  10. import ConfirmBotChangeModal from './ConfirmBotChangeModal';
  11. import BotTypeCard from './BotTypeCard';
  12. const botTypes = ['officialBot', 'customBotWithoutProxy', 'customBotWithProxy'];
  13. const SlackIntegration = (props) => {
  14. const { appContainer } = props;
  15. const { t } = useTranslation();
  16. const [currentBotType, setCurrentBotType] = useState(null);
  17. const [selectedBotType, setSelectedBotType] = useState(null);
  18. const [slackSigningSecret, setSlackSigningSecret] = useState(null);
  19. const [slackBotToken, setSlackBotToken] = useState(null);
  20. const [slackSigningSecretEnv, setSlackSigningSecretEnv] = useState('');
  21. const [slackBotTokenEnv, setSlackBotTokenEnv] = useState('');
  22. const [isConnectedToSlack, setIsConnectedToSlack] = useState(false);
  23. const [isRegisterSlackCredentials, setIsRegisterSlackCredentials] = useState(false);
  24. const [isSendTestMessage, setIsSendTestMessage] = useState(false);
  25. const [isSetupSlackBot, setIsSetupSlackBot] = useState(false);
  26. const [slackWSNameInWithoutProxy, setSlackWSNameInWithoutProxy] = useState(null);
  27. const fetchSlackWorkSpaceName = useCallback(async() => {
  28. if (!isConnectedToSlack) {
  29. return setSlackWSNameInWithoutProxy(null);
  30. }
  31. try {
  32. const res = await appContainer.apiv3.get('/slack-integration/custom-bot-without-proxy/slack-workspace-name');
  33. setSlackWSNameInWithoutProxy(res.data.slackWorkSpaceName);
  34. }
  35. catch (err) {
  36. toastError(err);
  37. }
  38. }, [appContainer.apiv3, isConnectedToSlack]);
  39. const fetchData = useCallback(async() => {
  40. try {
  41. const response = await appContainer.apiv3.get('slack-integration/');
  42. const { currentBotType, customBotWithoutProxySettings } = response.data.slackBotSettingParams;
  43. const {
  44. slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars,
  45. isSetupSlackBot, isConnectedToSlack,
  46. } = customBotWithoutProxySettings;
  47. setCurrentBotType(currentBotType);
  48. setSlackSigningSecret(slackSigningSecret);
  49. setSlackBotToken(slackBotToken);
  50. setSlackSigningSecretEnv(slackSigningSecretEnvVars);
  51. setSlackBotTokenEnv(slackBotTokenEnvVars);
  52. setIsSetupSlackBot(isSetupSlackBot);
  53. setIsConnectedToSlack(isConnectedToSlack);
  54. fetchSlackWorkSpaceName();
  55. if (isConnectedToSlack) {
  56. setIsRegisterSlackCredentials(true);
  57. }
  58. else {
  59. setIsRegisterSlackCredentials(false);
  60. setIsSendTestMessage(false);
  61. setSlackWSNameInWithoutProxy(null);
  62. }
  63. }
  64. catch (err) {
  65. toastError(err);
  66. }
  67. }, [appContainer.apiv3, fetchSlackWorkSpaceName]);
  68. useEffect(() => {
  69. fetchData();
  70. }, [fetchData]);
  71. const handleBotTypeSelect = (clickedBotType) => {
  72. if (clickedBotType === currentBotType) {
  73. return;
  74. }
  75. if (currentBotType === null) {
  76. setCurrentBotType(clickedBotType);
  77. return;
  78. }
  79. setSelectedBotType(clickedBotType);
  80. };
  81. const cancelBotChangeHandler = () => {
  82. setSelectedBotType(null);
  83. };
  84. const changeCurrentBotSettingsHandler = async() => {
  85. try {
  86. const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
  87. slackSigningSecret: '',
  88. slackBotToken: '',
  89. currentBotType: selectedBotType,
  90. });
  91. setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
  92. setSelectedBotType(null);
  93. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  94. setIsRegisterSlackCredentials(false);
  95. setIsConnectedToSlack(false);
  96. setSlackSigningSecret(null);
  97. setSlackBotToken(null);
  98. setIsConnectedToSlack(false);
  99. setIsSendTestMessage(false);
  100. setSlackWSNameInWithoutProxy(null);
  101. }
  102. catch (err) {
  103. toastError(err);
  104. }
  105. };
  106. let settingsComponent = null;
  107. switch (currentBotType) {
  108. case 'officialBot':
  109. settingsComponent = <OfficialBotSettings />;
  110. break;
  111. case 'customBotWithoutProxy':
  112. settingsComponent = (
  113. <CustomBotWithoutProxySettings
  114. isSendTestMessage={isSendTestMessage}
  115. isRegisterSlackCredentials={isRegisterSlackCredentials}
  116. isConnectedToSlack={isConnectedToSlack}
  117. isSetupSlackBot={isSetupSlackBot}
  118. slackBotTokenEnv={slackBotTokenEnv}
  119. slackBotToken={slackBotToken}
  120. slackSigningSecretEnv={slackSigningSecretEnv}
  121. slackSigningSecret={slackSigningSecret}
  122. slackWSNameInWithoutProxy={slackWSNameInWithoutProxy}
  123. onSetSlackSigningSecret={setSlackSigningSecret}
  124. onSetSlackBotToken={setSlackBotToken}
  125. onSetIsSendTestMessage={setIsSendTestMessage}
  126. fetchSlackWorkSpaceName={fetchSlackWorkSpaceName}
  127. fetchSlackIntegrationData={fetchData}
  128. />
  129. );
  130. break;
  131. case 'customBotWithProxy':
  132. settingsComponent = <CustomBotWithProxySettings />;
  133. break;
  134. }
  135. return (
  136. <>
  137. <ConfirmBotChangeModal
  138. isOpen={selectedBotType != null}
  139. onConfirmClick={changeCurrentBotSettingsHandler}
  140. onCancelClick={cancelBotChangeHandler}
  141. />
  142. <div className="selecting-bot-type my-5">
  143. <h2 className="admin-setting-header mb-4">
  144. {t('admin:slack_integration.selecting_bot_types.slack_bot')}
  145. {/* TODO: add an appropriate link by GW-5614 */}
  146. <a className="ml-2 btn-link" href="#">
  147. {t('admin:slack_integration.selecting_bot_types.detailed_explanation')}
  148. <i className="fa fa-external-link ml-1" aria-hidden="true"></i>
  149. </a>
  150. </h2>
  151. {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
  152. <div className="row my-4">
  153. <div className="card-deck mx-auto">
  154. {botTypes.map((botType) => {
  155. return (
  156. <BotTypeCard
  157. key={botType}
  158. botType={botType}
  159. isActive={currentBotType === botType}
  160. handleBotTypeSelect={handleBotTypeSelect}
  161. />
  162. );
  163. })}
  164. </div>
  165. </div>
  166. </div>
  167. {settingsComponent}
  168. </>
  169. );
  170. };
  171. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  172. SlackIntegration.propTypes = {
  173. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  174. };
  175. export default SlackIntegrationWrapper;