errorMigrationWarning.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import styled from '@emotion/styled';
  2. import {
  3. makePromptsCheckQueryKey,
  4. PromptResponse,
  5. promptsUpdate,
  6. usePromptsCheck,
  7. } from 'sentry/actionCreators/prompts';
  8. import Alert from 'sentry/components/alert';
  9. import {Button, LinkButton} from 'sentry/components/button';
  10. import ButtonBar from 'sentry/components/buttonBar';
  11. import {IconClose, IconEdit} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import type {Project} from 'sentry/types';
  14. import {promptIsDismissed} from 'sentry/utils/promptIsDismissed';
  15. import {setApiQueryData, useQueryClient} from 'sentry/utils/queryClient';
  16. import useApi from 'sentry/utils/useApi';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import type {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  19. import {
  20. hasIgnoreArchivedFeatureFlag,
  21. ruleNeedsErrorMigration,
  22. } from 'sentry/views/alerts/utils/migrationUi';
  23. interface ErrorMigrationWarningProps {
  24. project?: Project;
  25. rule?: MetricRule;
  26. }
  27. const METRIC_ALERT_IGNORE_ARCHIVED_ISSUES = 'metric_alert_ignore_archived_issues';
  28. /**
  29. * Displays a message to filter events from archived issues when the metric alert
  30. * is counting error events.
  31. */
  32. export function ErrorMigrationWarning({project, rule}: ErrorMigrationWarningProps) {
  33. const api = useApi();
  34. const organization = useOrganization();
  35. const queryClient = useQueryClient();
  36. const showErrorMigrationWarning =
  37. rule && hasIgnoreArchivedFeatureFlag(organization) && ruleNeedsErrorMigration(rule);
  38. const prompt = usePromptsCheck(
  39. {
  40. feature: METRIC_ALERT_IGNORE_ARCHIVED_ISSUES,
  41. organizationId: organization.id,
  42. projectId: project?.id,
  43. },
  44. {staleTime: Infinity, enabled: showErrorMigrationWarning}
  45. );
  46. const isPromptDismissed =
  47. prompt.isSuccess && prompt.data.data
  48. ? promptIsDismissed({
  49. dismissedTime: prompt.data.data.dismissed_ts,
  50. snoozedTime: prompt.data.data.snoozed_ts,
  51. })
  52. : false;
  53. if (!showErrorMigrationWarning || !rule || isPromptDismissed) {
  54. return null;
  55. }
  56. const dismissPrompt = () => {
  57. promptsUpdate(api, {
  58. organizationId: organization.id,
  59. projectId: project?.id,
  60. feature: METRIC_ALERT_IGNORE_ARCHIVED_ISSUES,
  61. status: 'dismissed',
  62. });
  63. // Update cached query data, set to dismissed
  64. setApiQueryData<PromptResponse>(
  65. queryClient,
  66. makePromptsCheckQueryKey({
  67. feature: METRIC_ALERT_IGNORE_ARCHIVED_ISSUES,
  68. organizationId: organization.id,
  69. projectId: project?.id,
  70. }),
  71. () => {
  72. const dimissedTs = new Date().getTime() / 1000;
  73. return {
  74. data: {dismissed_ts: dimissedTs},
  75. features: {[METRIC_ALERT_IGNORE_ARCHIVED_ISSUES]: {dismissed_ts: dimissedTs}},
  76. };
  77. }
  78. );
  79. };
  80. return (
  81. <Alert
  82. type="warning"
  83. showIcon
  84. trailingItems={
  85. <ButtonBar gap={1}>
  86. <LinkButton
  87. to={{
  88. pathname: `/organizations/${organization.slug}/alerts/metric-rules/${
  89. project?.slug ?? rule?.projects?.[0]
  90. }/${rule.id}/`,
  91. query: {migration: '1'},
  92. }}
  93. size="xs"
  94. icon={<IconEdit size="xs" />}
  95. >
  96. {t('Exclude archived issues')}
  97. </LinkButton>
  98. <DismissButton
  99. priority="link"
  100. icon={<IconClose />}
  101. onClick={dismissPrompt}
  102. aria-label={t('Dismiss Alert')}
  103. title={t('Dismiss Alert')}
  104. />
  105. </ButtonBar>
  106. }
  107. >
  108. {t(
  109. "Alert rules can now exclude errors associated with archived issues. Please make sure to review the rule's alert thresholds after editing."
  110. )}
  111. </Alert>
  112. );
  113. }
  114. const DismissButton = styled(Button)`
  115. color: ${p => p.theme.alert.warning.iconColor};
  116. pointer-events: all;
  117. &:hover {
  118. color: ${p => p.theme.alert.warning.iconHoverColor};
  119. opacity: 0.5;
  120. }
  121. `;