utils.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import orderBy from 'lodash/orderBy';
  2. import {bulkUpdate, useFetchIssueTags} from 'sentry/actionCreators/group';
  3. import {Client} from 'sentry/api';
  4. import {t} from 'sentry/locale';
  5. import {Group, GroupActivity} from 'sentry/types';
  6. import {Event} from 'sentry/types/event';
  7. export function markEventSeen(
  8. api: Client,
  9. orgId: string,
  10. projectId: string,
  11. groupId: string
  12. ) {
  13. bulkUpdate(
  14. api,
  15. {
  16. orgId,
  17. projectId,
  18. itemIds: [groupId],
  19. failSilently: true,
  20. data: {hasSeen: true},
  21. },
  22. {}
  23. );
  24. }
  25. export function fetchGroupUserReports(groupId: string, query: Record<string, string>) {
  26. const api = new Client();
  27. return api.requestPromise(`/issues/${groupId}/user-reports/`, {
  28. includeAllArgs: true,
  29. query,
  30. });
  31. }
  32. /**
  33. * Returns the environment name for an event or null
  34. *
  35. * @param event
  36. */
  37. export function getEventEnvironment(event: Event) {
  38. const tag = event.tags.find(({key}) => key === 'environment');
  39. return tag ? tag.value : null;
  40. }
  41. const SUBSCRIPTION_REASONS = {
  42. commented: t(
  43. "You're receiving workflow notifications because you have commented on this issue."
  44. ),
  45. assigned: t(
  46. "You're receiving workflow notifications because you were assigned to this issue."
  47. ),
  48. bookmarked: t(
  49. "You're receiving workflow notifications because you have bookmarked this issue."
  50. ),
  51. changed_status: t(
  52. "You're receiving workflow notifications because you have changed the status of this issue."
  53. ),
  54. mentioned: t(
  55. "You're receiving workflow notifications because you have been mentioned in this issue."
  56. ),
  57. };
  58. /**
  59. * @param group
  60. * @param removeLinks add/remove links to subscription reasons text (default: false)
  61. * @returns Reason for subscription
  62. */
  63. export function getSubscriptionReason(group: Group) {
  64. if (group.subscriptionDetails && group.subscriptionDetails.disabled) {
  65. return t('You have disabled workflow notifications for this project.');
  66. }
  67. if (!group.isSubscribed) {
  68. return t('Subscribe to workflow notifications for this issue');
  69. }
  70. if (group.subscriptionDetails) {
  71. const {reason} = group.subscriptionDetails;
  72. if (reason === 'unknown') {
  73. return t(
  74. "You're receiving workflow notifications because you are subscribed to this issue."
  75. );
  76. }
  77. if (reason && SUBSCRIPTION_REASONS.hasOwnProperty(reason)) {
  78. return SUBSCRIPTION_REASONS[reason];
  79. }
  80. }
  81. return t(
  82. "You're receiving updates because you are subscribed to workflow notifications for this project."
  83. );
  84. }
  85. export function getGroupMostRecentActivity(activities: GroupActivity[]) {
  86. // Most recent activity
  87. return orderBy([...activities], ({dateCreated}) => new Date(dateCreated), ['desc'])[0];
  88. }
  89. export enum ReprocessingStatus {
  90. REPROCESSED_AND_HASNT_EVENT = 'reprocessed_and_hasnt_event',
  91. REPROCESSED_AND_HAS_EVENT = 'reprocessed_and_has_event',
  92. REPROCESSING = 'reprocessing',
  93. NO_STATUS = 'no_status',
  94. }
  95. // Reprocessing Checks
  96. export function getGroupReprocessingStatus(
  97. group: Group,
  98. mostRecentActivity?: GroupActivity
  99. ) {
  100. const {status, count, activity: activities} = group;
  101. const groupCount = Number(count);
  102. switch (status) {
  103. case 'reprocessing':
  104. return ReprocessingStatus.REPROCESSING;
  105. case 'unresolved': {
  106. const groupMostRecentActivity =
  107. mostRecentActivity ?? getGroupMostRecentActivity(activities);
  108. if (groupMostRecentActivity?.type === 'reprocess') {
  109. if (groupCount === 0) {
  110. return ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT;
  111. }
  112. return ReprocessingStatus.REPROCESSED_AND_HAS_EVENT;
  113. }
  114. return ReprocessingStatus.NO_STATUS;
  115. }
  116. default:
  117. return ReprocessingStatus.NO_STATUS;
  118. }
  119. }
  120. export const useFetchIssueTagsForDetailsPage = (
  121. {
  122. groupId,
  123. environment = [],
  124. }: {
  125. environment: string[];
  126. groupId?: string;
  127. },
  128. {enabled = true}: {enabled?: boolean} = {}
  129. ) => {
  130. return useFetchIssueTags(
  131. {
  132. groupId,
  133. environment,
  134. readable: true,
  135. limit: 4,
  136. },
  137. {enabled}
  138. );
  139. };