SlackAppConfiguration.jsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import loggerFactory from '@alias/logger';
  5. import { createSubscribedElement } from '../../UnstatedUtils';
  6. import { toastSuccess, toastError } from '../../../util/apiNotification';
  7. import AppContainer from '../../../services/AppContainer';
  8. import AdminNotificationContainer from '../../../services/AdminNotificationContainer';
  9. import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow';
  10. const logger = loggerFactory('growi:slackAppConfiguration');
  11. class SlackAppConfiguration extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.onClickSubmit = this.onClickSubmit.bind(this);
  15. }
  16. async onClickSubmit() {
  17. const { t, adminNotificationContainer } = this.props;
  18. try {
  19. await adminNotificationContainer.updateSlackAppConfiguration();
  20. toastSuccess(t('notification_setting.updated_slackApp'));
  21. }
  22. catch (err) {
  23. toastError(err);
  24. logger.error(err);
  25. }
  26. }
  27. render() {
  28. const { t, adminNotificationContainer } = this.props;
  29. return (
  30. <React.Fragment>
  31. <div className="row mb-5">
  32. <div className="col-xs-6 text-left">
  33. <div className="my-0 btn-group">
  34. <div className="dropdown">
  35. <button className="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  36. <span className="pull-left">Slack {adminNotificationContainer.state.selectSlackOption} </span>
  37. <span className="bs-caret pull-right">
  38. <span className="caret" />
  39. </span>
  40. </button>
  41. {/* TODO adjust dropdown after BS4 */}
  42. <ul className="dropdown-menu" role="menu">
  43. <li type="button" onClick={() => adminNotificationContainer.switchSlackOption('Incoming Webhooks')}>
  44. <a role="menuitem">Slack Incoming Webhooks</a>
  45. </li>
  46. <li type="button" onClick={() => adminNotificationContainer.switchSlackOption('App')}>
  47. <a role="menuitem">Slack App</a>
  48. </li>
  49. </ul>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. {adminNotificationContainer.state.selectSlackOption === 'Incoming Webhooks' ? (
  55. <React.Fragment>
  56. <h2 className="border-bottom mb-5">{t('notification_setting.slack_incoming_configuration')}</h2>
  57. <div className="row mb-5">
  58. <label className="col-xs-3 text-right">Webhook URL</label>
  59. <div className="col-xs-6">
  60. <input
  61. className="form-control"
  62. type="text"
  63. defaultValue={adminNotificationContainer.state.webhookUrl || ''}
  64. onChange={e => adminNotificationContainer.changeWebhookUrl(e.target.value)}
  65. />
  66. </div>
  67. </div>
  68. <div className="row mb-5">
  69. <div className="col-xs-offset-3 col-xs-6 text-left">
  70. <div className="checkbox checkbox-success">
  71. <input
  72. id="cbPrioritizeIWH"
  73. type="checkbox"
  74. checked={adminNotificationContainer.state.isIncomingWebhookPrioritized || false}
  75. onChange={() => { adminNotificationContainer.switchIsIncomingWebhookPrioritized() }}
  76. />
  77. <label htmlFor="cbPrioritizeIWH">
  78. {t('notification_setting.prioritize_webhook')}
  79. </label>
  80. </div>
  81. <p className="help-block">
  82. {t('notification_setting.prioritize_webhook_desc')}
  83. </p>
  84. </div>
  85. </div>
  86. </React.Fragment>
  87. )
  88. : (
  89. <React.Fragment>
  90. <h2 className="border-bottom mb-5">{t('notification_setting.slack_app_configuration')}</h2>
  91. <div className="well">
  92. <i className="icon-fw icon-exclamation text-danger"></i><span className="text-danger">NOT RECOMMENDED</span>
  93. <br /><br />
  94. {/* eslint-disable-next-line react/no-danger */}
  95. <span dangerouslySetInnerHTML={{ __html: t('notification_setting.slack_app_configuration_desc') }} />
  96. <br /><br />
  97. <a
  98. href="#slack-incoming-webhooks"
  99. data-toggle="tab"
  100. onClick={() => adminNotificationContainer.switchSlackOption('Incoming Webhooks')}
  101. >
  102. {t('notification_setting.use_instead')}
  103. </a>{' '}
  104. </div>
  105. <div className="row mb-5">
  106. <label className="col-xs-3 text-right">OAuth access token</label>
  107. <div className="col-xs-6">
  108. <input
  109. className="form-control"
  110. type="text"
  111. defaultValue={adminNotificationContainer.state.slackToken || ''}
  112. onChange={e => adminNotificationContainer.changeSlackToken(e.target.value)}
  113. />
  114. </div>
  115. </div>
  116. </React.Fragment>
  117. )
  118. }
  119. <AdminUpdateButtonRow
  120. onClick={this.onClickSubmit}
  121. disabled={adminNotificationContainer.state.retrieveError != null}
  122. />
  123. <hr />
  124. <h3>
  125. <i className="icon-question" aria-hidden="true"></i>{' '}
  126. <a href="#collapseHelpForIwh" data-toggle="collapse">{t('notification_setting.how_to.header')}</a>
  127. </h3>
  128. <ol id="collapseHelpForIwh" className="collapse">
  129. <li>
  130. {t('notification_setting.how_to.workspace')}
  131. <ol>
  132. {/* eslint-disable-next-line react/no-danger */}
  133. <li dangerouslySetInnerHTML={{ __html: t('notification_setting.how_to.workspace_desc1') }} />
  134. <li>{t('notification_setting.how_to.workspace_desc2')}</li>
  135. <li>{t('notification_setting.how_to.workspace_desc3')}</li>
  136. </ol>
  137. </li>
  138. <li>
  139. {t('notification_setting.how_to.at_growi')}
  140. <ol>
  141. {/* eslint-disable-next-line react/no-danger */}
  142. <li dangerouslySetInnerHTML={{ __html: t('notification_setting.how_to.at_growi_desc') }} />
  143. </ol>
  144. </li>
  145. </ol>
  146. </React.Fragment>
  147. );
  148. }
  149. }
  150. const SlackAppConfigurationWrapper = (props) => {
  151. return createSubscribedElement(SlackAppConfiguration, props, [AppContainer, AdminNotificationContainer]);
  152. };
  153. SlackAppConfiguration.propTypes = {
  154. t: PropTypes.func.isRequired, // i18next
  155. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  156. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  157. };
  158. export default withTranslation()(SlackAppConfigurationWrapper);