utils.tsx 5.9 KB

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