notificationSettings.tsx 6.5 KB

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