SlackIntegration.jsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 createSlackIntegrationData = async() => {
  62. try {
  63. await appContainer.apiv3.put('/slack-integration-settings/slack-app-integrations');
  64. fetchSlackIntegrationData();
  65. toastSuccess(t('admin:slack_integration.adding_slack_ws_integration_settings_successful'));
  66. }
  67. catch (error) {
  68. toastError(error);
  69. }
  70. };
  71. const changeSecretAndToken = (secret, token) => {
  72. setSlackSigningSecret(secret);
  73. setSlackBotToken(token);
  74. };
  75. useEffect(() => {
  76. fetchSlackIntegrationData();
  77. }, [fetchSlackIntegrationData]);
  78. const changeCurrentBotSettings = async(botType) => {
  79. try {
  80. const res = await appContainer.apiv3.put('/slack-integration-settings/bot-type', {
  81. currentBotType: botType,
  82. });
  83. setCurrentBotType(res.data.slackBotTypeParam.slackBotType);
  84. setSelectedBotType(null);
  85. setIsRegisterSlackCredentials(false);
  86. setSlackSigningSecret(null);
  87. setSlackBotToken(null);
  88. setSlackWSNameInWithoutProxy(null);
  89. setConnectionStatuses({});
  90. }
  91. catch (err) {
  92. toastError(err);
  93. }
  94. };
  95. const botTypeSelectHandler = async(botType) => {
  96. if (botType === currentBotType) {
  97. return;
  98. }
  99. if (currentBotType == null) {
  100. return changeCurrentBotSettings(botType);
  101. }
  102. setSelectedBotType(botType);
  103. };
  104. const changeCurrentBotSettingsHandler = async() => {
  105. changeCurrentBotSettings(selectedBotType);
  106. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  107. };
  108. const cancelBotChangeHandler = () => {
  109. setSelectedBotType(null);
  110. };
  111. let settingsComponent = null;
  112. switch (currentBotType) {
  113. case 'officialBot':
  114. settingsComponent = (
  115. <OfficialBotSettings
  116. slackAppIntegrations={slackAppIntegrations}
  117. proxyServerUri={proxyServerUri}
  118. onClickAddSlackWorkspaceBtn={createSlackIntegrationData}
  119. connectionStatuses={connectionStatuses}
  120. />
  121. );
  122. break;
  123. case 'customBotWithoutProxy':
  124. settingsComponent = (
  125. <CustomBotWithoutProxySettings
  126. isRegisterSlackCredentials={isRegisterSlackCredentials}
  127. slackBotTokenEnv={slackBotTokenEnv}
  128. slackBotToken={slackBotToken}
  129. slackSigningSecretEnv={slackSigningSecretEnv}
  130. slackSigningSecret={slackSigningSecret}
  131. slackWSNameInWithoutProxy={slackWSNameInWithoutProxy}
  132. fetchSlackIntegrationData={fetchSlackIntegrationData}
  133. onUpdatedSecretToken={changeSecretAndToken}
  134. connectionStatuses={connectionStatuses}
  135. />
  136. );
  137. break;
  138. case 'customBotWithProxy':
  139. settingsComponent = (
  140. <CustomBotWithProxySettings
  141. slackAppIntegrations={slackAppIntegrations}
  142. proxyServerUri={proxyServerUri}
  143. onClickAddSlackWorkspaceBtn={createSlackIntegrationData}
  144. fetchSlackIntegrationData={fetchSlackIntegrationData}
  145. connectionStatuses={connectionStatuses}
  146. />
  147. );
  148. break;
  149. }
  150. return (
  151. <>
  152. <ConfirmBotChangeModal
  153. isOpen={selectedBotType != null}
  154. onConfirmClick={changeCurrentBotSettingsHandler}
  155. onCancelClick={cancelBotChangeHandler}
  156. />
  157. <DeleteSlackBotSettingsModal
  158. isResetAll
  159. isOpen={isDeleteConfirmModalShown}
  160. onClose={() => setIsDeleteConfirmModalShown(false)}
  161. onClickDeleteButton={resetAllSettings}
  162. />
  163. <div className="selecting-bot-type mb-5">
  164. <h2 className="admin-setting-header mb-4">
  165. {t('admin:slack_integration.selecting_bot_types.slack_bot')}
  166. {/* TODO: add an appropriate link by GW-5614 */}
  167. <a className="ml-2 btn-link" href="#">
  168. {t('admin:slack_integration.selecting_bot_types.detailed_explanation')}
  169. <i className="fa fa-external-link ml-1" aria-hidden="true"></i>
  170. </a>
  171. </h2>
  172. <div className="d-flex justify-content-end">
  173. <button
  174. className="btn btn-outline-danger"
  175. type="button"
  176. onClick={() => setIsDeleteConfirmModalShown(true)}
  177. >{t('admin:slack_integration.reset_all_settings')}
  178. </button>
  179. </div>
  180. <div className="row my-5 flex-wrap-reverse justify-content-center">
  181. {botTypes.map((botType) => {
  182. return (
  183. <div key={botType} className="m-3">
  184. <BotTypeCard
  185. botType={botType}
  186. isActive={currentBotType === botType}
  187. onBotTypeSelectHandler={botTypeSelectHandler}
  188. />
  189. </div>
  190. );
  191. })}
  192. </div>
  193. </div>
  194. {settingsComponent}
  195. </>
  196. );
  197. };
  198. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  199. SlackIntegration.propTypes = {
  200. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  201. };
  202. export default SlackIntegrationWrapper;