SlackIntegration.jsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. }
  45. catch (err) {
  46. toastError(err);
  47. }
  48. }, [appContainer.apiv3]);
  49. useEffect(() => {
  50. fetchData();
  51. }, [fetchData]);
  52. const handleBotTypeSelect = (clickedBotType) => {
  53. if (clickedBotType === currentBotType) {
  54. return;
  55. }
  56. if (currentBotType === null) {
  57. setCurrentBotType(clickedBotType);
  58. return;
  59. }
  60. setSelectedBotType(clickedBotType);
  61. };
  62. const cancelBotChangeHandler = () => {
  63. setSelectedBotType(null);
  64. };
  65. const changeCurrentBotSettingsHandler = async() => {
  66. try {
  67. const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
  68. slackSigningSecret: '',
  69. slackBotToken: '',
  70. currentBotType: selectedBotType,
  71. });
  72. setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
  73. setSelectedBotType(null);
  74. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  75. setIsRegisterSlackCredentials(false);
  76. setSlackSigningSecret(null);
  77. setSlackBotToken(null);
  78. setIsSendTestMessage(false);
  79. }
  80. catch (err) {
  81. toastError(err);
  82. }
  83. };
  84. let settingsComponent = null;
  85. switch (currentBotType) {
  86. case 'officialBot':
  87. settingsComponent = <OfficialBotSettings />;
  88. break;
  89. case 'customBotWithoutProxy':
  90. settingsComponent = (
  91. <CustomBotWithoutProxySettings
  92. isSendTestMessage={isSendTestMessage}
  93. isRegisterSlackCredentials={isRegisterSlackCredentials}
  94. isConnectedToSlack={isConnectedToSlack}
  95. slackBotTokenEnv={slackBotTokenEnv}
  96. slackBotToken={slackBotToken}
  97. slackSigningSecretEnv={slackSigningSecretEnv}
  98. slackSigningSecret={slackSigningSecret}
  99. isSetupSlackBot={isSetupSlackBot}
  100. onSetSlackSigningSecret={setSlackSigningSecret}
  101. onSetSlackBotToken={setSlackBotToken}
  102. onSetIsSendTestMessage={setIsSendTestMessage}
  103. onSetIsRegisterSlackCredentials={setIsRegisterSlackCredentials}
  104. />
  105. );
  106. break;
  107. case 'customBotWithProxy':
  108. settingsComponent = <CustomBotWithProxySettings />;
  109. break;
  110. }
  111. return (
  112. <>
  113. <ConfirmBotChangeModal
  114. isOpen={selectedBotType != null}
  115. onConfirmClick={changeCurrentBotSettingsHandler}
  116. onCancelClick={cancelBotChangeHandler}
  117. />
  118. <div className="selecting-bot-type my-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. {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
  128. <div className="row my-4 flex-wrap-reverse justify-content-center">
  129. {botTypes.map((botType) => {
  130. return (
  131. <div className="m-3">
  132. <BotTypeCard
  133. key={botType}
  134. botType={botType}
  135. isActive={currentBotType === botType}
  136. handleBotTypeSelect={handleBotTypeSelect}
  137. />
  138. </div>
  139. );
  140. })}
  141. </div>
  142. </div>
  143. {settingsComponent}
  144. </>
  145. );
  146. };
  147. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  148. SlackIntegration.propTypes = {
  149. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  150. };
  151. export default SlackIntegrationWrapper;