SlackIntegration.jsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 [slackWSNameInWithoutProxy, setSlackWSNameInWithoutProxy] = useState(null);
  26. const [isSlackScopeSet, setIsSlackScopeSet] = useState(false);
  27. const fetchSlackWorkSpaceNameInWithoutProxy = 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. setIsSlackScopeSet(true);
  35. }
  36. catch (err) {
  37. if (err[0].message === 'missing_scope') {
  38. setSlackWSNameInWithoutProxy(null);
  39. setIsSlackScopeSet(false);
  40. toastError(err, t('admin:slack_integration.set_scope'));
  41. }
  42. else {
  43. toastError(err);
  44. }
  45. }
  46. }, [appContainer.apiv3, isConnectedToSlack, t]);
  47. const fetchSlackIntegrationData = useCallback(async() => {
  48. try {
  49. const response = await appContainer.apiv3.get('slack-integration/');
  50. const { currentBotType, customBotWithoutProxySettings } = response.data.slackBotSettingParams;
  51. const {
  52. slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars,
  53. isConnectedToSlack,
  54. } = customBotWithoutProxySettings;
  55. setCurrentBotType(currentBotType);
  56. setSlackSigningSecret(slackSigningSecret);
  57. setSlackBotToken(slackBotToken);
  58. setSlackSigningSecretEnv(slackSigningSecretEnvVars);
  59. setSlackBotTokenEnv(slackBotTokenEnvVars);
  60. setIsConnectedToSlack(isConnectedToSlack);
  61. fetchSlackWorkSpaceNameInWithoutProxy();
  62. if (isConnectedToSlack) {
  63. setIsRegisterSlackCredentials(true);
  64. }
  65. else {
  66. setIsRegisterSlackCredentials(false);
  67. setIsSendTestMessage(false);
  68. }
  69. }
  70. catch (err) {
  71. toastError(err);
  72. }
  73. }, [appContainer.apiv3, fetchSlackWorkSpaceNameInWithoutProxy]);
  74. useEffect(() => {
  75. fetchSlackIntegrationData();
  76. }, [fetchSlackIntegrationData]);
  77. const handleBotTypeSelect = (clickedBotType) => {
  78. if (clickedBotType === currentBotType) {
  79. return;
  80. }
  81. if (currentBotType === null) {
  82. setCurrentBotType(clickedBotType);
  83. return;
  84. }
  85. setSelectedBotType(clickedBotType);
  86. };
  87. const cancelBotChangeHandler = () => {
  88. setSelectedBotType(null);
  89. };
  90. const changeCurrentBotSettingsHandler = async() => {
  91. try {
  92. const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
  93. slackSigningSecret: '',
  94. slackBotToken: '',
  95. currentBotType: selectedBotType,
  96. });
  97. setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
  98. setSelectedBotType(null);
  99. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  100. setIsRegisterSlackCredentials(false);
  101. setIsConnectedToSlack(false);
  102. setSlackSigningSecret(null);
  103. setSlackBotToken(null);
  104. setIsConnectedToSlack(false);
  105. setIsSendTestMessage(false);
  106. setSlackWSNameInWithoutProxy(null);
  107. setIsSlackScopeSet(false);
  108. }
  109. catch (err) {
  110. toastError(err);
  111. }
  112. };
  113. let settingsComponent = null;
  114. switch (currentBotType) {
  115. case 'officialBot':
  116. settingsComponent = <OfficialBotSettings />;
  117. break;
  118. case 'customBotWithoutProxy':
  119. settingsComponent = (
  120. <CustomBotWithoutProxySettings
  121. isSendTestMessage={isSendTestMessage}
  122. isRegisterSlackCredentials={isRegisterSlackCredentials}
  123. isConnectedToSlack={isConnectedToSlack}
  124. isSlackScopeSet={isSlackScopeSet}
  125. slackBotTokenEnv={slackBotTokenEnv}
  126. slackBotToken={slackBotToken}
  127. slackSigningSecretEnv={slackSigningSecretEnv}
  128. slackSigningSecret={slackSigningSecret}
  129. slackWSNameInWithoutProxy={slackWSNameInWithoutProxy}
  130. onSetSlackSigningSecret={setSlackSigningSecret}
  131. onSetSlackBotToken={setSlackBotToken}
  132. onSetIsSendTestMessage={setIsSendTestMessage}
  133. fetchSlackIntegrationData={fetchSlackIntegrationData}
  134. />
  135. );
  136. break;
  137. case 'customBotWithProxy':
  138. settingsComponent = <CustomBotWithProxySettings />;
  139. break;
  140. }
  141. return (
  142. <>
  143. <ConfirmBotChangeModal
  144. isOpen={selectedBotType != null}
  145. onConfirmClick={changeCurrentBotSettingsHandler}
  146. onCancelClick={cancelBotChangeHandler}
  147. />
  148. <div className="selecting-bot-type mb-5">
  149. <h2 className="admin-setting-header mb-4">
  150. {t('admin:slack_integration.selecting_bot_types.slack_bot')}
  151. {/* TODO: add an appropriate link by GW-5614 */}
  152. <a className="ml-2 btn-link" href="#">
  153. {t('admin:slack_integration.selecting_bot_types.detailed_explanation')}
  154. <i className="fa fa-external-link ml-1" aria-hidden="true"></i>
  155. </a>
  156. </h2>
  157. {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
  158. <div className="row my-5 flex-wrap-reverse justify-content-center">
  159. {botTypes.map((botType) => {
  160. return (
  161. <div key={botType} className="m-3">
  162. <BotTypeCard
  163. botType={botType}
  164. isActive={currentBotType === botType}
  165. handleBotTypeSelect={handleBotTypeSelect}
  166. />
  167. </div>
  168. );
  169. })}
  170. </div>
  171. </div>
  172. {settingsComponent}
  173. </>
  174. );
  175. };
  176. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  177. SlackIntegration.propTypes = {
  178. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  179. };
  180. export default SlackIntegrationWrapper;