SlackIntegration.jsx 6.4 KB

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