SlackIntegration.jsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 [isConnectedToSlack, setIsConnectedToSlack] = useState(false);
  23. const [isRegisterSlackCredentials, setIsRegisterSlackCredentials] = useState(false);
  24. const [isSendTestMessage, setIsSendTestMessage] = useState(false);
  25. const [isSetupSlackBot, setIsSetupSlackBot] = useState(false);
  26. const fetchData = useCallback(async() => {
  27. try {
  28. const response = await appContainer.apiv3.get('slack-integration/');
  29. const { currentBotType, customBotWithoutProxySettings } = response.data.slackBotSettingParams;
  30. const {
  31. slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars,
  32. isSetupSlackBot, isConnectedToSlack,
  33. } = customBotWithoutProxySettings;
  34. setCurrentBotType(currentBotType);
  35. setSlackSigningSecret(slackSigningSecret);
  36. setSlackBotToken(slackBotToken);
  37. setSlackSigningSecretEnv(slackSigningSecretEnvVars);
  38. setSlackBotTokenEnv(slackBotTokenEnvVars);
  39. setIsSetupSlackBot(isSetupSlackBot);
  40. setIsConnectedToSlack(isConnectedToSlack);
  41. if (isConnectedToSlack) {
  42. setIsRegisterSlackCredentials(true);
  43. }
  44. else {
  45. setIsRegisterSlackCredentials(false);
  46. setIsSendTestMessage(false);
  47. }
  48. }
  49. catch (err) {
  50. toastError(err);
  51. }
  52. }, [appContainer.apiv3]);
  53. useEffect(() => {
  54. fetchData();
  55. }, [fetchData]);
  56. const handleBotTypeSelect = (clickedBotType) => {
  57. if (clickedBotType === currentBotType) {
  58. return;
  59. }
  60. if (currentBotType === null) {
  61. setCurrentBotType(clickedBotType);
  62. return;
  63. }
  64. setSelectedBotType(clickedBotType);
  65. };
  66. const cancelBotChangeHandler = () => {
  67. setSelectedBotType(null);
  68. };
  69. const changeCurrentBotSettingsHandler = async() => {
  70. try {
  71. const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
  72. slackSigningSecret: '',
  73. slackBotToken: '',
  74. currentBotType: selectedBotType,
  75. });
  76. setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
  77. setSelectedBotType(null);
  78. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  79. setIsRegisterSlackCredentials(false);
  80. setIsConnectedToSlack(false);
  81. setSlackSigningSecret(null);
  82. setSlackBotToken(null);
  83. setIsSendTestMessage(false);
  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. isConnectedToSlack={isConnectedToSlack}
  100. slackBotTokenEnv={slackBotTokenEnv}
  101. slackBotToken={slackBotToken}
  102. slackSigningSecretEnv={slackSigningSecretEnv}
  103. slackSigningSecret={slackSigningSecret}
  104. isSetupSlackBot={isSetupSlackBot}
  105. onSetSlackSigningSecret={setSlackSigningSecret}
  106. onSetSlackBotToken={setSlackBotToken}
  107. onSetIsSendTestMessage={setIsSendTestMessage}
  108. fetchData={fetchData}
  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 my-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-4">
  134. <div className="card-deck mx-auto">
  135. {botTypes.map((botType) => {
  136. return (
  137. <BotTypeCard
  138. key={botType}
  139. botType={botType}
  140. isActive={currentBotType === botType}
  141. handleBotTypeSelect={handleBotTypeSelect}
  142. />
  143. );
  144. })}
  145. </div>
  146. </div>
  147. </div>
  148. {settingsComponent}
  149. </>
  150. );
  151. };
  152. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  153. SlackIntegration.propTypes = {
  154. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  155. };
  156. export default SlackIntegrationWrapper;