SlackIntegration.jsx 6.8 KB

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