NotificationSetting.jsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React, { useMemo } from 'react';
  2. import PropTypes from 'prop-types';
  3. import loggerFactory from '@alias/logger';
  4. import { withUnstatedContainers } from '../../UnstatedUtils';
  5. import { toastError } from '../../../util/apiNotification';
  6. import toArrayIfNot from '../../../../../lib/util/toArrayIfNot';
  7. import { withLoadingSppiner } from '../../SuspenseUtils';
  8. import AdminNotificationContainer from '../../../services/AdminNotificationContainer';
  9. import CustomNavigation from '../../CustomNavigation';
  10. import SlackAppConfiguration from './SlackAppConfiguration';
  11. import UserTriggerNotification from './UserTriggerNotification';
  12. import GlobalNotification from './GlobalNotification';
  13. const logger = loggerFactory('growi:NotificationSetting');
  14. let retrieveErrors = null;
  15. function NotificationSetting(props) {
  16. const { adminNotificationContainer } = props;
  17. if (adminNotificationContainer.state.webhookUrl === adminNotificationContainer.dummyWebhookUrl) {
  18. throw (async() => {
  19. try {
  20. await adminNotificationContainer.retrieveNotificationData();
  21. }
  22. catch (err) {
  23. const errs = toArrayIfNot(err);
  24. toastError(errs);
  25. logger.error(errs);
  26. retrieveErrors = errs;
  27. adminNotificationContainer.setState({ webhookUrl: adminNotificationContainer.dummyWebhookUrlForError });
  28. }
  29. })();
  30. }
  31. if (adminNotificationContainer.state.webhookUrl === adminNotificationContainer.dummyWebhookUrlForError) {
  32. throw new Error(`${retrieveErrors.length} errors occured`);
  33. }
  34. const navTabMapping = useMemo(() => {
  35. return {
  36. slack_configuration: {
  37. Icon: () => <i className="icon-settings" />,
  38. Content: SlackAppConfiguration,
  39. i18n: 'Slack configuration',
  40. index: 0,
  41. },
  42. user_trigger_notification: {
  43. Icon: () => <i className="icon-settings" />,
  44. Content: UserTriggerNotification,
  45. i18n: 'User trigger notification',
  46. index: 1,
  47. },
  48. global_notification: {
  49. Icon: () => <i className="icon-settings" />,
  50. Content: GlobalNotification,
  51. i18n: 'Global notification',
  52. index: 2,
  53. },
  54. };
  55. }, []);
  56. return <CustomNavigation navTabMapping={navTabMapping} />;
  57. }
  58. const NotificationSettingWithUnstatedContainer = withUnstatedContainers(withLoadingSppiner(NotificationSetting), [AdminNotificationContainer]);
  59. NotificationSetting.propTypes = {
  60. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  61. };
  62. export default NotificationSettingWithUnstatedContainer;