SlackIntegration.jsx 7.9 KB

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