groupDetails.tsx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. const scope = new Sentry.Scope();
  268. scope.setExtras({
  269. groupId,
  270. query: helpfulEventQuery,
  271. ...pick(latestOrHelpfulEvent.error, ['message', 'status', 'responseJSON']),
  272. });
  273. scope.setFingerprint(['issue-details-helpful-event-request-failed']);
  274. Sentry.captureException(
  275. new Error('Issue Details: Helpful event request failed'),
  276. scope
  277. );
  278. }
  279. }
  280. }, [
  281. latestOrHelpfulEvent.isError,
  282. latestOrHelpfulEvent.error,
  283. hasMostHelpfulEventFeature,
  284. groupId,
  285. helpfulEventQuery,
  286. ]);
  287. return isLatestOrHelpfulEvent ? latestOrHelpfulEvent : otherEventQuery;
  288. }
  289. type FetchGroupQueryParameters = {
  290. environments: string[];
  291. groupId: string;
  292. organizationSlug: string;
  293. };
  294. function makeFetchGroupQueryKey({
  295. groupId,
  296. organizationSlug,
  297. environments,
  298. }: FetchGroupQueryParameters): ApiQueryKey {
  299. return [
  300. `/organizations/${organizationSlug}/issues/${groupId}/`,
  301. {query: getGroupDetailsQueryData({environments})},
  302. ];
  303. }
  304. /**
  305. * This is a temporary measure to ensure that the GroupStore and query cache
  306. * are both up to date while we are still using both in the issue details page.
  307. * Once we remove all references to GroupStore in the issue details page we
  308. * should remove this.
  309. */
  310. function useSyncGroupStore(incomingEnvs: string[]) {
  311. const queryClient = useQueryClient();
  312. const organization = useOrganization();
  313. const environmentsRef = useRef<string[]>(incomingEnvs);
  314. environmentsRef.current = incomingEnvs;
  315. const unlisten = useRef<Function>();
  316. if (unlisten.current === undefined) {
  317. unlisten.current = GroupStore.listen(() => {
  318. const [storeGroup] = GroupStore.getState();
  319. const environments = environmentsRef.current;
  320. if (defined(storeGroup)) {
  321. setApiQueryData(
  322. queryClient,
  323. makeFetchGroupQueryKey({
  324. groupId: storeGroup.id,
  325. organizationSlug: organization.slug,
  326. environments,
  327. }),
  328. storeGroup
  329. );
  330. }
  331. }, undefined);
  332. }
  333. useEffect(() => {
  334. return () => unlisten.current?.();
  335. }, []);
  336. }
  337. function useFetchGroupDetails(): FetchGroupDetailsState {
  338. const api = useApi();
  339. const organization = useOrganization();
  340. const router = useRouter();
  341. const params = router.params;
  342. const [error, setError] = useState<boolean>(false);
  343. const [errorType, setErrorType] = useState<Error | null>(null);
  344. const [event, setEvent] = useState<Event | null>(null);
  345. const [allProjectChanged, setAllProjectChanged] = useState<boolean>(false);
  346. const environments = useEnvironmentsFromUrl();
  347. const groupId = params.groupId;
  348. const {
  349. data: eventData,
  350. isLoading: loadingEvent,
  351. isError,
  352. refetch: refetchEvent,
  353. } = useEventApiQuery({
  354. groupId,
  355. eventId: params.eventId,
  356. environments,
  357. });
  358. const {
  359. data: groupData,
  360. isLoading: loadingGroup,
  361. isError: isGroupError,
  362. error: groupError,
  363. refetch: refetchGroupCall,
  364. } = useApiQuery<Group>(
  365. makeFetchGroupQueryKey({organizationSlug: organization.slug, groupId, environments}),
  366. {
  367. staleTime: 30000,
  368. cacheTime: 30000,
  369. retry: false,
  370. }
  371. );
  372. const group = groupData ?? null;
  373. useEffect(() => {
  374. if (defined(group)) {
  375. GroupStore.loadInitialData([group]);
  376. }
  377. }, [groupId, group]);
  378. useSyncGroupStore(environments);
  379. useEffect(() => {
  380. if (eventData) {
  381. setEvent(eventData);
  382. }
  383. }, [eventData]);
  384. useEffect(() => {
  385. if (group && event) {
  386. const reprocessingNewRoute = getReprocessingNewRoute({
  387. group,
  388. event,
  389. router,
  390. organization,
  391. });
  392. if (reprocessingNewRoute) {
  393. browserHistory.push(reprocessingNewRoute);
  394. return;
  395. }
  396. }
  397. }, [group, event, router, organization]);
  398. useEffect(() => {
  399. const matchingProjectSlug = group?.project?.slug;
  400. if (!matchingProjectSlug) {
  401. return;
  402. }
  403. if (!group.hasSeen) {
  404. markEventSeen(api, organization.slug, matchingProjectSlug, params.groupId);
  405. }
  406. }, [
  407. api,
  408. group?.hasSeen,
  409. group?.project?.id,
  410. group?.project?.slug,
  411. organization.slug,
  412. params.groupId,
  413. ]);
  414. const allProjectsFlag = router.location.query._allp;
  415. useEffect(() => {
  416. const locationQuery = qs.parse(window.location.search) || {};
  417. // We use _allp as a temporary measure to know they came from the
  418. // issue list page with no project selected (all projects included in
  419. // filter).
  420. //
  421. // If it is not defined, we add the locked project id to the URL
  422. // (this is because if someone navigates directly to an issue on
  423. // single-project priveleges, then goes back - they were getting
  424. // assigned to the first project).
  425. //
  426. // If it is defined, we do not so that our back button will bring us
  427. // to the issue list page with no project selected instead of the
  428. // locked project.
  429. if (
  430. locationQuery.project === undefined &&
  431. !allProjectsFlag &&
  432. !allProjectChanged &&
  433. group?.project.id
  434. ) {
  435. locationQuery.project = group?.project.id;
  436. browserHistory.replace({...window.location, query: locationQuery});
  437. }
  438. if (allProjectsFlag && !allProjectChanged) {
  439. delete locationQuery.project;
  440. // We delete _allp from the URL to keep the hack a bit cleaner, but
  441. // this is not an ideal solution and will ultimately be replaced with
  442. // something smarter.
  443. delete locationQuery._allp;
  444. browserHistory.replace({...window.location, query: locationQuery});
  445. setAllProjectChanged(true);
  446. }
  447. }, [allProjectsFlag, group?.project.id, allProjectChanged]);
  448. const handleError = useCallback((e: RequestError) => {
  449. Sentry.captureException(e);
  450. setErrorType(getFetchDataRequestErrorType(e?.status));
  451. setError(true);
  452. }, []);
  453. useEffect(() => {
  454. if (isGroupError) {
  455. handleError(groupError);
  456. }
  457. }, [isGroupError, groupError, handleError]);
  458. const refetchGroup = useCallback(() => {
  459. if (group?.status !== GroupStatus.REPROCESSING || loadingGroup || loadingEvent) {
  460. return;
  461. }
  462. refetchGroupCall();
  463. }, [group, loadingGroup, loadingEvent, refetchGroupCall]);
  464. const refetchData = useCallback(() => {
  465. // Set initial state
  466. setError(false);
  467. setErrorType(null);
  468. refetchEvent();
  469. refetchGroup();
  470. }, [refetchGroup, refetchEvent]);
  471. // Refetch when group is stale
  472. useEffect(() => {
  473. if (group) {
  474. if ((group as Group & {stale?: boolean}).stale) {
  475. refetchGroup();
  476. return;
  477. }
  478. }
  479. }, [refetchGroup, group]);
  480. useRefetchGroupForReprocessing({refetchGroup});
  481. useEffect(() => {
  482. return () => {
  483. GroupStore.reset();
  484. };
  485. }, []);
  486. return {
  487. loadingGroup,
  488. loadingEvent,
  489. group,
  490. event,
  491. errorType,
  492. error,
  493. eventError: isError,
  494. refetchData,
  495. refetchGroup,
  496. };
  497. }
  498. function useLoadedEventType() {
  499. const organization = useOrganization();
  500. const params = useParams<{eventId?: string}>();
  501. const defaultIssueEvent = useDefaultIssueEvent();
  502. const hasMostHelpfulEventFeature = organization.features.includes(
  503. 'issue-details-most-helpful-event'
  504. );
  505. switch (params.eventId) {
  506. case undefined:
  507. return hasMostHelpfulEventFeature ? defaultIssueEvent : 'latest';
  508. case 'latest':
  509. case 'oldest':
  510. return params.eventId;
  511. default:
  512. return 'event_id';
  513. }
  514. }
  515. function useTrackView({
  516. group,
  517. event,
  518. project,
  519. tab,
  520. }: {
  521. event: Event | null;
  522. group: Group | null;
  523. tab: Tab;
  524. project?: Project;
  525. }) {
  526. const location = useLocation();
  527. const {alert_date, alert_rule_id, alert_type, ref_fallback, stream_index, query} =
  528. location.query;
  529. const groupEventType = useLoadedEventType();
  530. useRouteAnalyticsEventNames('issue_details.viewed', 'Issue Details: Viewed');
  531. useRouteAnalyticsParams({
  532. ...getAnalyticsDataForGroup(group),
  533. ...getAnalyticsDataForEvent(event),
  534. ...getAnalyicsDataForProject(project),
  535. tab,
  536. stream_index: typeof stream_index === 'string' ? Number(stream_index) : undefined,
  537. query: typeof query === 'string' ? query : undefined,
  538. // Alert properties track if the user came from email/slack alerts
  539. alert_date:
  540. typeof alert_date === 'string' ? getUtcDateString(Number(alert_date)) : undefined,
  541. alert_rule_id: typeof alert_rule_id === 'string' ? alert_rule_id : undefined,
  542. alert_type: typeof alert_type === 'string' ? alert_type : undefined,
  543. ref_fallback,
  544. group_event_type: groupEventType,
  545. // Will be updated by StacktraceLink if there is a stacktrace link
  546. stacktrace_link_viewed: false,
  547. // Will be updated by IssueQuickTrace if there is a trace
  548. trace_status: 'none',
  549. // Will be updated in GroupDetailsHeader if there are replays
  550. group_has_replay: false,
  551. });
  552. useDisableRouteAnalytics(!group || !event || !project);
  553. }
  554. const trackTabChanged = ({
  555. organization,
  556. project,
  557. group,
  558. event,
  559. tab,
  560. }: {
  561. event: Event | null;
  562. group: Group;
  563. organization: Organization;
  564. project: Project;
  565. tab: Tab;
  566. }) => {
  567. if (!project || !group) {
  568. return;
  569. }
  570. trackAnalytics('issue_details.tab_changed', {
  571. organization,
  572. project_id: parseInt(project.id, 10),
  573. tab,
  574. ...getAnalyticsDataForGroup(group),
  575. });
  576. if (group.issueCategory !== IssueCategory.ERROR) {
  577. return;
  578. }
  579. const analyticsData = event
  580. ? event.tags
  581. .filter(({key}) => ['device', 'os', 'browser'].includes(key))
  582. .reduce((acc, {key, value}) => {
  583. acc[key] = value;
  584. return acc;
  585. }, {})
  586. : {};
  587. trackAnalytics('issue_group_details.tab.clicked', {
  588. organization,
  589. tab,
  590. platform: project.platform,
  591. ...analyticsData,
  592. });
  593. };
  594. function GroupDetailsContentError({
  595. errorType,
  596. onRetry,
  597. }: {
  598. errorType: Error;
  599. onRetry: () => void;
  600. }) {
  601. const organization = useOrganization();
  602. const location = useLocation();
  603. const projectId = location.query.project;
  604. const {projects} = useProjects();
  605. const project = projects.find(proj => proj.id === projectId);
  606. switch (errorType) {
  607. case ERROR_TYPES.GROUP_NOT_FOUND:
  608. return (
  609. <StyledLoadingError
  610. message={t('The issue you were looking for was not found.')}
  611. />
  612. );
  613. case ERROR_TYPES.MISSING_MEMBERSHIP:
  614. return <MissingProjectMembership organization={organization} project={project} />;
  615. default:
  616. return <StyledLoadingError onRetry={onRetry} />;
  617. }
  618. }
  619. function GroupDetailsContent({
  620. children,
  621. group,
  622. project,
  623. loadingEvent,
  624. eventError,
  625. event,
  626. refetchData,
  627. }: GroupDetailsContentProps) {
  628. const organization = useOrganization();
  629. const router = useRouter();
  630. const {currentTab, baseUrl} = getCurrentRouteInfo({group, event, router, organization});
  631. const groupReprocessingStatus = getGroupReprocessingStatus(group);
  632. const environments = useEnvironmentsFromUrl();
  633. useTrackView({group, event, project, tab: currentTab});
  634. const childProps = {
  635. environments,
  636. group,
  637. project,
  638. event,
  639. loadingEvent,
  640. eventError,
  641. groupReprocessingStatus,
  642. onRetry: refetchData,
  643. baseUrl,
  644. };
  645. return (
  646. <Tabs
  647. value={currentTab}
  648. onChange={tab => trackTabChanged({tab, group, project, event, organization})}
  649. >
  650. <GroupHeader
  651. organization={organization}
  652. groupReprocessingStatus={groupReprocessingStatus}
  653. event={event ?? undefined}
  654. group={group}
  655. baseUrl={baseUrl}
  656. project={project as Project}
  657. />
  658. <GroupTabPanels>
  659. <TabPanels.Item key={currentTab}>
  660. {isValidElement(children) ? cloneElement(children, childProps) : children}
  661. </TabPanels.Item>
  662. </GroupTabPanels>
  663. </Tabs>
  664. );
  665. }
  666. function GroupDetailsPageContent(props: GroupDetailsProps & FetchGroupDetailsState) {
  667. const projectSlug = props.group?.project?.slug;
  668. const {
  669. projects,
  670. initiallyLoaded: projectsLoaded,
  671. fetchError: errorFetchingProjects,
  672. } = useProjects({slugs: projectSlug ? [projectSlug] : []});
  673. const project = projects.find(({slug}) => slug === projectSlug);
  674. const projectWithFallback = project ?? projects[0];
  675. useEffect(() => {
  676. if (props.group && projectsLoaded && !project) {
  677. Sentry.withScope(scope => {
  678. const projectIds = projects.map(item => item.id);
  679. scope.setContext('missingProject', {
  680. projectId: props.group?.project.id,
  681. availableProjects: projectIds,
  682. });
  683. scope.setFingerprint(['group-details-project-not-found']);
  684. Sentry.captureException(new Error('Project not found'));
  685. });
  686. }
  687. }, [props.group, project, projects, projectsLoaded]);
  688. if (props.error) {
  689. return (
  690. <GroupDetailsContentError errorType={props.errorType} onRetry={props.refetchData} />
  691. );
  692. }
  693. if (errorFetchingProjects) {
  694. return <StyledLoadingError message={t('Error loading the specified project')} />;
  695. }
  696. if (projectSlug && !errorFetchingProjects && projectsLoaded && !projectWithFallback) {
  697. return (
  698. <StyledLoadingError message={t('The project %s does not exist', projectSlug)} />
  699. );
  700. }
  701. if (!projectsLoaded || !projectWithFallback || !props.group) {
  702. return <LoadingIndicator />;
  703. }
  704. return (
  705. <GroupDetailsContent {...props} project={projectWithFallback} group={props.group} />
  706. );
  707. }
  708. function GroupDetails(props: GroupDetailsProps) {
  709. const organization = useOrganization();
  710. const router = useRouter();
  711. const {group, ...fetchGroupDetailsProps} = useFetchGroupDetails();
  712. const environments = useEnvironmentsFromUrl();
  713. const {data} = useFetchIssueTagsForDetailsPage(
  714. {
  715. groupId: router.params.groupId,
  716. organizationSlug: organization.slug,
  717. environment: environments,
  718. },
  719. // Don't want this query to take precedence over the main requests
  720. {enabled: defined(group)}
  721. );
  722. const isSampleError = data?.some(tag => tag.key === 'sample_event') ?? false;
  723. const getGroupDetailsTitle = () => {
  724. const defaultTitle = 'Sentry';
  725. if (!group) {
  726. return defaultTitle;
  727. }
  728. const {title} = getTitle(group, organization?.features);
  729. const message = getMessage(group);
  730. const eventDetails = `${organization.slug} — ${group.project.slug}`;
  731. if (title && message) {
  732. return `${title}: ${message} — ${eventDetails}`;
  733. }
  734. return `${title || message || defaultTitle} — ${eventDetails}`;
  735. };
  736. return (
  737. <Fragment>
  738. {isSampleError && group && (
  739. <SampleEventAlert project={group.project} organization={organization} />
  740. )}
  741. <SentryDocumentTitle noSuffix title={getGroupDetailsTitle()}>
  742. <PageFiltersContainer
  743. skipLoadLastUsed
  744. forceProject={group?.project}
  745. shouldForceProject
  746. >
  747. <GroupDetailsPageContent
  748. {...props}
  749. {...{
  750. group,
  751. ...fetchGroupDetailsProps,
  752. }}
  753. />
  754. </PageFiltersContainer>
  755. </SentryDocumentTitle>
  756. </Fragment>
  757. );
  758. }
  759. export default Sentry.withProfiler(GroupDetails);
  760. const StyledLoadingError = styled(LoadingError)`
  761. margin: ${space(2)};
  762. `;
  763. const GroupTabPanels = styled(TabPanels)`
  764. flex-grow: 1;
  765. display: flex;
  766. flex-direction: column;
  767. justify-content: stretch;
  768. `;