groupDetails.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. import {
  2. cloneElement,
  3. Fragment,
  4. isValidElement,
  5. useCallback,
  6. useEffect,
  7. useRef,
  8. useState,
  9. } from 'react';
  10. import {browserHistory, RouteComponentProps} from 'react-router';
  11. import styled from '@emotion/styled';
  12. import * as Sentry from '@sentry/react';
  13. import omit from 'lodash/omit';
  14. import pick from 'lodash/pick';
  15. import * as qs from 'query-string';
  16. import LoadingError from 'sentry/components/loadingError';
  17. import LoadingIndicator from 'sentry/components/loadingIndicator';
  18. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  19. import MissingProjectMembership from 'sentry/components/projects/missingProjectMembership';
  20. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  21. import {TabPanels, Tabs} from 'sentry/components/tabs';
  22. import {t} from 'sentry/locale';
  23. import GroupStore from 'sentry/stores/groupStore';
  24. import {space} from 'sentry/styles/space';
  25. import {Group, GroupStatus, IssueCategory, Organization, Project} from 'sentry/types';
  26. import {Event} from 'sentry/types/event';
  27. import {defined} from 'sentry/utils';
  28. import {trackAnalytics} from 'sentry/utils/analytics';
  29. import {getUtcDateString} from 'sentry/utils/dates';
  30. import {
  31. getAnalyticsDataForEvent,
  32. getAnalyticsDataForGroup,
  33. getMessage,
  34. getTitle,
  35. } from 'sentry/utils/events';
  36. import {getAnalyicsDataForProject} from 'sentry/utils/projects';
  37. import {
  38. ApiQueryKey,
  39. setApiQueryData,
  40. useApiQuery,
  41. useQueryClient,
  42. } from 'sentry/utils/queryClient';
  43. import recreateRoute from 'sentry/utils/recreateRoute';
  44. import RequestError from 'sentry/utils/requestError/requestError';
  45. import useDisableRouteAnalytics from 'sentry/utils/routeAnalytics/useDisableRouteAnalytics';
  46. import useRouteAnalyticsEventNames from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames';
  47. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  48. import useApi from 'sentry/utils/useApi';
  49. import {useLocation} from 'sentry/utils/useLocation';
  50. import useOrganization from 'sentry/utils/useOrganization';
  51. import {useParams} from 'sentry/utils/useParams';
  52. import useProjects from 'sentry/utils/useProjects';
  53. import useRouter from 'sentry/utils/useRouter';
  54. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  55. import {ERROR_TYPES} from './constants';
  56. import GroupHeader from './header';
  57. import SampleEventAlert from './sampleEventAlert';
  58. import {Tab, TabPaths} from './types';
  59. import {
  60. getGroupDetailsQueryData,
  61. getGroupEventDetailsQueryData,
  62. getGroupReprocessingStatus,
  63. markEventSeen,
  64. ReprocessingStatus,
  65. useDefaultIssueEvent,
  66. useEnvironmentsFromUrl,
  67. useFetchIssueTagsForDetailsPage,
  68. } from './utils';
  69. type Error = (typeof ERROR_TYPES)[keyof typeof ERROR_TYPES] | null;
  70. type RouterParams = {groupId: string; eventId?: string};
  71. type RouteProps = RouteComponentProps<RouterParams, {}>;
  72. type GroupDetailsProps = {
  73. children: React.ReactNode;
  74. organization: Organization;
  75. projects: Project[];
  76. };
  77. type FetchGroupDetailsState = {
  78. error: boolean;
  79. errorType: Error;
  80. event: Event | null;
  81. eventError: boolean;
  82. group: Group | null;
  83. loadingEvent: boolean;
  84. loadingGroup: boolean;
  85. refetchData: () => void;
  86. refetchGroup: () => void;
  87. };
  88. interface GroupDetailsContentProps extends GroupDetailsProps, FetchGroupDetailsState {
  89. group: Group;
  90. project: Project;
  91. }
  92. function getFetchDataRequestErrorType(status?: number | null): Error {
  93. if (!status) {
  94. return null;
  95. }
  96. if (status === 404) {
  97. return ERROR_TYPES.GROUP_NOT_FOUND;
  98. }
  99. if (status === 403) {
  100. return ERROR_TYPES.MISSING_MEMBERSHIP;
  101. }
  102. return null;
  103. }
  104. function getCurrentTab({router}: {router: RouteProps['router']}) {
  105. const currentRoute = router.routes[router.routes.length - 1];
  106. // If we're in the tag details page ("/tags/:tagKey/")
  107. if (router.params.tagKey) {
  108. return Tab.TAGS;
  109. }
  110. return (
  111. Object.values(Tab).find(tab => currentRoute.path === TabPaths[tab]) ?? Tab.DETAILS
  112. );
  113. }
  114. function getCurrentRouteInfo({
  115. group,
  116. event,
  117. organization,
  118. router,
  119. }: {
  120. event: Event | null;
  121. group: Group;
  122. organization: Organization;
  123. router: RouteProps['router'];
  124. }): {
  125. baseUrl: string;
  126. currentTab: Tab;
  127. } {
  128. const currentTab = getCurrentTab({router});
  129. const baseUrl = normalizeUrl(
  130. `/organizations/${organization.slug}/issues/${group.id}/${
  131. router.params.eventId && event ? `events/${event.id}/` : ''
  132. }`
  133. );
  134. return {baseUrl, currentTab};
  135. }
  136. function getReprocessingNewRoute({
  137. group,
  138. event,
  139. organization,
  140. router,
  141. }: {
  142. event: Event | null;
  143. group: Group;
  144. organization: Organization;
  145. router: RouteProps['router'];
  146. }) {
  147. const {routes, params, location} = router;
  148. const {groupId} = params;
  149. const {currentTab, baseUrl} = getCurrentRouteInfo({group, event, organization, router});
  150. const hasReprocessingV2Feature = organization.features?.includes('reprocessing-v2');
  151. const {id: nextGroupId} = group;
  152. const reprocessingStatus = getGroupReprocessingStatus(group);
  153. if (groupId !== nextGroupId) {
  154. if (hasReprocessingV2Feature) {
  155. // Redirects to the Activities tab
  156. if (
  157. reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT &&
  158. currentTab !== Tab.ACTIVITY
  159. ) {
  160. return {
  161. pathname: `${baseUrl}${Tab.ACTIVITY}/`,
  162. query: {...params, groupId: nextGroupId},
  163. };
  164. }
  165. }
  166. return recreateRoute('', {
  167. routes,
  168. location,
  169. params: {...params, groupId: nextGroupId},
  170. });
  171. }
  172. if (hasReprocessingV2Feature) {
  173. if (
  174. reprocessingStatus === ReprocessingStatus.REPROCESSING &&
  175. currentTab !== Tab.DETAILS
  176. ) {
  177. return {
  178. pathname: baseUrl,
  179. query: params,
  180. };
  181. }
  182. if (
  183. reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT &&
  184. currentTab !== Tab.ACTIVITY &&
  185. currentTab !== Tab.USER_FEEDBACK
  186. ) {
  187. return {
  188. pathname: `${baseUrl}${Tab.ACTIVITY}/`,
  189. query: params,
  190. };
  191. }
  192. }
  193. return undefined;
  194. }
  195. function useRefetchGroupForReprocessing({
  196. refetchGroup,
  197. }: Pick<FetchGroupDetailsState, 'refetchGroup'>) {
  198. const organization = useOrganization();
  199. const hasReprocessingV2Feature = organization.features?.includes('reprocessing-v2');
  200. useEffect(() => {
  201. let refetchInterval: number;
  202. if (hasReprocessingV2Feature) {
  203. refetchInterval = window.setInterval(refetchGroup, 30000);
  204. }
  205. return () => {
  206. window.clearInterval(refetchInterval);
  207. };
  208. }, [hasReprocessingV2Feature, refetchGroup]);
  209. }
  210. function useEventApiQuery({
  211. groupId,
  212. eventId,
  213. environments,
  214. }: {
  215. environments: string[];
  216. groupId: string;
  217. eventId?: string;
  218. }) {
  219. const organization = useOrganization();
  220. const location = useLocation<{query?: string}>();
  221. const router = useRouter();
  222. const hasMostHelpfulEventFeature = organization.features.includes(
  223. 'issue-details-most-helpful-event'
  224. );
  225. const defaultIssueEvent = useDefaultIssueEvent();
  226. const eventIdUrl =
  227. eventId ?? (hasMostHelpfulEventFeature ? defaultIssueEvent : 'latest');
  228. const helpfulEventQuery =
  229. hasMostHelpfulEventFeature && typeof location.query.query === 'string'
  230. ? location.query.query
  231. : undefined;
  232. const endpointEventId = eventIdUrl === 'recommended' ? 'helpful' : eventIdUrl;
  233. const queryKey: ApiQueryKey = [
  234. `/organizations/${organization.slug}/issues/${groupId}/events/${endpointEventId}/`,
  235. {
  236. query: getGroupEventDetailsQueryData({
  237. environments,
  238. query: helpfulEventQuery,
  239. }),
  240. },
  241. ];
  242. const tab = getCurrentTab({router});
  243. const isOnDetailsTab = tab === Tab.DETAILS;
  244. const isLatestOrHelpfulEvent = eventIdUrl === 'latest' || eventIdUrl === 'recommended';
  245. const latestOrHelpfulEvent = useApiQuery<Event>(queryKey, {
  246. // Latest/helpful event will change over time, so only cache for 30 seconds
  247. staleTime: 30000,
  248. cacheTime: 30000,
  249. enabled: isOnDetailsTab && isLatestOrHelpfulEvent,
  250. retry: false,
  251. });
  252. const otherEventQuery = useApiQuery<Event>(queryKey, {
  253. // Oldest/specific events will never change
  254. staleTime: Infinity,
  255. enabled: isOnDetailsTab && !isLatestOrHelpfulEvent,
  256. retry: false,
  257. });
  258. useEffect(() => {
  259. if (latestOrHelpfulEvent.isError) {
  260. // If we get an error from the helpful event endpoint, it probably means
  261. // the query failed validation. We should remove the query to try again.
  262. if (hasMostHelpfulEventFeature) {
  263. browserHistory.replace({
  264. ...window.location,
  265. query: omit(qs.parse(window.location.search), 'query'),
  266. });
  267. // 404s are expected if all events have exceeded retention
  268. if (latestOrHelpfulEvent.error.status === 404) {
  269. return;
  270. }
  271. const scope = new Sentry.Scope();
  272. scope.setExtras({
  273. groupId,
  274. query: helpfulEventQuery,
  275. ...pick(latestOrHelpfulEvent.error, ['message', 'status', 'responseJSON']),
  276. });
  277. scope.setFingerprint(['issue-details-helpful-event-request-failed']);
  278. Sentry.captureException(
  279. new Error('Issue Details: Helpful event request failed'),
  280. scope
  281. );
  282. }
  283. }
  284. }, [
  285. latestOrHelpfulEvent.isError,
  286. latestOrHelpfulEvent.error,
  287. hasMostHelpfulEventFeature,
  288. groupId,
  289. helpfulEventQuery,
  290. ]);
  291. return isLatestOrHelpfulEvent ? latestOrHelpfulEvent : otherEventQuery;
  292. }
  293. type FetchGroupQueryParameters = {
  294. environments: string[];
  295. groupId: string;
  296. organizationSlug: string;
  297. };
  298. function makeFetchGroupQueryKey({
  299. groupId,
  300. organizationSlug,
  301. environments,
  302. }: FetchGroupQueryParameters): ApiQueryKey {
  303. return [
  304. `/organizations/${organizationSlug}/issues/${groupId}/`,
  305. {query: getGroupDetailsQueryData({environments})},
  306. ];
  307. }
  308. /**
  309. * This is a temporary measure to ensure that the GroupStore and query cache
  310. * are both up to date while we are still using both in the issue details page.
  311. * Once we remove all references to GroupStore in the issue details page we
  312. * should remove this.
  313. */
  314. function useSyncGroupStore(incomingEnvs: string[]) {
  315. const queryClient = useQueryClient();
  316. const organization = useOrganization();
  317. const environmentsRef = useRef<string[]>(incomingEnvs);
  318. environmentsRef.current = incomingEnvs;
  319. const unlisten = useRef<Function>();
  320. if (unlisten.current === undefined) {
  321. unlisten.current = GroupStore.listen(() => {
  322. const [storeGroup] = GroupStore.getState();
  323. const environments = environmentsRef.current;
  324. if (defined(storeGroup)) {
  325. setApiQueryData(
  326. queryClient,
  327. makeFetchGroupQueryKey({
  328. groupId: storeGroup.id,
  329. organizationSlug: organization.slug,
  330. environments,
  331. }),
  332. storeGroup
  333. );
  334. }
  335. }, undefined);
  336. }
  337. useEffect(() => {
  338. return () => unlisten.current?.();
  339. }, []);
  340. }
  341. function useFetchGroupDetails(): FetchGroupDetailsState {
  342. const api = useApi();
  343. const organization = useOrganization();
  344. const router = useRouter();
  345. const params = router.params;
  346. const [error, setError] = useState<boolean>(false);
  347. const [errorType, setErrorType] = useState<Error | null>(null);
  348. const [event, setEvent] = useState<Event | null>(null);
  349. const [allProjectChanged, setAllProjectChanged] = useState<boolean>(false);
  350. const environments = useEnvironmentsFromUrl();
  351. const groupId = params.groupId;
  352. const {
  353. data: eventData,
  354. isLoading: loadingEvent,
  355. isError,
  356. refetch: refetchEvent,
  357. } = useEventApiQuery({
  358. groupId,
  359. eventId: params.eventId,
  360. environments,
  361. });
  362. const {
  363. data: groupData,
  364. isLoading: loadingGroup,
  365. isError: isGroupError,
  366. error: groupError,
  367. refetch: refetchGroupCall,
  368. } = useApiQuery<Group>(
  369. makeFetchGroupQueryKey({organizationSlug: organization.slug, groupId, environments}),
  370. {
  371. staleTime: 30000,
  372. cacheTime: 30000,
  373. retry: false,
  374. }
  375. );
  376. const group = groupData ?? null;
  377. useEffect(() => {
  378. if (defined(group)) {
  379. GroupStore.loadInitialData([group]);
  380. }
  381. }, [groupId, group]);
  382. useSyncGroupStore(environments);
  383. useEffect(() => {
  384. if (eventData) {
  385. setEvent(eventData);
  386. }
  387. }, [eventData]);
  388. useEffect(() => {
  389. if (group && event) {
  390. const reprocessingNewRoute = getReprocessingNewRoute({
  391. group,
  392. event,
  393. router,
  394. organization,
  395. });
  396. if (reprocessingNewRoute) {
  397. browserHistory.push(reprocessingNewRoute);
  398. return;
  399. }
  400. }
  401. }, [group, event, router, organization]);
  402. useEffect(() => {
  403. const matchingProjectSlug = group?.project?.slug;
  404. if (!matchingProjectSlug) {
  405. return;
  406. }
  407. if (!group.hasSeen) {
  408. markEventSeen(api, organization.slug, matchingProjectSlug, params.groupId);
  409. }
  410. }, [
  411. api,
  412. group?.hasSeen,
  413. group?.project?.id,
  414. group?.project?.slug,
  415. organization.slug,
  416. params.groupId,
  417. ]);
  418. const allProjectsFlag = router.location.query._allp;
  419. useEffect(() => {
  420. const locationQuery = qs.parse(window.location.search) || {};
  421. // We use _allp as a temporary measure to know they came from the
  422. // issue list page with no project selected (all projects included in
  423. // filter).
  424. //
  425. // If it is not defined, we add the locked project id to the URL
  426. // (this is because if someone navigates directly to an issue on
  427. // single-project priveleges, then goes back - they were getting
  428. // assigned to the first project).
  429. //
  430. // If it is defined, we do not so that our back button will bring us
  431. // to the issue list page with no project selected instead of the
  432. // locked project.
  433. if (
  434. locationQuery.project === undefined &&
  435. !allProjectsFlag &&
  436. !allProjectChanged &&
  437. group?.project.id
  438. ) {
  439. locationQuery.project = group?.project.id;
  440. browserHistory.replace({...window.location, query: locationQuery});
  441. }
  442. if (allProjectsFlag && !allProjectChanged) {
  443. delete locationQuery.project;
  444. // We delete _allp from the URL to keep the hack a bit cleaner, but
  445. // this is not an ideal solution and will ultimately be replaced with
  446. // something smarter.
  447. delete locationQuery._allp;
  448. browserHistory.replace({...window.location, query: locationQuery});
  449. setAllProjectChanged(true);
  450. }
  451. }, [allProjectsFlag, group?.project.id, allProjectChanged]);
  452. const handleError = useCallback((e: RequestError) => {
  453. Sentry.captureException(e);
  454. setErrorType(getFetchDataRequestErrorType(e?.status));
  455. setError(true);
  456. }, []);
  457. useEffect(() => {
  458. if (isGroupError) {
  459. handleError(groupError);
  460. }
  461. }, [isGroupError, groupError, handleError]);
  462. const refetchGroup = useCallback(() => {
  463. if (group?.status !== GroupStatus.REPROCESSING || loadingGroup || loadingEvent) {
  464. return;
  465. }
  466. refetchGroupCall();
  467. }, [group, loadingGroup, loadingEvent, refetchGroupCall]);
  468. const refetchData = useCallback(() => {
  469. // Set initial state
  470. setError(false);
  471. setErrorType(null);
  472. refetchEvent();
  473. refetchGroup();
  474. }, [refetchGroup, refetchEvent]);
  475. // Refetch when group is stale
  476. useEffect(() => {
  477. if (group) {
  478. if ((group as Group & {stale?: boolean}).stale) {
  479. refetchGroup();
  480. return;
  481. }
  482. }
  483. }, [refetchGroup, group]);
  484. useRefetchGroupForReprocessing({refetchGroup});
  485. useEffect(() => {
  486. return () => {
  487. GroupStore.reset();
  488. };
  489. }, []);
  490. return {
  491. loadingGroup,
  492. loadingEvent,
  493. group,
  494. event,
  495. errorType,
  496. error,
  497. eventError: isError,
  498. refetchData,
  499. refetchGroup,
  500. };
  501. }
  502. function useLoadedEventType() {
  503. const organization = useOrganization();
  504. const params = useParams<{eventId?: string}>();
  505. const defaultIssueEvent = useDefaultIssueEvent();
  506. const hasMostHelpfulEventFeature = organization.features.includes(
  507. 'issue-details-most-helpful-event'
  508. );
  509. switch (params.eventId) {
  510. case undefined:
  511. return hasMostHelpfulEventFeature ? defaultIssueEvent : 'latest';
  512. case 'latest':
  513. case 'oldest':
  514. return params.eventId;
  515. default:
  516. return 'event_id';
  517. }
  518. }
  519. function useTrackView({
  520. group,
  521. event,
  522. project,
  523. tab,
  524. }: {
  525. event: Event | null;
  526. group: Group | null;
  527. tab: Tab;
  528. project?: Project;
  529. }) {
  530. const location = useLocation();
  531. const {alert_date, alert_rule_id, alert_type, ref_fallback, stream_index, query} =
  532. location.query;
  533. const groupEventType = useLoadedEventType();
  534. useRouteAnalyticsEventNames('issue_details.viewed', 'Issue Details: Viewed');
  535. useRouteAnalyticsParams({
  536. ...getAnalyticsDataForGroup(group),
  537. ...getAnalyticsDataForEvent(event),
  538. ...getAnalyicsDataForProject(project),
  539. tab,
  540. stream_index: typeof stream_index === 'string' ? Number(stream_index) : undefined,
  541. query: typeof query === 'string' ? query : undefined,
  542. // Alert properties track if the user came from email/slack alerts
  543. alert_date:
  544. typeof alert_date === 'string' ? getUtcDateString(Number(alert_date)) : undefined,
  545. alert_rule_id: typeof alert_rule_id === 'string' ? alert_rule_id : undefined,
  546. alert_type: typeof alert_type === 'string' ? alert_type : undefined,
  547. ref_fallback,
  548. group_event_type: groupEventType,
  549. // Will be updated by StacktraceLink if there is a stacktrace link
  550. stacktrace_link_viewed: false,
  551. // Will be updated by IssueQuickTrace if there is a trace
  552. trace_status: 'none',
  553. // Will be updated in GroupDetailsHeader if there are replays
  554. group_has_replay: false,
  555. });
  556. useDisableRouteAnalytics(!group || !event || !project);
  557. }
  558. const trackTabChanged = ({
  559. organization,
  560. project,
  561. group,
  562. event,
  563. tab,
  564. }: {
  565. event: Event | null;
  566. group: Group;
  567. organization: Organization;
  568. project: Project;
  569. tab: Tab;
  570. }) => {
  571. if (!project || !group) {
  572. return;
  573. }
  574. trackAnalytics('issue_details.tab_changed', {
  575. organization,
  576. project_id: parseInt(project.id, 10),
  577. tab,
  578. ...getAnalyticsDataForGroup(group),
  579. });
  580. if (group.issueCategory !== IssueCategory.ERROR) {
  581. return;
  582. }
  583. const analyticsData = event
  584. ? event.tags
  585. .filter(({key}) => ['device', 'os', 'browser'].includes(key))
  586. .reduce((acc, {key, value}) => {
  587. acc[key] = value;
  588. return acc;
  589. }, {})
  590. : {};
  591. trackAnalytics('issue_group_details.tab.clicked', {
  592. organization,
  593. tab,
  594. platform: project.platform,
  595. ...analyticsData,
  596. });
  597. };
  598. function GroupDetailsContentError({
  599. errorType,
  600. onRetry,
  601. }: {
  602. errorType: Error;
  603. onRetry: () => void;
  604. }) {
  605. const organization = useOrganization();
  606. const location = useLocation();
  607. const projectId = location.query.project;
  608. const {projects} = useProjects();
  609. const project = projects.find(proj => proj.id === projectId);
  610. switch (errorType) {
  611. case ERROR_TYPES.GROUP_NOT_FOUND:
  612. return (
  613. <StyledLoadingError
  614. message={t('The issue you were looking for was not found.')}
  615. />
  616. );
  617. case ERROR_TYPES.MISSING_MEMBERSHIP:
  618. return <MissingProjectMembership organization={organization} project={project} />;
  619. default:
  620. return <StyledLoadingError onRetry={onRetry} />;
  621. }
  622. }
  623. function GroupDetailsContent({
  624. children,
  625. group,
  626. project,
  627. loadingEvent,
  628. eventError,
  629. event,
  630. refetchData,
  631. }: GroupDetailsContentProps) {
  632. const organization = useOrganization();
  633. const router = useRouter();
  634. const {currentTab, baseUrl} = getCurrentRouteInfo({group, event, router, organization});
  635. const groupReprocessingStatus = getGroupReprocessingStatus(group);
  636. const environments = useEnvironmentsFromUrl();
  637. useTrackView({group, event, project, tab: currentTab});
  638. const childProps = {
  639. environments,
  640. group,
  641. project,
  642. event,
  643. loadingEvent,
  644. eventError,
  645. groupReprocessingStatus,
  646. onRetry: refetchData,
  647. baseUrl,
  648. };
  649. return (
  650. <Tabs
  651. value={currentTab}
  652. onChange={tab => trackTabChanged({tab, group, project, event, organization})}
  653. >
  654. <GroupHeader
  655. organization={organization}
  656. groupReprocessingStatus={groupReprocessingStatus}
  657. event={event ?? undefined}
  658. group={group}
  659. baseUrl={baseUrl}
  660. project={project as Project}
  661. />
  662. <GroupTabPanels>
  663. <TabPanels.Item key={currentTab}>
  664. {isValidElement(children) ? cloneElement(children, childProps) : children}
  665. </TabPanels.Item>
  666. </GroupTabPanels>
  667. </Tabs>
  668. );
  669. }
  670. function GroupDetailsPageContent(props: GroupDetailsProps & FetchGroupDetailsState) {
  671. const projectSlug = props.group?.project?.slug;
  672. const {
  673. projects,
  674. initiallyLoaded: projectsLoaded,
  675. fetchError: errorFetchingProjects,
  676. } = useProjects({slugs: projectSlug ? [projectSlug] : []});
  677. const project = projects.find(({slug}) => slug === projectSlug);
  678. const projectWithFallback = project ?? projects[0];
  679. useEffect(() => {
  680. if (props.group && projectsLoaded && !project) {
  681. Sentry.withScope(scope => {
  682. const projectIds = projects.map(item => item.id);
  683. scope.setContext('missingProject', {
  684. projectId: props.group?.project.id,
  685. availableProjects: projectIds,
  686. });
  687. scope.setFingerprint(['group-details-project-not-found']);
  688. Sentry.captureException(new Error('Project not found'));
  689. });
  690. }
  691. }, [props.group, project, projects, projectsLoaded]);
  692. if (props.error) {
  693. return (
  694. <GroupDetailsContentError errorType={props.errorType} onRetry={props.refetchData} />
  695. );
  696. }
  697. if (errorFetchingProjects) {
  698. return <StyledLoadingError message={t('Error loading the specified project')} />;
  699. }
  700. if (projectSlug && !errorFetchingProjects && projectsLoaded && !projectWithFallback) {
  701. return (
  702. <StyledLoadingError message={t('The project %s does not exist', projectSlug)} />
  703. );
  704. }
  705. if (!projectsLoaded || !projectWithFallback || !props.group) {
  706. return <LoadingIndicator />;
  707. }
  708. return (
  709. <GroupDetailsContent {...props} project={projectWithFallback} group={props.group} />
  710. );
  711. }
  712. function GroupDetails(props: GroupDetailsProps) {
  713. const organization = useOrganization();
  714. const router = useRouter();
  715. const {group, ...fetchGroupDetailsProps} = useFetchGroupDetails();
  716. const environments = useEnvironmentsFromUrl();
  717. const {data} = useFetchIssueTagsForDetailsPage(
  718. {
  719. groupId: router.params.groupId,
  720. orgSlug: organization.slug,
  721. environment: environments,
  722. },
  723. // Don't want this query to take precedence over the main requests
  724. {enabled: defined(group)}
  725. );
  726. const isSampleError = data?.some(tag => tag.key === 'sample_event') ?? false;
  727. const getGroupDetailsTitle = () => {
  728. const defaultTitle = 'Sentry';
  729. if (!group) {
  730. return defaultTitle;
  731. }
  732. const {title} = getTitle(group, organization?.features);
  733. const message = getMessage(group);
  734. const eventDetails = `${organization.slug} — ${group.project.slug}`;
  735. if (title && message) {
  736. return `${title}: ${message} — ${eventDetails}`;
  737. }
  738. return `${title || message || defaultTitle} — ${eventDetails}`;
  739. };
  740. return (
  741. <Fragment>
  742. {isSampleError && group && (
  743. <SampleEventAlert project={group.project} organization={organization} />
  744. )}
  745. <SentryDocumentTitle noSuffix title={getGroupDetailsTitle()}>
  746. <PageFiltersContainer
  747. skipLoadLastUsed
  748. forceProject={group?.project}
  749. shouldForceProject
  750. >
  751. <GroupDetailsPageContent
  752. {...props}
  753. {...{
  754. group,
  755. ...fetchGroupDetailsProps,
  756. }}
  757. />
  758. </PageFiltersContainer>
  759. </SentryDocumentTitle>
  760. </Fragment>
  761. );
  762. }
  763. export default Sentry.withProfiler(GroupDetails);
  764. const StyledLoadingError = styled(LoadingError)`
  765. margin: ${space(2)};
  766. `;
  767. const GroupTabPanels = styled(TabPanels)`
  768. flex-grow: 1;
  769. display: flex;
  770. flex-direction: column;
  771. justify-content: stretch;
  772. `;