utils.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {Fragment} from 'react';
  2. import capitalize from 'lodash/capitalize';
  3. import Alert from 'sentry/components/alert';
  4. import ExternalLink from 'sentry/components/links/externalLink';
  5. import {t, tct, tn} from 'sentry/locale';
  6. import {Organization, ResolutionStatusDetails} from 'sentry/types';
  7. import ExtraDescription from './extraDescription';
  8. export const BULK_LIMIT = 1000;
  9. export const BULK_LIMIT_STR = BULK_LIMIT.toLocaleString();
  10. export enum ConfirmAction {
  11. RESOLVE = 'resolve',
  12. UNRESOLVE = 'unresolve',
  13. IGNORE = 'ignore',
  14. BOOKMARK = 'bookmark',
  15. UNBOOKMARK = 'unbookmark',
  16. MERGE = 'merge',
  17. DELETE = 'delete',
  18. }
  19. function getBulkConfirmMessage(action: string, queryCount: number) {
  20. if (queryCount > BULK_LIMIT) {
  21. return tct(
  22. 'Are you sure you want to [action] the first [bulkNumber] issues that match the search?',
  23. {
  24. action,
  25. bulkNumber: BULK_LIMIT_STR,
  26. }
  27. );
  28. }
  29. return tct(
  30. 'Are you sure you want to [action] all [bulkNumber] issues that match the search?',
  31. {
  32. action,
  33. bulkNumber: queryCount,
  34. }
  35. );
  36. }
  37. function PerformanceIssueAlert({
  38. allInQuerySelected,
  39. organization,
  40. children,
  41. }: {
  42. allInQuerySelected: boolean;
  43. children: string;
  44. organization: Organization;
  45. }) {
  46. if (!allInQuerySelected || !organization.features.includes('performance-issues')) {
  47. return null;
  48. }
  49. return (
  50. <Alert type="info" showIcon>
  51. {children}
  52. </Alert>
  53. );
  54. }
  55. export function getConfirm({
  56. numIssues,
  57. allInQuerySelected,
  58. query,
  59. queryCount,
  60. organization,
  61. }: {
  62. allInQuerySelected: boolean;
  63. numIssues: number;
  64. organization: Organization;
  65. query: string;
  66. queryCount: number;
  67. }) {
  68. return function ({
  69. action,
  70. canBeUndone,
  71. append = '',
  72. }: {
  73. action: ConfirmAction | string;
  74. canBeUndone: boolean;
  75. append?: string;
  76. }) {
  77. const question = allInQuerySelected
  78. ? getBulkConfirmMessage(`${action}${append}`, queryCount)
  79. : tn(
  80. // Use sprintf argument swapping since the number value must come
  81. // first. See https://github.com/alexei/sprintf.js#argument-swapping
  82. `Are you sure you want to %2$s this %s issue%3$s?`,
  83. `Are you sure you want to %2$s these %s issues%3$s?`,
  84. numIssues,
  85. action,
  86. append
  87. );
  88. let message: React.ReactNode;
  89. switch (action) {
  90. case ConfirmAction.DELETE:
  91. message = (
  92. <Fragment>
  93. <p>
  94. {tct(
  95. 'Bulk deletion is only recommended for junk data. To clear your stream, consider resolving or ignoring. [link:When should I delete events?]',
  96. {
  97. link: (
  98. <ExternalLink href="https://help.sentry.io/account/billing/when-should-i-delete-events/" />
  99. ),
  100. }
  101. )}
  102. </p>
  103. <PerformanceIssueAlert {...{organization, allInQuerySelected}}>
  104. {t('Deleting performance issues is not yet supported and will be skipped.')}
  105. </PerformanceIssueAlert>
  106. </Fragment>
  107. );
  108. break;
  109. case ConfirmAction.MERGE:
  110. message = (
  111. <Fragment>
  112. <p>{t('Note that unmerging is currently an experimental feature.')}</p>
  113. <PerformanceIssueAlert {...{organization, allInQuerySelected}}>
  114. {t('Merging performance issues is not yet supported and will be skipped.')}
  115. </PerformanceIssueAlert>
  116. </Fragment>
  117. );
  118. break;
  119. default:
  120. message = !canBeUndone ? <p>{t('This action cannot be undone.')}</p> : null;
  121. }
  122. return (
  123. <div>
  124. <p style={{marginBottom: '20px'}}>
  125. <strong>{question}</strong>
  126. </p>
  127. <ExtraDescription
  128. all={allInQuerySelected}
  129. query={query}
  130. queryCount={queryCount}
  131. />
  132. {message}
  133. </div>
  134. );
  135. };
  136. }
  137. export function getLabel(numIssues: number, allInQuerySelected: boolean) {
  138. return function (action: string, append = '') {
  139. const capitalized = capitalize(action);
  140. const text = allInQuerySelected
  141. ? t('Bulk %s issues', action)
  142. : // Use sprintf argument swapping to put the capitalized string first. See
  143. // https://github.com/alexei/sprintf.js#argument-swapping
  144. tn(`%2$s %s selected issue`, `%2$s %s selected issues`, numIssues, capitalized);
  145. return text + append;
  146. };
  147. }
  148. export function performanceIssuesSupportsIgnoreAction(
  149. statusDetails: ResolutionStatusDetails
  150. ) {
  151. return !(statusDetails.ignoreWindow || statusDetails.ignoreUserWindow);
  152. }