SlackIntegration.jsx 6.5 KB

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