notificationSettings.tsx 6.2 KB

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