utils.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. start,
  178. end,
  179. statsPeriod,
  180. }: {
  181. query: string | undefined;
  182. end?: string;
  183. environments?: string[];
  184. start?: string;
  185. statsPeriod?: string;
  186. }): Record<string, string | string[]> {
  187. const params: Record<string, string | string[]> = {
  188. collapse: ['fullRelease'],
  189. };
  190. if (query) {
  191. params.query = query;
  192. }
  193. if (environments && environments.length > 0) {
  194. params.environment = environments;
  195. }
  196. if (start) {
  197. params.start = start;
  198. }
  199. if (end) {
  200. params.end = end;
  201. }
  202. if (statsPeriod) {
  203. params.statsPeriod = statsPeriod;
  204. }
  205. return params;
  206. }
  207. export function getGroupEventQueryKey({
  208. orgSlug,
  209. groupId,
  210. eventId,
  211. environments,
  212. query,
  213. start,
  214. end,
  215. statsPeriod,
  216. }: {
  217. environments: string[];
  218. eventId: string;
  219. groupId: string;
  220. orgSlug: string;
  221. end?: string;
  222. query?: string;
  223. start?: string;
  224. statsPeriod?: string;
  225. }): ApiQueryKey {
  226. return [
  227. `/organizations/${orgSlug}/issues/${groupId}/events/${eventId}/`,
  228. {
  229. query: getGroupEventDetailsQueryData({
  230. environments,
  231. query,
  232. start,
  233. end,
  234. statsPeriod,
  235. }),
  236. },
  237. ];
  238. }
  239. export function useHasStreamlinedUI() {
  240. const location = useLocation();
  241. const user = useUser();
  242. const organization = useOrganization();
  243. // Allow query param to override all other settings to set the UI.
  244. if (defined(location.query.streamline)) {
  245. return location.query.streamline === '1';
  246. }
  247. // If the enforce flag is set for the organization, ignore user preferences and enable the UI
  248. if (organization.features.includes('issue-details-streamline-enforce')) {
  249. return true;
  250. }
  251. // Apply the UI based on user preferences
  252. return !!user?.options?.prefersIssueDetailsStreamlinedUI;
  253. }
  254. export function useIsSampleEvent(): boolean {
  255. const params = useParams<{groupId: string}>();
  256. const environments = useEnvironmentsFromUrl();
  257. const groupId = params.groupId;
  258. const group = GroupStore.get(groupId);
  259. const {data} = useGroupTagsReadable(
  260. {
  261. groupId,
  262. environment: environments,
  263. },
  264. // Don't want this query to take precedence over the main requests
  265. {enabled: defined(group)}
  266. );
  267. return data?.some(tag => tag.key === 'sample_event') ?? false;
  268. }