SlackIntegration.jsx 10 KB

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