SlackIntegration.jsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. import DeleteSlackBotSettingsModal from './DeleteSlackBotSettingsModal';
  13. const botTypes = ['officialBot', 'customBotWithoutProxy', 'customBotWithProxy'];
  14. const SlackIntegration = (props) => {
  15. const { appContainer } = props;
  16. const { t } = useTranslation();
  17. const [currentBotType, setCurrentBotType] = useState(null);
  18. const [selectedBotType, setSelectedBotType] = useState(null);
  19. const [slackSigningSecret, setSlackSigningSecret] = useState(null);
  20. const [slackBotToken, setSlackBotToken] = useState(null);
  21. const [slackSigningSecretEnv, setSlackSigningSecretEnv] = useState('');
  22. const [slackBotTokenEnv, setSlackBotTokenEnv] = useState('');
  23. const [isRegisterSlackCredentials, setIsRegisterSlackCredentials] = useState(false);
  24. const [slackWSNameInWithoutProxy, setSlackWSNameInWithoutProxy] = useState(null);
  25. const [isDeleteConfirmModalShown, setIsDeleteConfirmModalShown] = useState(false);
  26. const [slackAppIntegrations, setSlackAppIntegrations] = useState();
  27. const [proxyServerUri, setProxyServerUri] = useState();
  28. const [connectionStatuses, setConnectionStatuses] = useState(null);
  29. const fetchSlackIntegrationData = useCallback(async() => {
  30. try {
  31. const { data } = await appContainer.apiv3.get('/slack-integration-settings');
  32. const {
  33. slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars, slackAppIntegrations, proxyServerUri,
  34. } = data.settings;
  35. if (data.connectionStatuses == null) {
  36. data.connectionStatuses = {};
  37. }
  38. setConnectionStatuses(data.connectionStatuses);
  39. setCurrentBotType(data.currentBotType);
  40. setSlackSigningSecret(slackSigningSecret);
  41. setSlackBotToken(slackBotToken);
  42. setSlackSigningSecretEnv(slackSigningSecretEnvVars);
  43. setSlackBotTokenEnv(slackBotTokenEnvVars);
  44. setSlackAppIntegrations(slackAppIntegrations);
  45. setProxyServerUri(proxyServerUri);
  46. }
  47. catch (err) {
  48. toastError(err);
  49. }
  50. }, [appContainer.apiv3]);
  51. const resetAllSettings = async() => {
  52. try {
  53. await appContainer.apiv3.delete('/slack-integration-settings/bot-type');
  54. fetchSlackIntegrationData();
  55. toastSuccess(t('admin:slack_integration.bot_all_reset_successful'));
  56. }
  57. catch (error) {
  58. toastError(error);
  59. }
  60. };
  61. const resetWithOutSettings = async() => {
  62. try {
  63. await appContainer.apiv3.put('/slack-integration-settings/bot-type', { currentBotType: 'customBotWithoutProxy' });
  64. fetchSlackIntegrationData();
  65. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  66. }
  67. catch (error) {
  68. toastError(error);
  69. }
  70. };
  71. const createSlackIntegrationData = async() => {
  72. try {
  73. await appContainer.apiv3.put('/slack-integration-settings/slack-app-integrations');
  74. fetchSlackIntegrationData();
  75. toastSuccess(t('admin:slack_integration.adding_slack_ws_integration_settings_successful'));
  76. }
  77. catch (error) {
  78. toastError(error);
  79. }
  80. };
  81. useEffect(() => {
  82. fetchSlackIntegrationData();
  83. }, [fetchSlackIntegrationData]);
  84. const changeCurrentBotSettings = async(botType) => {
  85. try {
  86. const res = await appContainer.apiv3.put('/slack-integration-settings/bot-type', {
  87. currentBotType: botType,
  88. });
  89. setCurrentBotType(res.data.slackBotTypeParam.slackBotType);
  90. setSelectedBotType(null);
  91. setIsRegisterSlackCredentials(false);
  92. setSlackSigningSecret(null);
  93. setSlackBotToken(null);
  94. setSlackWSNameInWithoutProxy(null);
  95. }
  96. catch (err) {
  97. toastError(err);
  98. }
  99. };
  100. const botTypeSelectHandler = async(botType) => {
  101. if (botType === currentBotType) {
  102. return;
  103. }
  104. if (currentBotType == null) {
  105. return changeCurrentBotSettings(botType);
  106. }
  107. setSelectedBotType(botType);
  108. };
  109. const changeCurrentBotSettingsHandler = async() => {
  110. changeCurrentBotSettings(selectedBotType);
  111. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  112. };
  113. const cancelBotChangeHandler = () => {
  114. setSelectedBotType(null);
  115. };
  116. let settingsComponent = null;
  117. switch (currentBotType) {
  118. case 'officialBot':
  119. settingsComponent = (
  120. <OfficialBotSettings
  121. slackAppIntegrations={slackAppIntegrations}
  122. proxyServerUri={proxyServerUri}
  123. onClickAddSlackWorkspaceBtn={createSlackIntegrationData}
  124. connectionStatuses={connectionStatuses}
  125. />
  126. );
  127. break;
  128. case 'customBotWithoutProxy':
  129. settingsComponent = (
  130. <CustomBotWithoutProxySettings
  131. isRegisterSlackCredentials={isRegisterSlackCredentials}
  132. slackBotTokenEnv={slackBotTokenEnv}
  133. slackBotToken={slackBotToken}
  134. slackSigningSecretEnv={slackSigningSecretEnv}
  135. slackSigningSecret={slackSigningSecret}
  136. slackWSNameInWithoutProxy={slackWSNameInWithoutProxy}
  137. onSigningSecretChanged={setSlackSigningSecret}
  138. onBotTokenChanged={setSlackBotToken}
  139. onResetSettings={resetWithOutSettings}
  140. fetchSlackIntegrationData={fetchSlackIntegrationData}
  141. connectionStatuses={connectionStatuses}
  142. />
  143. );
  144. break;
  145. case 'customBotWithProxy':
  146. settingsComponent = (
  147. <CustomBotWithProxySettings
  148. slackAppIntegrations={slackAppIntegrations}
  149. proxyServerUri={proxyServerUri}
  150. onClickAddSlackWorkspaceBtn={createSlackIntegrationData}
  151. fetchSlackIntegrationData={fetchSlackIntegrationData}
  152. connectionStatuses={connectionStatuses}
  153. />
  154. );
  155. break;
  156. }
  157. return (
  158. <>
  159. <ConfirmBotChangeModal
  160. isOpen={selectedBotType != null}
  161. onConfirmClick={changeCurrentBotSettingsHandler}
  162. onCancelClick={cancelBotChangeHandler}
  163. />
  164. <DeleteSlackBotSettingsModal
  165. isResetAll
  166. isOpen={isDeleteConfirmModalShown}
  167. onClose={() => setIsDeleteConfirmModalShown(false)}
  168. onClickDeleteButton={resetAllSettings}
  169. />
  170. <div className="selecting-bot-type mb-5">
  171. <h2 className="admin-setting-header mb-4">
  172. {t('admin:slack_integration.selecting_bot_types.slack_bot')}
  173. {/* TODO: add an appropriate link by GW-5614 */}
  174. <a className="ml-2 btn-link" href="#">
  175. {t('admin:slack_integration.selecting_bot_types.detailed_explanation')}
  176. <i className="fa fa-external-link ml-1" aria-hidden="true"></i>
  177. </a>
  178. </h2>
  179. <div className="d-flex justify-content-end">
  180. <button
  181. className="btn btn-outline-danger"
  182. type="button"
  183. onClick={() => setIsDeleteConfirmModalShown(true)}
  184. >{t('admin:slack_integration.reset_all_settings')}
  185. </button>
  186. </div>
  187. <div className="row my-5 flex-wrap-reverse justify-content-center">
  188. {botTypes.map((botType) => {
  189. return (
  190. <div key={botType} className="m-3">
  191. <BotTypeCard
  192. botType={botType}
  193. isActive={currentBotType === botType}
  194. onBotTypeSelectHandler={botTypeSelectHandler}
  195. />
  196. </div>
  197. );
  198. })}
  199. </div>
  200. </div>
  201. {settingsComponent}
  202. </>
  203. );
  204. };
  205. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  206. SlackIntegration.propTypes = {
  207. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  208. };
  209. export default SlackIntegrationWrapper;