notificationSettings.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import {Fragment} from 'react';
  2. import AlertLink from 'sentry/components/alertLink';
  3. import AsyncComponent from 'sentry/components/asyncComponent';
  4. import Form from 'sentry/components/forms/form';
  5. import JsonForm from 'sentry/components/forms/jsonForm';
  6. import FormModel from 'sentry/components/forms/model';
  7. import {FieldObject} from 'sentry/components/forms/type';
  8. import Link from 'sentry/components/links/link';
  9. import {IconMail} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import {Organization} from 'sentry/types';
  12. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  13. import withOrganizations from 'sentry/utils/withOrganizations';
  14. import {
  15. CONFIRMATION_MESSAGE,
  16. NOTIFICATION_SETTINGS_TYPES,
  17. NotificationSettingsObject,
  18. SELF_NOTIFICATION_SETTINGS_TYPES,
  19. } from 'sentry/views/settings/account/notifications/constants';
  20. import {NOTIFICATION_SETTING_FIELDS} from 'sentry/views/settings/account/notifications/fields2';
  21. import {
  22. decideDefault,
  23. getParentIds,
  24. getStateToPutForDefault,
  25. isSufficientlyComplex,
  26. mergeNotificationSettings,
  27. } from 'sentry/views/settings/account/notifications/utils';
  28. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  29. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  30. type Props = AsyncComponent['props'] & {
  31. organizations: Organization[];
  32. };
  33. type State = {
  34. legacyData: {[key: string]: string};
  35. notificationSettings: NotificationSettingsObject;
  36. } & AsyncComponent['state'];
  37. class NotificationSettings extends AsyncComponent<Props, State> {
  38. model = new FormModel();
  39. getDefaultState(): State {
  40. return {
  41. ...super.getDefaultState(),
  42. notificationSettings: {},
  43. legacyData: {},
  44. };
  45. }
  46. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  47. return [
  48. ['notificationSettings', `/users/me/notification-settings/`],
  49. ['legacyData', '/users/me/notifications/'],
  50. ];
  51. }
  52. componentDidMount() {
  53. // only tied to a user
  54. trackAdvancedAnalyticsEvent('notification_settings.index_page_viewed', {
  55. organization: null,
  56. });
  57. }
  58. getStateToPutForDefault = (
  59. changedData: {[key: string]: string},
  60. notificationType: string
  61. ) => {
  62. /**
  63. * Update the current providers' parent-independent notification settings
  64. * with the new value. If the new value is "never", then also update all
  65. * parent-specific notification settings to "default". If the previous value
  66. * was "never", then assume providerList should be "email" only.
  67. */
  68. const {notificationSettings} = this.state;
  69. const updatedNotificationSettings = getStateToPutForDefault(
  70. notificationType,
  71. notificationSettings,
  72. changedData,
  73. getParentIds(notificationType, notificationSettings)
  74. );
  75. this.setState({
  76. notificationSettings: mergeNotificationSettings(
  77. notificationSettings,
  78. updatedNotificationSettings
  79. ),
  80. });
  81. return updatedNotificationSettings;
  82. };
  83. get notificationSettingsType() {
  84. // filter out quotas if the feature flag isn't set
  85. const hasSlackOverage = this.props.organizations.some(org =>
  86. org.features?.includes('slack-overage-notifications')
  87. );
  88. const hasActiveRelease = this.props.organizations.some(org =>
  89. org.features?.includes('active-release-monitor-alpha')
  90. );
  91. return NOTIFICATION_SETTINGS_TYPES.filter(type => {
  92. if (type === 'quota' && !hasSlackOverage) {
  93. return false;
  94. }
  95. if (type === 'activeRelease' && !hasActiveRelease) {
  96. return false;
  97. }
  98. return true;
  99. });
  100. }
  101. getInitialData(): {[key: string]: string} {
  102. const {notificationSettings, legacyData} = this.state;
  103. const notificationsInitialData = Object.fromEntries(
  104. this.notificationSettingsType.map(notificationType => [
  105. notificationType,
  106. decideDefault(notificationType, notificationSettings),
  107. ])
  108. );
  109. const allInitialData = {
  110. ...notificationsInitialData,
  111. ...legacyData,
  112. };
  113. return allInitialData;
  114. }
  115. getFields(): FieldObject[] {
  116. const {notificationSettings} = this.state;
  117. const fields: FieldObject[] = [];
  118. const endOfFields: FieldObject[] = [];
  119. for (const notificationType of this.notificationSettingsType) {
  120. const field = Object.assign({}, NOTIFICATION_SETTING_FIELDS[notificationType], {
  121. getData: data => this.getStateToPutForDefault(data, notificationType),
  122. help: (
  123. <Fragment>
  124. <p>
  125. {NOTIFICATION_SETTING_FIELDS[notificationType].help}
  126. &nbsp;
  127. <Link
  128. data-test-id="fine-tuning"
  129. to={`/settings/account/notifications/${notificationType}`}
  130. >
  131. Fine tune
  132. </Link>
  133. </p>
  134. </Fragment>
  135. ),
  136. }) as any;
  137. if (
  138. isSufficientlyComplex(notificationType, notificationSettings) &&
  139. typeof field !== 'function'
  140. ) {
  141. field.confirm = {never: CONFIRMATION_MESSAGE};
  142. }
  143. if (field.type === 'blank') {
  144. endOfFields.push(field);
  145. } else {
  146. fields.push(field);
  147. }
  148. }
  149. const legacyField = SELF_NOTIFICATION_SETTINGS_TYPES.map(
  150. type => NOTIFICATION_SETTING_FIELDS[type] as FieldObject
  151. );
  152. fields.push(...legacyField);
  153. const allFields = [...fields, ...endOfFields];
  154. return allFields;
  155. }
  156. onFieldChange = (fieldName: string) => {
  157. if (SELF_NOTIFICATION_SETTINGS_TYPES.includes(fieldName)) {
  158. this.model.setFormOptions({apiEndpoint: '/users/me/notifications/'});
  159. } else {
  160. this.model.setFormOptions({apiEndpoint: '/users/me/notification-settings/'});
  161. }
  162. };
  163. renderBody() {
  164. return (
  165. <Fragment>
  166. <SettingsPageHeader title="Notifications" />
  167. <TextBlock>Personal notifications sent by email or an integration.</TextBlock>
  168. <Form
  169. model={this.model}
  170. saveOnBlur
  171. apiMethod="PUT"
  172. onFieldChange={this.onFieldChange}
  173. initialData={this.getInitialData()}
  174. >
  175. <JsonForm title={t('Notifications')} fields={this.getFields()} />
  176. </Form>
  177. <AlertLink to="/settings/account/emails" icon={<IconMail />}>
  178. {t('Looking to add or remove an email address? Use the emails panel.')}
  179. </AlertLink>
  180. </Fragment>
  181. );
  182. }
  183. }
  184. export default withOrganizations(NotificationSettings);