SlackIntegration.jsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 resetSettings = async() => {
  46. try {
  47. await appContainer.apiv3.put('/slack-integration-settings/bot-type', { currentBotType });
  48. fetchSlackIntegrationData();
  49. toastSuccess('success');
  50. }
  51. catch (error) {
  52. toastError(error);
  53. }
  54. };
  55. useEffect(() => {
  56. fetchSlackIntegrationData();
  57. }, [fetchSlackIntegrationData]);
  58. const handleBotTypeSelect = (clickedBotType) => {
  59. if (clickedBotType === currentBotType) {
  60. return;
  61. }
  62. if (currentBotType === null) {
  63. setCurrentBotType(clickedBotType);
  64. return;
  65. }
  66. setSelectedBotType(clickedBotType);
  67. };
  68. const cancelBotChangeHandler = () => {
  69. setSelectedBotType(null);
  70. };
  71. const changeCurrentBotSettingsHandler = async() => {
  72. try {
  73. const res = await appContainer.apiv3.put('/slack-integration-settings/bot-type', {
  74. currentBotType: selectedBotType,
  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. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  84. }
  85. catch (err) {
  86. toastError(err);
  87. }
  88. };
  89. let settingsComponent = null;
  90. switch (currentBotType) {
  91. case 'officialBot':
  92. settingsComponent = <OfficialBotSettings />;
  93. break;
  94. case 'customBotWithoutProxy':
  95. settingsComponent = (
  96. <CustomBotWithoutProxySettings
  97. isSendTestMessage={isSendTestMessage}
  98. isRegisterSlackCredentials={isRegisterSlackCredentials}
  99. currentBotType={currentBotType}
  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={resetSettings}
  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. {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
  133. <div className="row my-5 flex-wrap-reverse justify-content-center">
  134. {botTypes.map((botType) => {
  135. return (
  136. <div key={botType} className="m-3">
  137. <BotTypeCard
  138. botType={botType}
  139. isActive={currentBotType === botType}
  140. handleBotTypeSelect={handleBotTypeSelect}
  141. />
  142. </div>
  143. );
  144. })}
  145. </div>
  146. </div>
  147. {settingsComponent}
  148. </>
  149. );
  150. };
  151. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  152. SlackIntegration.propTypes = {
  153. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  154. };
  155. export default SlackIntegrationWrapper;