notificationSettingsByProjects.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import AsyncComponent from 'sentry/components/asyncComponent';
  4. import EmptyMessage from 'sentry/components/emptyMessage';
  5. import Form from 'sentry/components/forms/form';
  6. import JsonForm from 'sentry/components/forms/jsonForm';
  7. import Pagination from 'sentry/components/pagination';
  8. import {t} from 'sentry/locale';
  9. import {Project} from 'sentry/types';
  10. import {sortProjects} from 'sentry/utils';
  11. import {
  12. MIN_PROJECTS_FOR_PAGINATION,
  13. MIN_PROJECTS_FOR_SEARCH,
  14. NotificationSettingsByProviderObject,
  15. NotificationSettingsObject,
  16. } from 'sentry/views/settings/account/notifications/constants';
  17. import {
  18. getParentData,
  19. getParentField,
  20. groupByOrganization,
  21. } from 'sentry/views/settings/account/notifications/utils';
  22. import {
  23. RenderSearch,
  24. SearchWrapper,
  25. } from 'sentry/views/settings/components/defaultSearchBar';
  26. type Props = {
  27. notificationSettings: NotificationSettingsObject;
  28. notificationType: string;
  29. onChange: (
  30. changedData: NotificationSettingsByProviderObject,
  31. parentId: string
  32. ) => NotificationSettingsObject;
  33. onSubmitSuccess: () => void;
  34. } & AsyncComponent['props'];
  35. type State = {
  36. projects: Project[];
  37. } & AsyncComponent['state'];
  38. class NotificationSettingsByProjects extends AsyncComponent<Props, State> {
  39. getDefaultState(): State {
  40. return {
  41. ...super.getDefaultState(),
  42. projects: [],
  43. };
  44. }
  45. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  46. return [['projects', '/projects/']];
  47. }
  48. /**
  49. * Check the notification settings for how many projects there are.
  50. */
  51. getProjectCount = (): number => {
  52. const {notificationType, notificationSettings} = this.props;
  53. return Object.values(notificationSettings[notificationType]?.project || {}).length;
  54. };
  55. /**
  56. * The UI expects projects to be grouped by organization but can also use
  57. * this function to make a single group with all organizations.
  58. */
  59. getGroupedProjects = (): {[key: string]: Project[]} => {
  60. const {projects: stateProjects} = this.state;
  61. return Object.fromEntries(
  62. Object.values(groupByOrganization(sortProjects(stateProjects))).map(
  63. ({organization, projects}) => [`${organization.name} Projects`, projects]
  64. )
  65. );
  66. };
  67. renderBody() {
  68. const {notificationType, notificationSettings, onChange, onSubmitSuccess} =
  69. this.props;
  70. const {projects, projectsPageLinks} = this.state;
  71. const canSearch = this.getProjectCount() >= MIN_PROJECTS_FOR_SEARCH;
  72. const shouldPaginate = projects.length >= MIN_PROJECTS_FOR_PAGINATION;
  73. const renderSearch: RenderSearch = ({defaultSearchBar}) => (
  74. <StyledSearchWrapper>{defaultSearchBar}</StyledSearchWrapper>
  75. );
  76. return (
  77. <Fragment>
  78. {canSearch &&
  79. this.renderSearchInput({
  80. stateKey: 'projects',
  81. url: '/projects/',
  82. placeholder: t('Search Projects'),
  83. children: renderSearch,
  84. })}
  85. <Form
  86. saveOnBlur
  87. apiMethod="PUT"
  88. apiEndpoint="/users/me/notification-settings/"
  89. initialData={getParentData(notificationType, notificationSettings, projects)}
  90. onSubmitSuccess={onSubmitSuccess}
  91. >
  92. {projects.length === 0 ? (
  93. <EmptyMessage>{t('No projects found')}</EmptyMessage>
  94. ) : (
  95. Object.entries(this.getGroupedProjects()).map(([groupTitle, parents]) => (
  96. <JsonForm
  97. collapsible
  98. key={groupTitle}
  99. title={groupTitle}
  100. fields={parents.map(parent =>
  101. getParentField(notificationType, notificationSettings, parent, onChange)
  102. )}
  103. />
  104. ))
  105. )}
  106. </Form>
  107. {canSearch && shouldPaginate && (
  108. <Pagination pageLinks={projectsPageLinks} {...this.props} />
  109. )}
  110. </Fragment>
  111. );
  112. }
  113. }
  114. export default NotificationSettingsByProjects;
  115. const StyledSearchWrapper = styled(SearchWrapper)`
  116. * {
  117. width: 100%;
  118. }
  119. `;