CustomBotWithoutProxySettings.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // slackWSNameInWithoutProxy={props.slackWSNameInWithoutProxy}
  56. // isIntegrationSuccess={isIntegrationSuccess}
  57. workspaceNames={workspaceNames}
  58. />
  59. <h2 className="admin-setting-header">{t('admin:slack_integration.integration_procedure')}</h2>
  60. {(props.slackSigningSecret || props.slackBotToken) && (
  61. <button
  62. className="mx-3 pull-right btn text-danger border-danger"
  63. type="button"
  64. onClick={() => setIsDeleteConfirmModalShown(true)}
  65. >{t('admin:slack_integration.reset')}
  66. </button>
  67. )}
  68. <div className="my-5 mx-3">
  69. <CustomBotWithoutProxySettingsAccordion
  70. {...props}
  71. activeStep={botInstallationStep.CREATE_BOT}
  72. connectionMessage={connectionMessage}
  73. connectionErrorCode={connectionErrorCode}
  74. isIntegrationSuccess={isIntegrationSuccess}
  75. testChannel={testChannel}
  76. onTestFormSubmitted={testConnection}
  77. inputTestChannelHandler={inputTestChannelHandler}
  78. />
  79. </div>
  80. <DeleteSlackBotSettingsModal
  81. isResetAll={false}
  82. isOpen={isDeleteConfirmModalShown}
  83. onClose={() => setIsDeleteConfirmModalShown(false)}
  84. onClickDeleteButton={resetSettings}
  85. />
  86. </>
  87. );
  88. };
  89. const CustomBotWithoutProxySettingsWrapper = withUnstatedContainers(CustomBotWithoutProxySettings, [AppContainer, AdminAppContainer]);
  90. CustomBotWithoutProxySettings.propTypes = {
  91. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  92. adminAppContainer: PropTypes.instanceOf(AdminAppContainer).isRequired,
  93. slackSigningSecret: PropTypes.string,
  94. slackSigningSecretEnv: PropTypes.string,
  95. slackBotToken: PropTypes.string,
  96. slackBotTokenEnv: PropTypes.string,
  97. isRgisterSlackCredentials: PropTypes.bool,
  98. isIntegrationSuccess: PropTypes.bool,
  99. slackWSNameInWithoutProxy: PropTypes.string,
  100. onResetSettings: PropTypes.func,
  101. connectionStatuses: PropTypes.object.isRequired,
  102. };
  103. export default CustomBotWithoutProxySettingsWrapper;