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