SlackIntegration.jsx 7.3 KB

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