SlackIntegration.jsx 7.3 KB

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