SlackIntegration.jsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. const SlackIntegration = (props) => {
  13. const { appContainer } = props;
  14. const { t } = useTranslation();
  15. const [currentBotType, setCurrentBotType] = useState(null);
  16. const [selectedBotType, setSelectedBotType] = useState(null);
  17. const [accessToken, setAccessToken] = useState('');
  18. const fetchData = useCallback(async() => {
  19. try {
  20. const response = await appContainer.apiv3.get('slack-integration/');
  21. const { currentBotType, accessToken } = response.data.slackBotSettingParams;
  22. setCurrentBotType(currentBotType);
  23. setAccessToken(accessToken);
  24. }
  25. catch (err) {
  26. toastError(err);
  27. }
  28. }, [appContainer.apiv3]);
  29. useEffect(() => {
  30. fetchData();
  31. }, [fetchData]);
  32. const handleBotTypeSelect = (clickedBotType) => {
  33. if (clickedBotType === currentBotType) {
  34. return;
  35. }
  36. if (currentBotType === null) {
  37. setCurrentBotType(clickedBotType);
  38. return;
  39. }
  40. setSelectedBotType(clickedBotType);
  41. };
  42. const cancelBotChangeHandler = () => {
  43. setSelectedBotType(null);
  44. };
  45. const changeCurrentBotSettingsHandler = async() => {
  46. try {
  47. const res = await appContainer.apiv3.put('slack-integration/custom-bot-without-proxy', {
  48. slackSigningSecret: '',
  49. slackBotToken: '',
  50. currentBotType: selectedBotType,
  51. });
  52. setCurrentBotType(res.data.customBotWithoutProxySettingParams.slackBotType);
  53. setSelectedBotType(null);
  54. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  55. }
  56. catch (err) {
  57. toastError(err);
  58. }
  59. };
  60. const generateTokenHandler = async() => {
  61. try {
  62. await appContainer.apiv3.put('slack-integration/access-token');
  63. fetchData();
  64. }
  65. catch (err) {
  66. toastError(err);
  67. }
  68. };
  69. const discardTokenHandler = async() => {
  70. try {
  71. await appContainer.apiv3.delete('slack-integration/access-token');
  72. fetchData();
  73. toastSuccess(t('admin:slack_integration.bot_reset_successful'));
  74. }
  75. catch (err) {
  76. toastError(err);
  77. }
  78. };
  79. let settingsComponent = null;
  80. switch (currentBotType) {
  81. case 'official-bot':
  82. settingsComponent = <OfficialBotSettings />;
  83. break;
  84. case 'custom-bot-without-proxy':
  85. settingsComponent = (
  86. <CustomBotWithoutProxySettings />
  87. );
  88. break;
  89. case 'custom-bot-with-proxy':
  90. settingsComponent = <CustomBotWithProxySettings />;
  91. break;
  92. }
  93. return (
  94. <>
  95. <ConfirmBotChangeModal
  96. isOpen={selectedBotType != null}
  97. onConfirmClick={changeCurrentBotSettingsHandler}
  98. onCancelClick={cancelBotChangeHandler}
  99. />
  100. <AccessTokenSettings
  101. accessToken={accessToken}
  102. onClickDiscardButton={discardTokenHandler}
  103. onClickGenerateToken={generateTokenHandler}
  104. />
  105. <div className="row my-5">
  106. <div className="card-deck mx-auto">
  107. <div
  108. className={`card admin-bot-card mx-3 py-5 rounded ${currentBotType === 'official-bot' ? 'border-info' : ''}`}
  109. onClick={() => handleBotTypeSelect('official-bot')}
  110. >
  111. <div className="card-body">
  112. <h5 className="card-title">Official Bot</h5>
  113. <p className="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
  114. </div>
  115. </div>
  116. <div
  117. className={`card admin-bot-card mx-3 py-5 rounded ${currentBotType === 'custom-bot-without-proxy' ? 'border-info' : ''}`}
  118. onClick={() => handleBotTypeSelect('custom-bot-without-proxy')}
  119. >
  120. <div className="card-body">
  121. <h5 className="card-title">Custom Bot (Without Proxy)</h5>
  122. <p className="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. </p>
  123. </div>
  124. </div>
  125. <div
  126. className={`card admin-bot-card mx-3 py-5 rounded ${currentBotType === 'custom-bot-with-proxy' ? 'border-info' : ''}`}
  127. onClick={() => handleBotTypeSelect('custom-bot-with-proxy')}
  128. >
  129. <div className="card-body">
  130. <h5 className="card-title">Custom Bot (With Proxy)</h5>
  131. <p className="card-text">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
  132. </div>
  133. </div>
  134. </div>
  135. </div>
  136. {settingsComponent}
  137. </>
  138. );
  139. };
  140. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  141. SlackIntegration.propTypes = {
  142. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  143. };
  144. export default SlackIntegrationWrapper;