SlackIntegration.jsx 7.5 KB

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