settings.tsx 5.5 KB

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