settings.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import AlertLink from 'sentry/components/alertLink';
  4. import {Button} from 'sentry/components/button';
  5. import Form from 'sentry/components/forms/form';
  6. import JsonForm from 'sentry/components/forms/jsonForm';
  7. import PanelAlert from 'sentry/components/panels/panelAlert';
  8. import PluginList from 'sentry/components/pluginList';
  9. import {fields} from 'sentry/data/forms/projectAlerts';
  10. import {IconMail} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import {Organization, Plugin, Project} from 'sentry/types';
  13. import routeTitleGen from 'sentry/utils/routeTitle';
  14. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  15. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  16. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  17. type RouteParams = {projectId: string};
  18. type Props = RouteComponentProps<RouteParams, {}> &
  19. DeprecatedAsyncView['props'] & {
  20. canEditRule: boolean;
  21. organization: Organization;
  22. };
  23. type State = DeprecatedAsyncView['state'] & {
  24. pluginList: Array<Plugin> | null;
  25. project: Project | null;
  26. };
  27. class Settings extends DeprecatedAsyncView<Props, State> {
  28. getDefaultState() {
  29. return {
  30. ...super.getDefaultState(),
  31. project: null,
  32. pluginList: [],
  33. };
  34. }
  35. getProjectEndpoint() {
  36. const {organization, params} = this.props;
  37. return `/projects/${organization.slug}/${params.projectId}/`;
  38. }
  39. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  40. const {organization, params} = this.props;
  41. const projectEndpoint = this.getProjectEndpoint();
  42. return [
  43. ['project', projectEndpoint],
  44. ['pluginList', `/projects/${organization.slug}/${params.projectId}/plugins/`],
  45. ];
  46. }
  47. handleEnablePlugin = (plugin: Plugin) => {
  48. this.setState(prevState => ({
  49. pluginList: (prevState.pluginList ?? []).map(p => {
  50. if (p.id !== plugin.id) {
  51. return p;
  52. }
  53. return {
  54. ...plugin,
  55. enabled: true,
  56. };
  57. }),
  58. }));
  59. };
  60. handleDisablePlugin = (plugin: Plugin) => {
  61. this.setState(prevState => ({
  62. pluginList: (prevState.pluginList ?? []).map(p => {
  63. if (p.id !== plugin.id) {
  64. return p;
  65. }
  66. return {
  67. ...plugin,
  68. enabled: false,
  69. };
  70. }),
  71. }));
  72. };
  73. getTitle() {
  74. const {projectId} = this.props.params;
  75. return routeTitleGen(t('Alerts Settings'), projectId, false);
  76. }
  77. renderBody() {
  78. const {canEditRule, organization} = this.props;
  79. const {project, pluginList} = this.state;
  80. if (!project) {
  81. return null;
  82. }
  83. const projectEndpoint = this.getProjectEndpoint();
  84. return (
  85. <Fragment>
  86. <SettingsPageHeader
  87. title={t('Alerts Settings')}
  88. action={
  89. <Button
  90. to={{
  91. pathname: `/organizations/${organization.slug}/alerts/rules/`,
  92. query: {project: project.id},
  93. }}
  94. size="sm"
  95. >
  96. {t('View Alert Rules')}
  97. </Button>
  98. }
  99. />
  100. <PermissionAlert project={project} />
  101. <AlertLink to="/settings/account/notifications/" icon={<IconMail />}>
  102. {t(
  103. 'Looking to fine-tune your personal notification preferences? Visit your Account Settings'
  104. )}
  105. </AlertLink>
  106. <Form
  107. saveOnBlur
  108. allowUndo
  109. initialData={{
  110. subjectTemplate: project.subjectTemplate,
  111. digestsMinDelay: project.digestsMinDelay,
  112. digestsMaxDelay: project.digestsMaxDelay,
  113. }}
  114. apiMethod="PUT"
  115. apiEndpoint={projectEndpoint}
  116. >
  117. <JsonForm
  118. disabled={!canEditRule}
  119. title={t('Email Settings')}
  120. fields={[fields.subjectTemplate]}
  121. />
  122. <JsonForm
  123. title={t('Digests')}
  124. disabled={!canEditRule}
  125. fields={[fields.digestsMinDelay, fields.digestsMaxDelay]}
  126. renderHeader={() => (
  127. <PanelAlert type="info">
  128. {t(
  129. 'Sentry will automatically digest alerts sent by some services to avoid flooding your inbox with individual issue notifications. To control how frequently notifications are delivered, use the sliders below.'
  130. )}
  131. </PanelAlert>
  132. )}
  133. />
  134. </Form>
  135. {canEditRule && (
  136. <PluginList
  137. organization={organization}
  138. project={project}
  139. pluginList={(pluginList ?? []).filter(
  140. p => p.type === 'notification' && p.hasConfiguration
  141. )}
  142. onEnablePlugin={this.handleEnablePlugin}
  143. onDisablePlugin={this.handleDisablePlugin}
  144. />
  145. )}
  146. </Fragment>
  147. );
  148. }
  149. }
  150. export default Settings;