notificationSettings.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. checkFeatureFlag(flag: string) {
  88. return this.props.organizations.some(org => org.features?.includes(flag));
  89. }
  90. get notificationSettingsType() {
  91. // filter out notification settings if the feature flag isn't set
  92. return NOTIFICATION_SETTINGS_TYPES.filter(type => {
  93. const notificationFlag = NOTIFICATION_FEATURE_MAP[type];
  94. if (notificationFlag) {
  95. return this.checkFeatureFlag(notificationFlag);
  96. }
  97. return true;
  98. });
  99. }
  100. getInitialData(): {[key: string]: string} {
  101. const {notificationSettings, legacyData} = this.state;
  102. const notificationsInitialData = Object.fromEntries(
  103. this.notificationSettingsType.map(notificationType => [
  104. notificationType,
  105. decideDefault(notificationType, notificationSettings),
  106. ])
  107. );
  108. const allInitialData = {
  109. ...notificationsInitialData,
  110. ...legacyData,
  111. };
  112. return allInitialData;
  113. }
  114. getFields(): FieldObject[] {
  115. const {notificationSettings} = this.state;
  116. const fields: FieldObject[] = [];
  117. const endOfFields: FieldObject[] = [];
  118. for (const notificationType of this.notificationSettingsType) {
  119. const field = Object.assign({}, NOTIFICATION_SETTING_FIELDS[notificationType], {
  120. getData: data => this.getStateToPutForDefault(data, notificationType),
  121. help: (
  122. <Fragment>
  123. <p>
  124. {NOTIFICATION_SETTING_FIELDS[notificationType].help}
  125. &nbsp;
  126. <Link
  127. data-test-id="fine-tuning"
  128. to={`/settings/account/notifications/${NOTIFICATION_SETTINGS_PATHNAMES[notificationType]}`}
  129. >
  130. Fine tune
  131. </Link>
  132. </p>
  133. </Fragment>
  134. ),
  135. }) as any;
  136. if (
  137. isSufficientlyComplex(notificationType, notificationSettings) &&
  138. typeof field !== 'function'
  139. ) {
  140. field.confirm = {never: CONFIRMATION_MESSAGE};
  141. }
  142. if (field.type === 'blank') {
  143. endOfFields.push(field);
  144. } else {
  145. fields.push(field);
  146. }
  147. }
  148. const legacyField = SELF_NOTIFICATION_SETTINGS_TYPES.map(
  149. type => NOTIFICATION_SETTING_FIELDS[type] as FieldObject
  150. );
  151. fields.push(...legacyField);
  152. const allFields = [...fields, ...endOfFields];
  153. return allFields;
  154. }
  155. onFieldChange = (fieldName: string) => {
  156. if (SELF_NOTIFICATION_SETTINGS_TYPES.includes(fieldName)) {
  157. this.model.setFormOptions({apiEndpoint: '/users/me/notifications/'});
  158. } else {
  159. this.model.setFormOptions({apiEndpoint: '/users/me/notification-settings/'});
  160. }
  161. };
  162. renderBody() {
  163. return (
  164. <Fragment>
  165. <SentryDocumentTitle title={t('Notifications')} />
  166. <SettingsPageHeader title={t('Notifications')} />
  167. <TextBlock>
  168. {t('Personal notifications sent by email or an integration.')}
  169. </TextBlock>
  170. <Form
  171. model={this.model}
  172. saveOnBlur
  173. apiMethod="PUT"
  174. onFieldChange={this.onFieldChange}
  175. initialData={this.getInitialData()}
  176. >
  177. <JsonForm title={t('Notifications')} fields={this.getFields()} />
  178. </Form>
  179. <AlertLink to="/settings/account/emails" icon={<IconMail />}>
  180. {t('Looking to add or remove an email address? Use the emails panel.')}
  181. </AlertLink>
  182. </Fragment>
  183. );
  184. }
  185. }
  186. export default withOrganizations(NotificationSettings);