SlackIntegration.jsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 AccessTokenSettings from './AccessTokenSettings';
  8. import OfficialBotSettings from './OfficialBotSettings';
  9. import CustomBotWithoutProxySettings from './CustomBotWithoutProxySettings';
  10. import CustomBotWithProxySettings from './CustomBotWithProxySettings';
  11. import ConfirmBotChangeModal from './ConfirmBotChangeModal';
  12. import BotTypeCard from './BotTypeCard';
  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 [accessToken, setAccessToken] = useState('');
  20. const [slackSigningSecret, setSlackSigningSecret] = useState('');
  21. const [slackBotToken, setSlackBotToken] = useState('');
  22. const [slackSigningSecretEnv, setSlackSigningSecretEnv] = useState('');
  23. const [slackBotTokenEnv, setSlackBotTokenEnv] = useState('');
  24. const [isConnectedToSlack, setIsConnectedToSlack] = useState(null);
  25. const [isRgisterSlackCredentials, setIsRgisterSlackCredentials] = useState(false);
  26. const fetchData = useCallback(async() => {
  27. try {
  28. const response = await appContainer.apiv3.get('slack-integration/');
  29. const { currentBotType, customBotWithoutProxySettings, accessToken } = response.data.slackBotSettingParams;
  30. const {
  31. slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars,
  32. } = customBotWithoutProxySettings;
  33. setCurrentBotType(currentBotType);
  34. setAccessToken(accessToken);
  35. setSlackSigningSecret(slackSigningSecret);
  36. setSlackBotToken(slackBotToken);
  37. setSlackSigningSecretEnv(slackSigningSecretEnvVars);
  38. setSlackBotTokenEnv(slackBotTokenEnvVars);
  39. setIsConnectedToSlack(isConnectedToSlack);
  40. if ((slackBotToken && slackSigningSecret) || (slackBotTokenEnv && slackSigningSecretEnv)) {
  41. setIsRgisterSlackCredentials(true);
  42. }
  43. }
  44. catch (err) {
  45. toastError(err);
  46. }
  47. }, [appContainer.apiv3, isConnectedToSlack, slackBotTokenEnv, slackSigningSecretEnv]);
  48. useEffect(() => {
  49. fetchData();
  50. }, [fetchData]);
  51. const handleBotTypeSelect = (clickedBotType) => {
  52. if (clickedBotType === currentBotType) {
  53. return;
  54. }
  55. if (currentBotType === null) {
  56. setCurrentBotType(clickedBotType);
  57. return;
  58. }
  59. setSelectedBotType(clickedBotType);
  60. };
  61. const cancelBotChangeHandler = () => {
  62. setSelectedBotType(null);
  63. };
  64. const changeCurrentBotSettingsHandler = async() => {
  65. try {
  66. const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
  67. slackSigningSecret: '',
  68. slackBotToken: '',
  69. currentBotType: selectedBotType,
  70. });
  71. setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
  72. setSelectedBotType(null);
  73. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  74. }
  75. catch (err) {
  76. toastError(err);
  77. }
  78. };
  79. const generateTokenHandler = async() => {
  80. try {
  81. await appContainer.apiv3.put('slack-integration/access-token');
  82. fetchData();
  83. }
  84. catch (err) {
  85. toastError(err);
  86. }
  87. };
  88. const discardTokenHandler = async() => {
  89. try {
  90. await appContainer.apiv3.delete('slack-integration/access-token');
  91. fetchData();
  92. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  93. }
  94. catch (err) {
  95. toastError(err);
  96. }
  97. };
  98. let settingsComponent = null;
  99. switch (currentBotType) {
  100. case 'officialBot':
  101. settingsComponent = <OfficialBotSettings />;
  102. break;
  103. case 'customBotWithoutProxy':
  104. settingsComponent = (
  105. <CustomBotWithoutProxySettings {
  106. ...{
  107. slackBotToken,
  108. slackBotTokenEnv,
  109. slackSigningSecret,
  110. slackSigningSecretEnv,
  111. isConnectedToSlack,
  112. isRgisterSlackCredentials,
  113. }
  114. }
  115. />
  116. );
  117. break;
  118. case 'customBotWithProxy':
  119. settingsComponent = <CustomBotWithProxySettings />;
  120. break;
  121. }
  122. return (
  123. <>
  124. <ConfirmBotChangeModal
  125. isOpen={selectedBotType != null}
  126. onConfirmClick={changeCurrentBotSettingsHandler}
  127. onCancelClick={cancelBotChangeHandler}
  128. />
  129. <AccessTokenSettings
  130. accessToken={accessToken}
  131. onClickDiscardButton={discardTokenHandler}
  132. onClickGenerateToken={generateTokenHandler}
  133. />
  134. <div className="selecting-bot-type my-5">
  135. <h2 className="admin-setting-header mb-4">
  136. {t('admin:slack_integration.selecting_bot_types.slack_bot')}
  137. <span className="ml-2 btn-link">
  138. <span className="mr-1">{t('admin:slack_integration.selecting_bot_types.detailed_explanation')}</span>
  139. {/* TODO: add an appropriate link by GW-5614 */}
  140. <i className="fa fa-external-link" aria-hidden="true"></i>
  141. </span>
  142. </h2>
  143. {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
  144. <div className="row my-4">
  145. <div className="card-deck mx-auto">
  146. {botTypes.map((botType) => {
  147. return (
  148. <BotTypeCard
  149. key={botType}
  150. botType={botType}
  151. isActive={currentBotType === botType}
  152. handleBotTypeSelect={handleBotTypeSelect}
  153. />
  154. );
  155. })}
  156. </div>
  157. </div>
  158. </div>
  159. {settingsComponent}
  160. </>
  161. );
  162. };
  163. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  164. SlackIntegration.propTypes = {
  165. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  166. };
  167. export default SlackIntegrationWrapper;