CustomBotWithoutProxySecretTokenSection.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React, { useState, useEffect } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { useTranslation } from 'react-i18next';
  4. import AppContainer from '~/client/services/AppContainer';
  5. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  6. import { apiv3Put } from '~/client/util/apiv3-client';
  7. import { withUnstatedContainers } from '../../UnstatedUtils';
  8. import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow';
  9. const CustomBotWithoutProxySecretTokenSection = (props) => {
  10. const {
  11. appContainer, slackSigningSecret, slackBotToken, slackSigningSecretEnv, slackBotTokenEnv, onUpdatedSecretToken,
  12. } = props;
  13. const { t } = useTranslation();
  14. const [inputSigningSecret, setInputSigningSecret] = useState(slackSigningSecret || '');
  15. const [inputBotToken, setInputBotToken] = useState(slackBotToken || '');
  16. // update states when props are updated
  17. useEffect(() => {
  18. setInputSigningSecret(slackSigningSecret || '');
  19. }, [slackSigningSecret]);
  20. useEffect(() => {
  21. setInputBotToken(slackBotToken || '');
  22. }, [slackBotToken]);
  23. const updatedSecretToken = async() => {
  24. try {
  25. await apiv3Put('/slack-integration-settings/without-proxy/update-settings', {
  26. slackSigningSecret: inputSigningSecret,
  27. slackBotToken: inputBotToken,
  28. });
  29. if (onUpdatedSecretToken != null) {
  30. onUpdatedSecretToken(inputSigningSecret, inputBotToken);
  31. }
  32. toastSuccess(t('toaster.update_successed', { target: t('admin:slack_integration.custom_bot_without_proxy_settings') }));
  33. }
  34. catch (err) {
  35. toastError(err);
  36. }
  37. };
  38. return (
  39. <div className="w-75 mx-auto">
  40. <h3>Signing Secret</h3>
  41. <div className="row">
  42. <div className="col-sm">
  43. <p>Database</p>
  44. <input
  45. className="form-control"
  46. type="text"
  47. value={inputSigningSecret}
  48. onChange={e => setInputSigningSecret(e.target.value)}
  49. />
  50. </div>
  51. <div className="col-sm">
  52. <p>Environment variables</p>
  53. <input
  54. className="form-control"
  55. type="text"
  56. defaultValue={slackSigningSecretEnv}
  57. readOnly
  58. />
  59. <p className="form-text text-muted">
  60. {/* eslint-disable-next-line max-len, react/no-danger */}
  61. <small dangerouslySetInnerHTML={{ __html: t('admin:slack_integration.use_env_var_if_empty', { variable: 'SLACKBOT_WITHOUT_PROXY_SIGNING_SECRET' }) }} />
  62. </p>
  63. </div>
  64. </div>
  65. <h3>Bot User OAuth Token</h3>
  66. <div className="row">
  67. <div className="col-sm">
  68. <p>Database</p>
  69. <input
  70. className="form-control"
  71. type="text"
  72. value={inputBotToken}
  73. onChange={e => setInputBotToken(e.target.value)}
  74. />
  75. </div>
  76. <div className="col-sm">
  77. <p>Environment variables</p>
  78. <input
  79. className="form-control"
  80. type="text"
  81. defaultValue={slackBotTokenEnv}
  82. readOnly
  83. />
  84. <p className="form-text text-muted">
  85. {/* eslint-disable-next-line react/no-danger */}
  86. <small dangerouslySetInnerHTML={{ __html: t('admin:slack_integration.use_env_var_if_empty', { variable: 'SLACKBOT_WITHOUT_PROXY_BOT_TOKEN' }) }} />
  87. </p>
  88. </div>
  89. </div>
  90. <AdminUpdateButtonRow onClick={updatedSecretToken} disabled={false} />
  91. </div>
  92. );
  93. };
  94. const CustomBotWithoutProxySecretTokenSectionWrapper = withUnstatedContainers(CustomBotWithoutProxySecretTokenSection, [AppContainer]);
  95. CustomBotWithoutProxySecretTokenSection.propTypes = {
  96. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  97. onUpdatedSecretToken: PropTypes.func,
  98. slackSigningSecret: PropTypes.string,
  99. slackSigningSecretEnv: PropTypes.string,
  100. slackBotToken: PropTypes.string,
  101. slackBotTokenEnv: PropTypes.string,
  102. };
  103. export default CustomBotWithoutProxySecretTokenSectionWrapper;