CustomBotWithoutProxySettings.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 CustomBotWithoutProxyIntegrationCard from './CustomBotWithoutProxyIntegrationCard';
  9. import DeleteSlackBotSettingsModal from './DeleteSlackBotSettingsModal';
  10. const CustomBotWithoutProxySettings = (props) => {
  11. const { appContainer, onResetSettings, connectionStatuses } = props;
  12. const { t } = useTranslation();
  13. const [siteName, setSiteName] = useState('');
  14. const [isDeleteConfirmModalShown, setIsDeleteConfirmModalShown] = useState(false);
  15. const [isIntegrationSuccess, setIsIntegrationSuccess] = useState(false);
  16. const [connectionMessage, setConnectionMessage] = useState('');
  17. const [connectionErrorCode, setConnectionErrorCode] = useState(null);
  18. const [testChannel, setTestChannel] = useState('');
  19. const workspaceNameObjects = Object.values(connectionStatuses);
  20. const workspaceNames = workspaceNameObjects.map((w) => {
  21. return w.workspaceName;
  22. });
  23. const resetSettings = async() => {
  24. if (onResetSettings == null) {
  25. return;
  26. }
  27. onResetSettings();
  28. };
  29. const testConnection = async() => {
  30. setConnectionErrorCode(null);
  31. setConnectionMessage(null);
  32. try {
  33. await appContainer.apiv3.post('/slack-integration-settings/without-proxy/test', { channel: testChannel });
  34. setConnectionMessage('Send the message to slack work space.');
  35. setIsIntegrationSuccess(true);
  36. }
  37. catch (err) {
  38. setConnectionErrorCode(err[0].code);
  39. setConnectionMessage(err[0].message);
  40. setIsIntegrationSuccess(false);
  41. }
  42. };
  43. const inputTestChannelHandler = (channel) => {
  44. setTestChannel(channel);
  45. };
  46. useEffect(() => {
  47. const siteName = appContainer.config.crowi.title;
  48. setSiteName(siteName);
  49. }, [appContainer]);
  50. return (
  51. <>
  52. <h2 className="admin-setting-header">{t('admin:slack_integration.custom_bot_without_proxy_integration')}</h2>
  53. <CustomBotWithoutProxyIntegrationCard
  54. siteName={siteName}
  55. workspaceNames={workspaceNames}
  56. />
  57. <h2 className="admin-setting-header">{t('admin:slack_integration.integration_procedure')}</h2>
  58. {(props.slackSigningSecret || props.slackBotToken) && (
  59. <button
  60. className="mx-3 pull-right btn text-danger border-danger"
  61. type="button"
  62. onClick={() => setIsDeleteConfirmModalShown(true)}
  63. >{t('admin:slack_integration.reset')}
  64. </button>
  65. )}
  66. <div className="my-5 mx-3">
  67. <CustomBotWithoutProxySettingsAccordion
  68. {...props}
  69. activeStep={botInstallationStep.CREATE_BOT}
  70. connectionMessage={connectionMessage}
  71. connectionErrorCode={connectionErrorCode}
  72. isIntegrationSuccess={isIntegrationSuccess}
  73. testChannel={testChannel}
  74. onTestFormSubmitted={testConnection}
  75. inputTestChannelHandler={inputTestChannelHandler}
  76. />
  77. </div>
  78. <DeleteSlackBotSettingsModal
  79. isResetAll={false}
  80. isOpen={isDeleteConfirmModalShown}
  81. onClose={() => setIsDeleteConfirmModalShown(false)}
  82. onClickDeleteButton={resetSettings}
  83. />
  84. </>
  85. );
  86. };
  87. const CustomBotWithoutProxySettingsWrapper = withUnstatedContainers(CustomBotWithoutProxySettings, [AppContainer, AdminAppContainer]);
  88. CustomBotWithoutProxySettings.propTypes = {
  89. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  90. adminAppContainer: PropTypes.instanceOf(AdminAppContainer).isRequired,
  91. slackSigningSecret: PropTypes.string,
  92. slackSigningSecretEnv: PropTypes.string,
  93. slackBotToken: PropTypes.string,
  94. slackBotTokenEnv: PropTypes.string,
  95. isRgisterSlackCredentials: PropTypes.bool,
  96. isIntegrationSuccess: PropTypes.bool,
  97. slackWSNameInWithoutProxy: PropTypes.string,
  98. onResetSettings: PropTypes.func,
  99. connectionStatuses: PropTypes.object.isRequired,
  100. };
  101. export default CustomBotWithoutProxySettingsWrapper;