SlackIntegration.jsx 7.2 KB

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