SlackIntegration.jsx 8.4 KB

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