SlackIntegration.jsx 6.6 KB

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