utils.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. end: string;
  149. start: string;
  150. transaction: string;
  151. };
  152. },
  153. {enabled = true}: {enabled?: boolean} = {}
  154. ) => {
  155. return useFetchIssueTags(
  156. {
  157. groupId,
  158. orgSlug,
  159. environment,
  160. readable: true,
  161. limit: 4,
  162. isStatisticalDetector,
  163. statisticalDetectorParameters,
  164. },
  165. {enabled}
  166. );
  167. };
  168. export function useEnvironmentsFromUrl(): string[] {
  169. const location = useLocation();
  170. const envs = location.query.environment;
  171. const envsArray = useMemo(() => {
  172. return typeof envs === 'string' ? [envs] : envs ?? [];
  173. }, [envs]);
  174. return envsArray;
  175. }
  176. export function getGroupDetailsQueryData({
  177. environments,
  178. }: {
  179. environments?: string[];
  180. } = {}): Record<string, string | string[]> {
  181. // Note, we do not want to include the environment key at all if there are no environments
  182. const query: Record<string, string | string[]> = {
  183. ...(!isEmpty(environments) ? {environment: environments} : {}),
  184. expand: ['inbox', 'owners'],
  185. collapse: ['release', 'tags'],
  186. };
  187. return query;
  188. }
  189. export function getGroupEventDetailsQueryData({
  190. environments,
  191. query,
  192. stacktraceOnly,
  193. }: {
  194. environments?: string[];
  195. query?: string;
  196. stacktraceOnly?: boolean;
  197. } = {}): Record<string, string | string[]> {
  198. const defaultParams = {
  199. collapse: stacktraceOnly ? ['stacktraceOnly'] : ['fullRelease'],
  200. ...(query ? {query} : {}),
  201. };
  202. if (!environments || isEmpty(environments)) {
  203. return defaultParams;
  204. }
  205. return {...defaultParams, environment: environments};
  206. }