SlackIntegration.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 fetchData = useCallback(async() => {
  25. try {
  26. const response = await appContainer.apiv3.get('slack-integration/');
  27. const { currentBotType, customBotWithoutProxySettings, accessToken } = response.data.slackBotSettingParams;
  28. const {
  29. slackSigningSecret, slackBotToken, slackSigningSecretEnvVars, slackBotTokenEnvVars,
  30. } = customBotWithoutProxySettings;
  31. setCurrentBotType(currentBotType);
  32. setAccessToken(accessToken);
  33. setSlackSigningSecret(slackSigningSecret);
  34. setSlackBotToken(slackBotToken);
  35. setSlackSigningSecretEnv(slackSigningSecretEnvVars);
  36. setSlackBotTokenEnv(slackBotTokenEnvVars);
  37. }
  38. catch (err) {
  39. toastError(err);
  40. }
  41. }, [appContainer.apiv3]);
  42. useEffect(() => {
  43. fetchData();
  44. }, [fetchData]);
  45. const handleBotTypeSelect = (clickedBotType) => {
  46. if (clickedBotType === currentBotType) {
  47. return;
  48. }
  49. if (currentBotType === null) {
  50. setCurrentBotType(clickedBotType);
  51. return;
  52. }
  53. setSelectedBotType(clickedBotType);
  54. };
  55. const cancelBotChangeHandler = () => {
  56. setSelectedBotType(null);
  57. };
  58. const changeCurrentBotSettingsHandler = async() => {
  59. try {
  60. const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
  61. slackSigningSecret: '',
  62. slackBotToken: '',
  63. currentBotType: selectedBotType,
  64. });
  65. setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
  66. setSelectedBotType(null);
  67. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  68. }
  69. catch (err) {
  70. toastError(err);
  71. }
  72. };
  73. const generateTokenHandler = async() => {
  74. try {
  75. await appContainer.apiv3.put('slack-integration/access-token');
  76. fetchData();
  77. }
  78. catch (err) {
  79. toastError(err);
  80. }
  81. };
  82. const discardTokenHandler = async() => {
  83. try {
  84. await appContainer.apiv3.delete('slack-integration/access-token');
  85. fetchData();
  86. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  87. }
  88. catch (err) {
  89. toastError(err);
  90. }
  91. };
  92. let settingsComponent = null;
  93. switch (currentBotType) {
  94. case 'officialBot':
  95. settingsComponent = <OfficialBotSettings />;
  96. break;
  97. case 'customBotWithoutProxy':
  98. settingsComponent = (
  99. <CustomBotWithoutProxySettings {...props} />
  100. );
  101. break;
  102. case 'customBotWithProxy':
  103. settingsComponent = <CustomBotWithProxySettings />;
  104. break;
  105. }
  106. return (
  107. <>
  108. <ConfirmBotChangeModal
  109. isOpen={selectedBotType != null}
  110. onConfirmClick={changeCurrentBotSettingsHandler}
  111. onCancelClick={cancelBotChangeHandler}
  112. />
  113. <AccessTokenSettings
  114. accessToken={accessToken}
  115. onClickDiscardButton={discardTokenHandler}
  116. onClickGenerateToken={generateTokenHandler}
  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. <span className="ml-2 btn-link">
  122. <span className="mr-1">{t('admin:slack_integration.selecting_bot_types.detailed_explanation')}</span>
  123. {/* TODO: add an appropriate link by GW-5614 */}
  124. <i className="fa fa-external-link" aria-hidden="true"></i>
  125. </span>
  126. </h2>
  127. {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
  128. <div className="row my-4">
  129. <div className="card-deck mx-auto">
  130. {botTypes.map((botType) => {
  131. return (
  132. <BotTypeCard
  133. key={botType}
  134. botType={botType}
  135. isActive={currentBotType === botType}
  136. handleBotTypeSelect={handleBotTypeSelect}
  137. />
  138. );
  139. })}
  140. </div>
  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;