SlackIntegration.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 showBotTypeLevel = (level) => {
  94. return <span>{t(`admin:slack_integration.selecting_bot_types.${level}`)}</span>;
  95. };
  96. const showBotTypeLabel = (label) => {
  97. return <span>{t(`admin:slack_integration.selecting_bot_types.${label}`)}</span>;
  98. };
  99. const showBotTypeDiscription = (desc) => {
  100. return <span className={`bot-type-disc-${desc}`}>{t(`admin:slack_integration.selecting_bot_types.${desc}`)}</span>;
  101. };
  102. return (
  103. <>
  104. <ConfirmBotChangeModal
  105. isOpen={selectedBotType != null}
  106. onConfirmClick={changeCurrentBotSettingsHandler}
  107. onCancelClick={cancelBotChangeHandler}
  108. />
  109. <AccessTokenSettings
  110. accessToken={accessToken}
  111. onClickDiscardButton={discardTokenHandler}
  112. onClickGenerateToken={generateTokenHandler}
  113. />
  114. <div className="selecting-bot-type my-5">
  115. <h2 className="admin-setting-header mb-4">
  116. {t('admin:slack_integration.selecting_bot_types.slack_bot')}
  117. <span className="ml-2 btn-link">
  118. <span className="mr-1">{t('admin:slack_integration.selecting_bot_types.detailed_explanation')}</span>
  119. {/* TODO: add an appropriate link by GW-5614 */}
  120. <i className="fa fa-external-link" aria-hidden="true"></i>
  121. </span>
  122. </h2>
  123. {t('admin:slack_integration.selecting_bot_types.selecting_bot_type')}
  124. <div className="row my-4">
  125. <div className="card-deck mx-auto">
  126. <div
  127. className={`card admin-bot-card mx-3 rounded border-radius-sm shadow ${currentBotType === 'official-bot' ? 'border-primary' : ''}`}
  128. onClick={() => handleBotTypeSelect('official-bot')}
  129. role="button"
  130. >
  131. <div>
  132. <h3 className={`card-header mb-0 py-3 text-center ${currentBotType === 'official-bot' ? 'bg-primary text-light' : ''}`}>
  133. <span className="mr-2">
  134. {t('admin:slack_integration.selecting_bot_types.official_bot')}
  135. </span>
  136. <span className="badge badge-info mr-2">
  137. {t('admin:slack_integration.selecting_bot_types.recommended')}
  138. </span>
  139. {/* TODO: add an appropriate link by GW-5614 */}
  140. <i className={`fa fa-external-link btn-link ${currentBotType === 'official-bot' ? 'bg-primary text-light' : ''}`} aria-hidden="true"></i>
  141. </h3>
  142. </div>
  143. <div className="card-body p-4">
  144. <p className="card-text">
  145. <div className="text-center">
  146. {showBotTypeLevel('for_beginners')}
  147. </div>
  148. <div className="my-4">
  149. <div className="d-flex justify-content-between mb-2">
  150. {showBotTypeLabel('set_up')}
  151. {showBotTypeDiscription('easy')}
  152. </div>
  153. <div className="d-flex justify-content-between mb-2">
  154. {showBotTypeLabel('multiple_workspaces_integration')}
  155. {showBotTypeDiscription('possible')}
  156. </div>
  157. <div className="d-flex justify-content-between">
  158. {showBotTypeLabel('security_control')}
  159. {showBotTypeDiscription('impossible')}
  160. </div>
  161. </div>
  162. </p>
  163. </div>
  164. </div>
  165. <div
  166. className={`card admin-bot-card mx-3 rounded shadow ${currentBotType === 'custom-bot-without-proxy' ? 'border-primary' : ''}`}
  167. onClick={() => handleBotTypeSelect('custom-bot-without-proxy')}
  168. role="button"
  169. >
  170. <h3 className={`card-header mb-0 py-3 text-center text-nowrap ${currentBotType === 'custom-bot-without-proxy' ? 'bg-primary text-light' : ''}`}>
  171. <span className="mr-2">
  172. {t('admin:slack_integration.selecting_bot_types.custom_bot')}
  173. </span>
  174. <span className="supplementary-desc mr-2">
  175. {t('admin:slack_integration.selecting_bot_types.without_proxy')}
  176. </span>
  177. {/* TODO: add an appropriate link by GW-5614 */}
  178. <i
  179. className={`fa fa-external-link btn-link ${currentBotType === 'custom-bot-without-proxy' ? 'bg-primary text-light' : ''}`}
  180. aria-hidden="true"
  181. >
  182. </i>
  183. </h3>
  184. <div className="card-body p-4">
  185. <p className="card-text">
  186. <div className="text-center">
  187. {showBotTypeLevel('for_intermediate')}
  188. </div>
  189. <div className="my-4">
  190. <div className="d-flex justify-content-between mb-2">
  191. {showBotTypeLabel('set_up')}
  192. {showBotTypeDiscription('normal')}
  193. </div>
  194. <div className="d-flex justify-content-between mb-2">
  195. {showBotTypeLabel('multiple_workspaces_integration')}
  196. {showBotTypeDiscription('impossible')}
  197. </div>
  198. <div className="d-flex justify-content-between">
  199. {showBotTypeLabel('security_control')}
  200. {showBotTypeDiscription('possible')}
  201. </div>
  202. </div>
  203. </p>
  204. </div>
  205. </div>
  206. <div
  207. className={`card admin-bot-card mx-3 rounded shadow ${currentBotType === 'custom-bot-with-proxy' ? 'border-primary' : ''}`}
  208. onClick={() => handleBotTypeSelect('custom-bot-with-proxy')}
  209. role="button"
  210. >
  211. <h3 className={`card-header mb-0 py-3 text-center text-nowrap ${currentBotType === 'custom-bot-with-proxy' ? 'bg-primary text-light' : ''}`}>
  212. <span className="mr-2">
  213. {t('admin:slack_integration.selecting_bot_types.custom_bot')}
  214. </span>
  215. <span className="supplementary-desc mr-2">
  216. {t('admin:slack_integration.selecting_bot_types.with_proxy')}
  217. </span>
  218. {/* TODO: add an appropriate link by GW-5614 */}
  219. <i
  220. className={`fa fa-external-link btn-link ${currentBotType === 'custom-bot-with-proxy' ? 'bg-primary text-light' : ''}`}
  221. aria-hidden="true"
  222. >
  223. </i>
  224. </h3>
  225. <div className="card-body p-4">
  226. <p className="card-text">
  227. <div className="text-center">
  228. {showBotTypeLevel('for_advanced')}
  229. </div>
  230. <div className="my-4">
  231. <div className="d-flex justify-content-between mb-2">
  232. {showBotTypeLabel('set_up')}
  233. {showBotTypeDiscription('hard')}
  234. </div>
  235. <div className="d-flex justify-content-between mb-2">
  236. {showBotTypeLabel('multiple_workspaces_integration')}
  237. {showBotTypeDiscription('possible')}
  238. </div>
  239. <div className="d-flex justify-content-between">
  240. {showBotTypeLabel('security_control')}
  241. {showBotTypeDiscription('impossible')}
  242. </div>
  243. </div>
  244. </p>
  245. </div>
  246. </div>
  247. </div>
  248. </div>
  249. </div>
  250. {settingsComponent}
  251. </>
  252. );
  253. };
  254. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  255. SlackIntegration.propTypes = {
  256. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  257. };
  258. export default SlackIntegrationWrapper;