utils.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {useMemo} from 'react';
  2. import isEmpty from 'lodash/isEmpty';
  3. import orderBy from 'lodash/orderBy';
  4. import {bulkUpdate, useFetchIssueTags} from 'sentry/actionCreators/group';
  5. import {Client} from 'sentry/api';
  6. import {t} from 'sentry/locale';
  7. import ConfigStore from 'sentry/stores/configStore';
  8. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  9. import {Group, GroupActivity} from 'sentry/types';
  10. import {Event} from 'sentry/types/event';
  11. import {useLocation} from 'sentry/utils/useLocation';
  12. export function markEventSeen(
  13. api: Client,
  14. orgId: string,
  15. projectId: string,
  16. groupId: string
  17. ) {
  18. bulkUpdate(
  19. api,
  20. {
  21. orgId,
  22. projectId,
  23. itemIds: [groupId],
  24. failSilently: true,
  25. data: {hasSeen: true},
  26. },
  27. {}
  28. );
  29. }
  30. export function fetchGroupUserReports(
  31. orgSlug: string,
  32. groupId: string,
  33. query: Record<string, string>
  34. ) {
  35. const api = new Client();
  36. return api.requestPromise(`/organizations/${orgSlug}/issues/${groupId}/user-reports/`, {
  37. includeAllArgs: true,
  38. query,
  39. });
  40. }
  41. export function useDefaultIssueEvent() {
  42. const user = useLegacyStore(ConfigStore).user;
  43. const options = user ? user.options : null;
  44. return options?.defaultIssueEvent ?? 'recommended';
  45. }
  46. /**
  47. * Returns the environment name for an event or null
  48. *
  49. * @param event
  50. */
  51. export function getEventEnvironment(event: Event) {
  52. const tag = event.tags.find(({key}) => key === 'environment');
  53. return tag ? tag.value : null;
  54. }
  55. const SUBSCRIPTION_REASONS = {
  56. commented: t(
  57. "You're receiving workflow notifications because you have commented on this issue."
  58. ),
  59. assigned: t(
  60. "You're receiving workflow notifications because you were assigned to this issue."
  61. ),
  62. bookmarked: t(
  63. "You're receiving workflow notifications because you have bookmarked this issue."
  64. ),
  65. changed_status: t(
  66. "You're receiving workflow notifications because you have changed the status of this issue."
  67. ),
  68. mentioned: t(
  69. "You're receiving workflow notifications because you have been mentioned in this issue."
  70. ),
  71. };
  72. /**
  73. * @param group
  74. * @param removeLinks add/remove links to subscription reasons text (default: false)
  75. * @returns Reason for subscription
  76. */
  77. export function getSubscriptionReason(group: Group) {
  78. if (group.subscriptionDetails && group.subscriptionDetails.disabled) {
  79. return t('You have disabled workflow notifications for this project.');
  80. }
  81. if (!group.isSubscribed) {
  82. return t('Subscribe to workflow notifications for this issue');
  83. }
  84. if (group.subscriptionDetails) {
  85. const {reason} = group.subscriptionDetails;
  86. if (reason === 'unknown') {
  87. return t(
  88. "You're receiving workflow notifications because you are subscribed to this issue."
  89. );
  90. }
  91. if (reason && SUBSCRIPTION_REASONS.hasOwnProperty(reason)) {
  92. return SUBSCRIPTION_REASONS[reason];
  93. }
  94. }
  95. return t(
  96. "You're receiving updates because you are subscribed to workflow notifications for this project."
  97. );
  98. }
  99. export function getGroupMostRecentActivity(activities: GroupActivity[]) {
  100. // Most recent activity
  101. return orderBy([...activities], ({dateCreated}) => new Date(dateCreated), ['desc'])[0];
  102. }
  103. export enum ReprocessingStatus {
  104. REPROCESSED_AND_HASNT_EVENT = 'reprocessed_and_hasnt_event',
  105. REPROCESSED_AND_HAS_EVENT = 'reprocessed_and_has_event',
  106. REPROCESSING = 'reprocessing',
  107. NO_STATUS = 'no_status',
  108. }
  109. // Reprocessing Checks
  110. export function getGroupReprocessingStatus(
  111. group: Group,
  112. mostRecentActivity?: GroupActivity
  113. ) {
  114. const {status, count, activity: activities} = group;
  115. const groupCount = Number(count);
  116. switch (status) {
  117. case 'reprocessing':
  118. return ReprocessingStatus.REPROCESSING;
  119. case 'unresolved': {
  120. const groupMostRecentActivity =
  121. mostRecentActivity ?? getGroupMostRecentActivity(activities);
  122. if (groupMostRecentActivity?.type === 'reprocess') {
  123. if (groupCount === 0) {
  124. return ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT;
  125. }
  126. return ReprocessingStatus.REPROCESSED_AND_HAS_EVENT;
  127. }
  128. return ReprocessingStatus.NO_STATUS;
  129. }
  130. default:
  131. return ReprocessingStatus.NO_STATUS;
  132. }
  133. }
  134. export const useFetchIssueTagsForDetailsPage = (
  135. {
  136. groupId,
  137. orgSlug,
  138. environment = [],
  139. isStatisticalDetector = false,
  140. statisticalDetectorParameters,
  141. }: {
  142. environment: string[];
  143. orgSlug: string;
  144. groupId?: string;
  145. isStatisticalDetector?: boolean;
  146. statisticalDetectorParameters?: {
  147. durationBaseline: number;
  148. transaction: string;
  149. };
  150. },
  151. {enabled = true}: {enabled?: boolean} = {}
  152. ) => {
  153. return useFetchIssueTags(
  154. {
  155. groupId,
  156. orgSlug,
  157. environment,
  158. readable: true,
  159. limit: 4,
  160. isStatisticalDetector,
  161. statisticalDetectorParameters,
  162. },
  163. {enabled}
  164. );
  165. };
  166. export function useEnvironmentsFromUrl(): string[] {
  167. const location = useLocation();
  168. const envs = location.query.environment;
  169. const envsArray = useMemo(() => {
  170. return typeof envs === 'string' ? [envs] : envs ?? [];
  171. }, [envs]);
  172. return envsArray;
  173. }
  174. export function getGroupDetailsQueryData({
  175. environments,
  176. }: {
  177. environments?: string[];
  178. } = {}): Record<string, string | string[]> {
  179. // Note, we do not want to include the environment key at all if there are no environments
  180. const query: Record<string, string | string[]> = {
  181. ...(!isEmpty(environments) ? {environment: environments} : {}),
  182. expand: ['inbox', 'owners'],
  183. collapse: ['release', 'tags'],
  184. };
  185. return query;
  186. }
  187. export function getGroupEventDetailsQueryData({
  188. environments,
  189. query,
  190. stacktraceOnly,
  191. }: {
  192. environments?: string[];
  193. query?: string;
  194. stacktraceOnly?: boolean;
  195. } = {}): Record<string, string | string[]> {
  196. const defaultParams = {
  197. collapse: stacktraceOnly ? ['stacktraceOnly'] : ['fullRelease'],
  198. ...(query ? {query} : {}),
  199. };
  200. if (!environments || isEmpty(environments)) {
  201. return defaultParams;
  202. }
  203. return {...defaultParams, environment: environments};
  204. }