CustomBotWithoutProxySettings.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React, { useState, useEffect } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import PropTypes from 'prop-types';
  4. import AppContainer from '../../../services/AppContainer';
  5. import AdminAppContainer from '../../../services/AdminAppContainer';
  6. import { withUnstatedContainers } from '../../UnstatedUtils';
  7. import CustomBotWithoutProxySettingsAccordion, { botInstallationStep } from './CustomBotWithoutProxySettingsAccordion';
  8. import CustomBotWithoutProxyConnectionStatus from './CustomBotWithoutProxyConnectionStatus';
  9. const CustomBotWithoutProxySettings = (props) => {
  10. const { appContainer, connectionStatuses, onTestConnectionInvoked } = props;
  11. const { t } = useTranslation();
  12. const [siteName, setSiteName] = useState('');
  13. const [isIntegrationSuccess, setIsIntegrationSuccess] = useState(false);
  14. const [connectionMessage, setConnectionMessage] = useState(null);
  15. const [testChannel, setTestChannel] = useState('');
  16. const addLogs = (log) => {
  17. const newLog = `${log.code} ${log.message}\n\n`;
  18. if (connectionMessage == null) {
  19. return setConnectionMessage(newLog);
  20. }
  21. setConnectionMessage(`${newLog}${connectionMessage}`);
  22. };
  23. const testConnection = async() => {
  24. try {
  25. await appContainer.apiv3.post('/slack-integration-settings/without-proxy/test', { channel: testChannel });
  26. setConnectionMessage('');
  27. setIsIntegrationSuccess(true);
  28. if (onTestConnectionInvoked != null) {
  29. onTestConnectionInvoked();
  30. }
  31. }
  32. catch (err) {
  33. addLogs(err[0]);
  34. setIsIntegrationSuccess(false);
  35. }
  36. };
  37. const inputTestChannelHandler = (channel) => {
  38. setTestChannel(channel);
  39. };
  40. useEffect(() => {
  41. const siteName = appContainer.config.crowi.title;
  42. setSiteName(siteName);
  43. }, [appContainer]);
  44. const workspaceName = connectionStatuses[props.slackBotToken]?.workspaceName;
  45. return (
  46. <>
  47. <h2 className="admin-setting-header">{t('admin:slack_integration.custom_bot_without_proxy_integration')}
  48. {/* TODO: add an appropriate links by GW-5614 */}
  49. <i className="fa fa-external-link btn-link ml-2" aria-hidden="true"></i>
  50. </h2>
  51. <CustomBotWithoutProxyConnectionStatus
  52. siteName={siteName}
  53. connectionStatuses={connectionStatuses}
  54. />
  55. <h2 className="admin-setting-header">{t('admin:slack_integration.integration_procedure')}</h2>
  56. <div className="px-3">
  57. <div className="my-3 d-flex align-items-center justify-content-between">
  58. <h2 id={props.slackBotToken || 'settings-accordions'}>
  59. {(workspaceName != null) ? `${workspaceName} Work Space` : 'Settings'}
  60. </h2>
  61. </div>
  62. <CustomBotWithoutProxySettingsAccordion
  63. {...props}
  64. activeStep={botInstallationStep.CREATE_BOT}
  65. connectionMessage={connectionMessage}
  66. isIntegrationSuccess={isIntegrationSuccess}
  67. testChannel={testChannel}
  68. onTestFormSubmitted={testConnection}
  69. inputTestChannelHandler={inputTestChannelHandler}
  70. />
  71. </div>
  72. </>
  73. );
  74. };
  75. const CustomBotWithoutProxySettingsWrapper = withUnstatedContainers(CustomBotWithoutProxySettings, [AppContainer, AdminAppContainer]);
  76. CustomBotWithoutProxySettings.propTypes = {
  77. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  78. adminAppContainer: PropTypes.instanceOf(AdminAppContainer).isRequired,
  79. slackSigningSecret: PropTypes.string,
  80. slackSigningSecretEnv: PropTypes.string,
  81. slackBotToken: PropTypes.string,
  82. slackBotTokenEnv: PropTypes.string,
  83. isIntegrationSuccess: PropTypes.bool,
  84. connectionStatuses: PropTypes.object.isRequired,
  85. onTestConnectionInvoked: PropTypes.func,
  86. };
  87. export default CustomBotWithoutProxySettingsWrapper;