SlackIntegration.jsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. const botTypes = {
  94. officialBot: {
  95. name: t('admin:slack_integration.selecting_bot_types.official_bot'),
  96. level: t('admin:slack_integration.selecting_bot_types.for_beginners'),
  97. setUp: 'easy',
  98. multiWSIntegration: 'possible',
  99. securityControl: 'impossible',
  100. },
  101. customBotWithoutProxy: {
  102. name: t('admin:slack_integration.selecting_bot_types.custom_bot'),
  103. supplementaryBotName: t('admin:slack_integration.selecting_bot_types.without_proxy'),
  104. level: t('admin:slack_integration.selecting_bot_types.for_intermediate'),
  105. setUp: 'normal',
  106. multiWSIntegration: 'impossible',
  107. securityControl: 'possible',
  108. },
  109. customBotWithProxy: {
  110. name: t('admin:slack_integration.selecting_bot_types.custom_bot'),
  111. supplementaryBotName: t('admin:slack_integration.selecting_bot_types.with_proxy'),
  112. level: t('admin:slack_integration.selecting_bot_types.for_advanced'),
  113. setUp: 'hard',
  114. multiWSIntegration: 'possible',
  115. securityControl: 'impossible',
  116. },
  117. };
  118. const renderRecommendedBadge = () => {
  119. return (
  120. <span className="badge badge-info mr-2">
  121. {t('admin:slack_integration.selecting_bot_types.recommended')}
  122. </span>
  123. );
  124. };
  125. const renderBotTypeCards = () => {
  126. return (
  127. Object.entries(botTypes).map(([key, value]) => {
  128. return (
  129. <div
  130. className={`card admin-bot-card mx-3 rounded border-radius-sm shadow ${currentBotType === `${key}` ? 'border-primary' : ''}`}
  131. onClick={() => handleBotTypeSelect(`${key}`)}
  132. role="button"
  133. key={key}
  134. >
  135. <div>
  136. <h3 className={`card-header mb-0 py-3 text-center ${currentBotType === `${key}` ? 'bg-primary text-light' : ''}`}>
  137. <span className="mr-2">
  138. {value.name}
  139. </span>
  140. <span className="supplementary-bot-name mr-2">
  141. {value.supplementaryBotName}
  142. </span>
  143. {key === 'officialBot' ? renderRecommendedBadge() : ''}
  144. {/* TODO: add an appropriate links by GW-5614 */}
  145. <i className={`fa fa-external-link btn-link ${currentBotType === `${key}` ? 'bg-primary text-light' : ''}`} aria-hidden="true"></i>
  146. </h3>
  147. </div>
  148. <div className="card-body p-4">
  149. <div className="card-text">
  150. <div className="text-center">
  151. {value.level}
  152. </div>
  153. <div className="my-4">
  154. <div className="d-flex justify-content-between mb-2">
  155. <span>{t('admin:slack_integration.selecting_bot_types.set_up')}</span>
  156. <span className={`bot-type-disc-${value.setUp}`}>{t(`admin:slack_integration.selecting_bot_types.${value.setUp}`)}</span>
  157. </div>
  158. <div className="d-flex justify-content-between mb-2">
  159. <span>{t('admin:slack_integration.selecting_bot_types.multiple_workspaces_integration')}</span>
  160. <span className={`bot-type-disc-${value.multiWSIntegration}`}>
  161. {t(`admin:slack_integration.selecting_bot_types.${value.multiWSIntegration}`)}
  162. </span>
  163. </div>
  164. <div className="d-flex justify-content-between">
  165. <span>{t('admin:slack_integration.selecting_bot_types.security_control')}</span>
  166. <span className={`bot-type-disc-${value.securityControl}`}>
  167. {t(`admin:slack_integration.selecting_bot_types.${value.securityControl}`)}
  168. </span>
  169. </div>
  170. </div>
  171. </div>
  172. </div>
  173. </div>
  174. );
  175. })
  176. );
  177. };
  178. return (
  179. <>
  180. <ConfirmBotChangeModal
  181. isOpen={selectedBotType != null}
  182. onConfirmClick={changeCurrentBotSettingsHandler}
  183. onCancelClick={cancelBotChangeHandler}
  184. />
  185. <AccessTokenSettings
  186. accessToken={accessToken}
  187. onClickDiscardButton={discardTokenHandler}
  188. onClickGenerateToken={generateTokenHandler}
  189. />
  190. <div className="selecting-bot-type my-5">
  191. <h2 className="admin-setting-header mb-4">
  192. {t('admin:slack_integration.selecting_bot_types.slack_bot')}
  193. <span className="ml-2 btn-link">
  194. <span className="mr-1">{t('admin:slack_integration.selecting_bot_types.detailed_explanation')}</span>
  195. {/* TODO: add an appropriate link by GW-5614 */}
  196. <i className="fa fa-external-link" aria-hidden="true"></i>
  197. </span>
  198. </h2>
  199. {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
  200. <div className="row my-4">
  201. <div className="card-deck mx-auto">
  202. {renderBotTypeCards()}
  203. </div>
  204. </div>
  205. </div>
  206. {settingsComponent}
  207. </>
  208. );
  209. };
  210. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  211. SlackIntegration.propTypes = {
  212. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  213. };
  214. export default SlackIntegrationWrapper;