settings.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import AlertLink from 'sentry/components/alertLink';
  4. import {LinkButton} from 'sentry/components/button';
  5. import Form from 'sentry/components/forms/form';
  6. import JsonForm from 'sentry/components/forms/jsonForm';
  7. import LoadingError from 'sentry/components/loadingError';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import PanelAlert from 'sentry/components/panels/panelAlert';
  10. import PluginList from 'sentry/components/pluginList';
  11. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  12. import {fields} from 'sentry/data/forms/projectAlerts';
  13. import {IconMail} from 'sentry/icons';
  14. import {t} from 'sentry/locale';
  15. import type {Plugin, Project} from 'sentry/types';
  16. import {
  17. ApiQueryKey,
  18. setApiQueryData,
  19. useApiQuery,
  20. useQueryClient,
  21. } from 'sentry/utils/queryClient';
  22. import routeTitleGen from 'sentry/utils/routeTitle';
  23. import useOrganization from 'sentry/utils/useOrganization';
  24. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  25. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  26. interface ProjectAlertSettingsProps extends RouteComponentProps<{projectId: string}, {}> {
  27. canEditRule: boolean;
  28. }
  29. function makeFetchProjectPluginsQueryKey(
  30. organizationSlug: string,
  31. projectSlug: string
  32. ): ApiQueryKey {
  33. return [`/projects/${organizationSlug}/${projectSlug}/plugins/`];
  34. }
  35. function ProjectAlertSettings({canEditRule, params}: ProjectAlertSettingsProps) {
  36. const organization = useOrganization();
  37. const queryClient = useQueryClient();
  38. const projectSlug = params.projectId;
  39. const {
  40. data: project,
  41. isLoading: isProjectLoading,
  42. isError: isProjectError,
  43. refetch: refetchProject,
  44. } = useApiQuery<Project>([`/projects/${organization.slug}/${projectSlug}/`], {
  45. staleTime: 0,
  46. cacheTime: 0,
  47. });
  48. const {
  49. data: pluginList = [],
  50. isLoading: isPluginListLoading,
  51. isError: isPluginListError,
  52. refetch: refetchPluginList,
  53. } = useApiQuery<Plugin[]>(
  54. makeFetchProjectPluginsQueryKey(organization.slug, projectSlug),
  55. {staleTime: 0, cacheTime: 0}
  56. );
  57. if ((!isProjectLoading && !project) || isPluginListError || isProjectError) {
  58. return (
  59. <LoadingError
  60. onRetry={() => {
  61. isProjectError && refetchProject();
  62. isPluginListError && refetchPluginList();
  63. }}
  64. />
  65. );
  66. }
  67. const updatePlugin = (plugin: Plugin, enabled: boolean) => {
  68. setApiQueryData<Plugin[]>(
  69. queryClient,
  70. makeFetchProjectPluginsQueryKey(organization.slug, projectSlug),
  71. oldState =>
  72. oldState.map(p => {
  73. if (p.id !== plugin.id) {
  74. return p;
  75. }
  76. return {
  77. ...plugin,
  78. enabled,
  79. };
  80. })
  81. );
  82. };
  83. const handleEnablePlugin = (plugin: Plugin) => {
  84. updatePlugin(plugin, true);
  85. };
  86. const handleDisablePlugin = (plugin: Plugin) => {
  87. updatePlugin(plugin, false);
  88. };
  89. return (
  90. <Fragment>
  91. <SentryDocumentTitle
  92. title={routeTitleGen(t('Alerts Settings'), projectSlug, false)}
  93. />
  94. <SettingsPageHeader
  95. title={t('Alerts Settings')}
  96. action={
  97. <LinkButton
  98. to={{
  99. pathname: `/organizations/${organization.slug}/alerts/rules/`,
  100. query: {project: project?.id},
  101. }}
  102. size="sm"
  103. >
  104. {t('View Alert Rules')}
  105. </LinkButton>
  106. }
  107. />
  108. <PermissionAlert project={project} />
  109. <AlertLink to="/settings/account/notifications/" icon={<IconMail />}>
  110. {t(
  111. 'Looking to fine-tune your personal notification preferences? Visit your Account Settings'
  112. )}
  113. </AlertLink>
  114. {isProjectLoading || isPluginListLoading ? (
  115. <LoadingIndicator />
  116. ) : (
  117. <Fragment>
  118. <Form
  119. saveOnBlur
  120. allowUndo
  121. initialData={{
  122. subjectTemplate: project.subjectTemplate,
  123. digestsMinDelay: project.digestsMinDelay,
  124. digestsMaxDelay: project.digestsMaxDelay,
  125. }}
  126. apiMethod="PUT"
  127. apiEndpoint={`/projects/${organization.slug}/${project.slug}/`}
  128. >
  129. <JsonForm
  130. disabled={!canEditRule}
  131. title={t('Email Settings')}
  132. fields={[fields.subjectTemplate]}
  133. />
  134. <JsonForm
  135. title={t('Digests')}
  136. disabled={!canEditRule}
  137. fields={[fields.digestsMinDelay, fields.digestsMaxDelay]}
  138. renderHeader={() => (
  139. <PanelAlert type="info">
  140. {t(
  141. '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.'
  142. )}
  143. </PanelAlert>
  144. )}
  145. />
  146. </Form>
  147. {canEditRule && (
  148. <PluginList
  149. organization={organization}
  150. project={project}
  151. pluginList={(pluginList ?? []).filter(
  152. p => p.type === 'notification' && p.hasConfiguration
  153. )}
  154. onEnablePlugin={handleEnablePlugin}
  155. onDisablePlugin={handleDisablePlugin}
  156. />
  157. )}
  158. </Fragment>
  159. )}
  160. </Fragment>
  161. );
  162. }
  163. export default ProjectAlertSettings;