utils.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import {useMemo} from 'react';
  2. import orderBy from 'lodash/orderBy';
  3. import {bulkUpdate} from 'sentry/actionCreators/group';
  4. import type {Client} from 'sentry/api';
  5. import {t} from 'sentry/locale';
  6. import ConfigStore from 'sentry/stores/configStore';
  7. import GroupStore from 'sentry/stores/groupStore';
  8. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  9. import type {Event} from 'sentry/types/event';
  10. import type {Group, GroupActivity, TagValue} from 'sentry/types/group';
  11. import {defined} from 'sentry/utils';
  12. import type {ApiQueryKey} from 'sentry/utils/queryClient';
  13. import {useLocation} from 'sentry/utils/useLocation';
  14. import useOrganization from 'sentry/utils/useOrganization';
  15. import {useParams} from 'sentry/utils/useParams';
  16. import {useUser} from 'sentry/utils/useUser';
  17. import {useGroupTagsReadable} from 'sentry/views/issueDetails/groupTags/useGroupTags';
  18. export function markEventSeen(
  19. api: Client,
  20. orgId: string,
  21. projectId: string,
  22. groupId: string
  23. ) {
  24. bulkUpdate(
  25. api,
  26. {
  27. orgId,
  28. projectId,
  29. itemIds: [groupId],
  30. failSilently: true,
  31. data: {hasSeen: true},
  32. },
  33. {}
  34. );
  35. }
  36. export function useDefaultIssueEvent() {
  37. const user = useLegacyStore(ConfigStore).user;
  38. const options = user ? user.options : null;
  39. return options?.defaultIssueEvent ?? 'recommended';
  40. }
  41. /**
  42. * Combines two TagValue arrays and combines TagValue.count upon conflict
  43. */
  44. export function mergeAndSortTagValues(
  45. tagValues1: TagValue[],
  46. tagValues2: TagValue[],
  47. sort: 'count' | 'lastSeen' = 'lastSeen'
  48. ): TagValue[] {
  49. const tagValueCollection = tagValues1.reduce<Record<string, TagValue>>(
  50. (acc, tagValue) => {
  51. acc[tagValue.value] = tagValue;
  52. return acc;
  53. },
  54. {}
  55. );
  56. tagValues2.forEach(tagValue => {
  57. if (tagValueCollection[tagValue.value]) {
  58. tagValueCollection[tagValue.value].count += tagValue.count;
  59. if (tagValue.lastSeen > tagValueCollection[tagValue.value].lastSeen) {
  60. tagValueCollection[tagValue.value].lastSeen = tagValue.lastSeen;
  61. }
  62. } else {
  63. tagValueCollection[tagValue.value] = tagValue;
  64. }
  65. });
  66. const allTagValues: TagValue[] = Object.values(tagValueCollection);
  67. if (sort === 'count') {
  68. allTagValues.sort((a, b) => b.count - a.count);
  69. } else {
  70. allTagValues.sort((a, b) => (b.lastSeen < a.lastSeen ? -1 : 1));
  71. }
  72. return allTagValues;
  73. }
  74. /**
  75. * Returns the environment name for an event or null
  76. *
  77. * @param event
  78. */
  79. export function getEventEnvironment(event: Event) {
  80. const tag = event.tags.find(({key}) => key === 'environment');
  81. return tag ? tag.value : null;
  82. }
  83. const SUBSCRIPTION_REASONS = {
  84. commented: t(
  85. "You're receiving workflow notifications because you have commented on this issue."
  86. ),
  87. assigned: t(
  88. "You're receiving workflow notifications because you were assigned to this issue."
  89. ),
  90. bookmarked: t(
  91. "You're receiving workflow notifications because you have bookmarked this issue."
  92. ),
  93. changed_status: t(
  94. "You're receiving workflow notifications because you have changed the status of this issue."
  95. ),
  96. mentioned: t(
  97. "You're receiving workflow notifications because you have been mentioned in this issue."
  98. ),
  99. };
  100. /**
  101. * @param group
  102. * @param removeLinks add/remove links to subscription reasons text (default: false)
  103. * @returns Reason for subscription
  104. */
  105. export function getSubscriptionReason(group: Group) {
  106. if (group.subscriptionDetails?.disabled) {
  107. return t('You have disabled workflow notifications for this project.');
  108. }
  109. if (!group.isSubscribed) {
  110. return t('Subscribe to workflow notifications for this issue');
  111. }
  112. if (group.subscriptionDetails) {
  113. const {reason} = group.subscriptionDetails;
  114. if (reason === 'unknown') {
  115. return t(
  116. "You're receiving workflow notifications because you are subscribed to this issue."
  117. );
  118. }
  119. if (reason && SUBSCRIPTION_REASONS.hasOwnProperty(reason)) {
  120. return SUBSCRIPTION_REASONS[reason];
  121. }
  122. }
  123. return t(
  124. "You're receiving updates because you are subscribed to workflow notifications for this project."
  125. );
  126. }
  127. export function getGroupMostRecentActivity(
  128. activities: GroupActivity[] | undefined
  129. ): GroupActivity | undefined {
  130. // Most recent activity
  131. return activities
  132. ? orderBy([...activities], ({dateCreated}) => new Date(dateCreated), ['desc'])[0]
  133. : undefined;
  134. }
  135. export enum ReprocessingStatus {
  136. REPROCESSED_AND_HASNT_EVENT = 'reprocessed_and_hasnt_event',
  137. REPROCESSED_AND_HAS_EVENT = 'reprocessed_and_has_event',
  138. REPROCESSING = 'reprocessing',
  139. NO_STATUS = 'no_status',
  140. }
  141. // Reprocessing Checks
  142. export function getGroupReprocessingStatus(
  143. group: Group,
  144. mostRecentActivity?: GroupActivity
  145. ) {
  146. const {status, count, activity: activities} = group;
  147. const groupCount = Number(count);
  148. switch (status) {
  149. case 'reprocessing':
  150. return ReprocessingStatus.REPROCESSING;
  151. case 'unresolved': {
  152. const groupMostRecentActivity =
  153. mostRecentActivity ?? getGroupMostRecentActivity(activities);
  154. if (groupMostRecentActivity?.type === 'reprocess') {
  155. if (groupCount === 0) {
  156. return ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT;
  157. }
  158. return ReprocessingStatus.REPROCESSED_AND_HAS_EVENT;
  159. }
  160. return ReprocessingStatus.NO_STATUS;
  161. }
  162. default:
  163. return ReprocessingStatus.NO_STATUS;
  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 getGroupEventDetailsQueryData({
  175. environments,
  176. query,
  177. }: {
  178. query: string | undefined;
  179. environments?: string[];
  180. }): Record<string, string | string[]> {
  181. const params: Record<string, string | string[]> = {
  182. collapse: ['fullRelease'],
  183. };
  184. if (query) {
  185. params.query = query;
  186. }
  187. if (environments && environments.length > 0) {
  188. params.environment = environments;
  189. }
  190. return params;
  191. }
  192. export function getGroupEventQueryKey({
  193. orgSlug,
  194. groupId,
  195. eventId,
  196. environments,
  197. recommendedEventQuery,
  198. }: {
  199. environments: string[];
  200. eventId: string;
  201. groupId: string;
  202. orgSlug: string;
  203. recommendedEventQuery?: string;
  204. }): ApiQueryKey {
  205. return [
  206. `/organizations/${orgSlug}/issues/${groupId}/events/${eventId}/`,
  207. {
  208. query: getGroupEventDetailsQueryData({
  209. environments,
  210. query: recommendedEventQuery,
  211. }),
  212. },
  213. ];
  214. }
  215. export function useHasStreamlinedUI() {
  216. const location = useLocation();
  217. const user = useUser();
  218. const organization = useOrganization();
  219. // Allow query param to override all other settings to set the UI.
  220. if (defined(location.query.streamline)) {
  221. return location.query.streamline === '1';
  222. }
  223. // If the enforce flag is set for the organization, ignore user preferences and enable the UI
  224. if (organization.features.includes('issue-details-streamline-enforce')) {
  225. return true;
  226. }
  227. // Apply the UI based on user preferences
  228. return !!user?.options?.prefersIssueDetailsStreamlinedUI;
  229. }
  230. export function useIsSampleEvent(): boolean {
  231. const params = useParams<{groupId: string}>();
  232. const environments = useEnvironmentsFromUrl();
  233. const groupId = params.groupId;
  234. const group = GroupStore.get(groupId);
  235. const {data} = useGroupTagsReadable(
  236. {
  237. groupId: groupId,
  238. environment: environments,
  239. },
  240. // Don't want this query to take precedence over the main requests
  241. {enabled: defined(group)}
  242. );
  243. return data?.some(tag => tag.key === 'sample_event') ?? false;
  244. }