SlackIntegration.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. >
  130. <div>
  131. <h3 className={`card-header mb-0 py-3 text-center ${currentBotType === 'official-bot' ? 'bg-primary text-light' : ''}`}>
  132. <span className="mr-2">
  133. {t('admin:slack_integration.selecting_bot_types.official_bot')}
  134. </span>
  135. <span className="badge badge-info mr-2">
  136. {t('admin:slack_integration.selecting_bot_types.recommended')}
  137. </span>
  138. {/* TODO: add an appropriate link by GW-5614 */}
  139. <i className={`fa fa-external-link btn-link ${currentBotType === 'official-bot' ? 'bg-primary text-light' : ''}`} aria-hidden="true"></i>
  140. </h3>
  141. </div>
  142. <div className="card-body p-4">
  143. <p className="card-text">
  144. <div className="text-center">
  145. {showBotTypeLevel('for_beginners')}
  146. </div>
  147. <div className="my-4">
  148. <div className="d-flex justify-content-between mb-2">
  149. {showBotTypeLabel('set_up')}
  150. {showBotTypeDiscription('easy')}
  151. </div>
  152. <div className="d-flex justify-content-between mb-2">
  153. {showBotTypeLabel('multiple_workspaces_integration')}
  154. {showBotTypeDiscription('possible')}
  155. </div>
  156. <div className="d-flex justify-content-between">
  157. {showBotTypeLabel('security_control')}
  158. {showBotTypeDiscription('impossible')}
  159. </div>
  160. </div>
  161. </p>
  162. </div>
  163. </div>
  164. <div
  165. className={`card admin-bot-card mx-3 rounded shadow ${currentBotType === 'custom-bot-without-proxy' ? 'border-primary' : ''}`}
  166. onClick={() => handleBotTypeSelect('custom-bot-without-proxy')}
  167. >
  168. <h3 className={`card-header mb-0 py-3 text-center text-nowrap ${currentBotType === 'custom-bot-without-proxy' ? 'bg-primary text-light' : ''}`}>
  169. <span className="mr-2">
  170. {t('admin:slack_integration.selecting_bot_types.custom_bot')}
  171. </span>
  172. <span className="supplementary-desc mr-2">
  173. {t('admin:slack_integration.selecting_bot_types.without_proxy')}
  174. </span>
  175. {/* TODO: add an appropriate link by GW-5614 */}
  176. <i
  177. className={`fa fa-external-link btn-link ${currentBotType === 'custom-bot-without-proxy' ? 'bg-primary text-light' : ''}`}
  178. aria-hidden="true"
  179. >
  180. </i>
  181. </h3>
  182. <div className="card-body p-4">
  183. <p className="card-text">
  184. <div className="text-center">
  185. {showBotTypeLevel('for_intermediate')}
  186. </div>
  187. <div className="my-4">
  188. <div className="d-flex justify-content-between mb-2">
  189. {showBotTypeLabel('set_up')}
  190. {showBotTypeDiscription('normal')}
  191. </div>
  192. <div className="d-flex justify-content-between mb-2">
  193. {showBotTypeLabel('multiple_workspaces_integration')}
  194. {showBotTypeDiscription('impossible')}
  195. </div>
  196. <div className="d-flex justify-content-between">
  197. {showBotTypeLabel('security_control')}
  198. {showBotTypeDiscription('possible')}
  199. </div>
  200. </div>
  201. </p>
  202. </div>
  203. </div>
  204. <div
  205. className={`card admin-bot-card mx-3 rounded shadow ${currentBotType === 'custom-bot-with-proxy' ? 'border-primary' : ''}`}
  206. onClick={() => handleBotTypeSelect('custom-bot-with-proxy')}
  207. >
  208. <h3 className={`card-header mb-0 py-3 text-center text-nowrap ${currentBotType === 'custom-bot-with-proxy' ? 'bg-primary text-light' : ''}`}>
  209. <span className="mr-2">
  210. {t('admin:slack_integration.selecting_bot_types.custom_bot')}
  211. </span>
  212. <span className="supplementary-desc mr-2">
  213. {t('admin:slack_integration.selecting_bot_types.with_proxy')}
  214. </span>
  215. {/* TODO: add an appropriate link by GW-5614 */}
  216. <i
  217. className={`fa fa-external-link btn-link ${currentBotType === 'custom-bot-with-proxy' ? 'bg-primary text-light' : ''}`}
  218. aria-hidden="true"
  219. >
  220. </i>
  221. </h3>
  222. <div className="card-body p-4">
  223. <p className="card-text">
  224. <div className="text-center">
  225. {showBotTypeLevel('for_advanced')}
  226. </div>
  227. <div className="my-4">
  228. <div className="d-flex justify-content-between mb-2">
  229. {showBotTypeLabel('set_up')}
  230. {showBotTypeDiscription('hard')}
  231. </div>
  232. <div className="d-flex justify-content-between mb-2">
  233. {showBotTypeLabel('multiple_workspaces_integration')}
  234. {showBotTypeDiscription('possible')}
  235. </div>
  236. <div className="d-flex justify-content-between">
  237. {showBotTypeLabel('security_control')}
  238. {showBotTypeDiscription('impossible')}
  239. </div>
  240. </div>
  241. </p>
  242. </div>
  243. </div>
  244. </div>
  245. </div>
  246. </div>
  247. {settingsComponent}
  248. </>
  249. );
  250. };
  251. const SlackIntegrationWrapper = withUnstatedContainers(SlackIntegration, [AppContainer]);
  252. SlackIntegration.propTypes = {
  253. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  254. };
  255. export default SlackIntegrationWrapper;