123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807 |
- import {
- cloneElement,
- Fragment,
- isValidElement,
- useCallback,
- useEffect,
- useState,
- } from 'react';
- import {browserHistory, RouteComponentProps} from 'react-router';
- import styled from '@emotion/styled';
- import * as Sentry from '@sentry/react';
- import {fetchOrganizationEnvironments} from 'sentry/actionCreators/environments';
- import LoadingError from 'sentry/components/loadingError';
- import LoadingIndicator from 'sentry/components/loadingIndicator';
- import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
- import MissingProjectMembership from 'sentry/components/projects/missingProjectMembership';
- import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
- import {TabPanels, Tabs} from 'sentry/components/tabs';
- import {t} from 'sentry/locale';
- import GroupStore from 'sentry/stores/groupStore';
- import {useLegacyStore} from 'sentry/stores/useLegacyStore';
- import {space} from 'sentry/styles/space';
- import {Group, IssueCategory, Organization, Project} from 'sentry/types';
- import {Event} from 'sentry/types/event';
- import {defined} from 'sentry/utils';
- import {trackAnalytics} from 'sentry/utils/analytics';
- import {getUtcDateString} from 'sentry/utils/dates';
- import {
- getAnalyticsDataForEvent,
- getAnalyticsDataForGroup,
- getMessage,
- getTitle,
- } from 'sentry/utils/events';
- import {getAnalyicsDataForProject} from 'sentry/utils/projects';
- import {useApiQuery} from 'sentry/utils/queryClient';
- import recreateRoute from 'sentry/utils/recreateRoute';
- import RequestError from 'sentry/utils/requestError/requestError';
- import useRouteAnalyticsEventNames from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames';
- import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
- import useApi from 'sentry/utils/useApi';
- import {useLocation} from 'sentry/utils/useLocation';
- import useOrganization from 'sentry/utils/useOrganization';
- import usePrevious from 'sentry/utils/usePrevious';
- import useProjects from 'sentry/utils/useProjects';
- import useRouter from 'sentry/utils/useRouter';
- import {normalizeUrl} from 'sentry/utils/withDomainRequired';
- import {ERROR_TYPES} from './constants';
- import GroupHeader from './header';
- import SampleEventAlert from './sampleEventAlert';
- import {Tab, TabPaths} from './types';
- import {
- getGroupReprocessingStatus,
- markEventSeen,
- ReprocessingStatus,
- useFetchIssueTagsForDetailsPage,
- } from './utils';
- type Error = (typeof ERROR_TYPES)[keyof typeof ERROR_TYPES] | null;
- type RouterParams = {groupId: string; eventId?: string};
- type RouteProps = RouteComponentProps<RouterParams, {}>;
- type GroupDetailsProps = {
- children: React.ReactNode;
- environments: string[];
- isGlobalSelectionReady: boolean;
- organization: Organization;
- projects: Project[];
- };
- type FetchGroupDetailsState = {
- error: boolean;
- errorType: Error;
- event: Event | null;
- eventError: boolean;
- fetchData: () => Promise<void>;
- group: Group | null;
- loadingEvent: boolean;
- loadingGroup: boolean;
- project: Project | null;
- refetchData: () => void;
- refetchGroup: () => void;
- };
- interface GroupDetailsContentProps extends GroupDetailsProps, FetchGroupDetailsState {
- group: Group;
- project: Project;
- }
- function getGroupQuery({
- environments,
- }: Pick<GroupDetailsProps, 'environments'>): Record<string, string | string[]> {
- // Note, we do not want to include the environment key at all if there are no environments
- const query: Record<string, string | string[]> = {
- ...(environments ? {environment: environments} : {}),
- expand: ['inbox', 'owners'],
- collapse: ['release', 'tags'],
- };
- return query;
- }
- function getFetchDataRequestErrorType(status?: number | null): Error {
- if (!status) {
- return null;
- }
- if (status === 404) {
- return ERROR_TYPES.GROUP_NOT_FOUND;
- }
- if (status === 403) {
- return ERROR_TYPES.MISSING_MEMBERSHIP;
- }
- return null;
- }
- function getCurrentTab({router}: {router: RouteProps['router']}) {
- const currentRoute = router.routes[router.routes.length - 1];
- // If we're in the tag details page ("/tags/:tagKey/")
- if (router.params.tagKey) {
- return Tab.TAGS;
- }
- return (
- Object.values(Tab).find(tab => currentRoute.path === TabPaths[tab]) ?? Tab.DETAILS
- );
- }
- function getCurrentRouteInfo({
- group,
- event,
- organization,
- router,
- }: {
- event: Event | null;
- group: Group;
- organization: Organization;
- router: RouteProps['router'];
- }): {
- baseUrl: string;
- currentTab: Tab;
- } {
- const currentTab = getCurrentTab({router});
- const baseUrl = normalizeUrl(
- `/organizations/${organization.slug}/issues/${group.id}/${
- router.params.eventId && event ? `events/${event.id}/` : ''
- }`
- );
- return {baseUrl, currentTab};
- }
- function getReprocessingNewRoute({
- group,
- event,
- organization,
- router,
- }: {
- event: Event | null;
- group: Group;
- organization: Organization;
- router: RouteProps['router'];
- }) {
- const {routes, params, location} = router;
- const {groupId} = params;
- const {currentTab, baseUrl} = getCurrentRouteInfo({group, event, organization, router});
- const hasReprocessingV2Feature = organization.features?.includes('reprocessing-v2');
- const {id: nextGroupId} = group;
- const reprocessingStatus = getGroupReprocessingStatus(group);
- if (groupId !== nextGroupId) {
- if (hasReprocessingV2Feature) {
- // Redirects to the Activities tab
- if (
- reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT &&
- currentTab !== Tab.ACTIVITY
- ) {
- return {
- pathname: `${baseUrl}${Tab.ACTIVITY}/`,
- query: {...params, groupId: nextGroupId},
- };
- }
- }
- return recreateRoute('', {
- routes,
- location,
- params: {...params, groupId: nextGroupId},
- });
- }
- if (hasReprocessingV2Feature) {
- if (
- reprocessingStatus === ReprocessingStatus.REPROCESSING &&
- currentTab !== Tab.DETAILS
- ) {
- return {
- pathname: baseUrl,
- query: params,
- };
- }
- if (
- reprocessingStatus === ReprocessingStatus.REPROCESSED_AND_HASNT_EVENT &&
- currentTab !== Tab.ACTIVITY &&
- currentTab !== Tab.USER_FEEDBACK
- ) {
- return {
- pathname: `${baseUrl}${Tab.ACTIVITY}/`,
- query: params,
- };
- }
- }
- return undefined;
- }
- function useRefetchGroupForReprocessing({
- refetchGroup,
- }: Pick<FetchGroupDetailsState, 'refetchGroup'>) {
- const organization = useOrganization();
- const hasReprocessingV2Feature = organization.features?.includes('reprocessing-v2');
- useEffect(() => {
- let refetchInterval: number;
- if (hasReprocessingV2Feature) {
- refetchInterval = window.setInterval(refetchGroup, 30000);
- }
- return () => {
- window.clearInterval(refetchInterval);
- };
- }, [hasReprocessingV2Feature, refetchGroup]);
- }
- function useFetchOnMount({fetchData}: Pick<FetchGroupDetailsState, 'fetchData'>) {
- const api = useApi();
- const organization = useOrganization();
- useEffect(() => {
- fetchData();
- // Fetch environments early - used in GroupEventDetailsContainer
- fetchOrganizationEnvironments(api, organization.slug);
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, []);
- }
- function useEventApiQuery(
- eventID: string,
- queryKey: [string, {query: {environment?: string[]}}]
- ) {
- const isLatest = eventID === 'latest';
- const latestEventQuery = useApiQuery<Event>(queryKey, {
- staleTime: 30000,
- cacheTime: 30000,
- enabled: isLatest,
- retry: (_, error) => error.status !== 404,
- });
- const otherEventQuery = useApiQuery<Event>(queryKey, {
- staleTime: Infinity,
- enabled: !isLatest,
- retry: (_, error) => error.status !== 404,
- });
- return isLatest ? latestEventQuery : otherEventQuery;
- }
- function useFetchGroupDetails({
- isGlobalSelectionReady,
- environments,
- }: Pick<
- GroupDetailsProps,
- 'isGlobalSelectionReady' | 'environments'
- >): FetchGroupDetailsState {
- const api = useApi();
- const organization = useOrganization();
- const router = useRouter();
- const params = router.params;
- const location = useLocation();
- const {projects} = useProjects();
- const allGroups = useLegacyStore(GroupStore);
- const group = allGroups.find(({id}) => id === params.groupId) as Group;
- const [project, setProject] = useState<Project | null>(null);
- const [loadingGroup, setLoadingGroup] = useState<boolean>(false);
- const [error, setError] = useState<boolean>(false);
- const [errorType, setErrorType] = useState<Error | null>(null);
- const [event, setEvent] = useState<Event | null>(null);
- const groupId = params.groupId;
- const eventId = params.eventId ?? 'latest';
- const eventUrl = `/issues/${groupId}/events/${eventId}/`;
- const eventQuery: {environment?: string[]} = {};
- if (environments.length !== 0) {
- eventQuery.environment = environments;
- }
- const {
- data: eventData,
- isLoading: loadingEvent,
- isError,
- refetch: refetchEvent,
- } = useEventApiQuery(eventId, [eventUrl, {query: eventQuery}]);
- useEffect(() => {
- if (eventData) {
- setEvent(eventData);
- }
- }, [eventData]);
- const fetchGroupReleases = useCallback(async () => {
- const releases = await api.requestPromise(
- `/issues/${params.groupId}/first-last-release/`
- );
- GroupStore.onPopulateReleases(params.groupId, releases);
- }, [api, params.groupId]);
- const handleError = useCallback((e: RequestError) => {
- Sentry.captureException(e);
- setLoadingGroup(false);
- setErrorType(getFetchDataRequestErrorType(e?.status));
- setError(true);
- }, []);
- const fetchData = useCallback(async () => {
- // Need to wait for global selection store to be ready before making request
- if (!isGlobalSelectionReady) {
- return;
- }
- try {
- const groupResponse = await api.requestPromise(`/issues/${params.groupId}/`, {
- query: getGroupQuery({environments}),
- });
- fetchGroupReleases();
- const reprocessingNewRoute = getReprocessingNewRoute({
- group: groupResponse,
- event,
- router,
- organization,
- });
- if (reprocessingNewRoute) {
- browserHistory.push(reprocessingNewRoute);
- return;
- }
- const matchingProject = projects?.find(p => p.id === groupResponse.project.id);
- if (!matchingProject) {
- Sentry.withScope(scope => {
- const projectIds = projects.map(item => item.id);
- scope.setContext('missingProject', {
- projectId: groupResponse.project.id,
- availableProjects: projectIds,
- });
- Sentry.captureException(new Error('Project not found'));
- });
- } else {
- markEventSeen(api, organization.slug, matchingProject.slug, params.groupId);
- const locationQuery = {...location.query};
- if (locationQuery.project === undefined && locationQuery._allp === undefined) {
- // We use _allp as a temporary measure to know they came from the
- // issue list page with no project selected (all projects included in
- // filter).
- //
- // If it is not defined, we add the locked project id to the URL
- // (this is because if someone navigates directly to an issue on
- // single-project priveleges, then goes back - they were getting
- // assigned to the first project).
- //
- // If it is defined, we do not so that our back button will bring us
- // to the issue list page with no project selected instead of the
- // locked project.
- locationQuery.project = matchingProject.id;
- }
- // We delete _allp from the URL to keep the hack a bit cleaner, but
- // this is not an ideal solution and will ultimately be replaced with
- // something smarter.
- delete locationQuery._allp;
- browserHistory.replace({...window.location, query: locationQuery});
- }
- setProject(matchingProject || groupResponse.project);
- setLoadingGroup(false);
- GroupStore.loadInitialData([groupResponse]);
- } catch (e) {
- handleError(e);
- }
- }, [
- api,
- environments,
- fetchGroupReleases,
- handleError,
- isGlobalSelectionReady,
- location,
- organization,
- params,
- projects,
- event,
- router,
- ]);
- // Refetch when group is stale
- useEffect(() => {
- if (group) {
- // TODO(ts) This needs a better approach. issueActions is splicing attributes onto
- // group objects to cheat here.
- if ((group as Group & {stale?: boolean}).stale) {
- fetchData();
- return;
- }
- }
- }, [fetchData, group]);
- useTrackView({group, event, project});
- const refetchData = useCallback(() => {
- // Set initial state
- setLoadingGroup(true);
- setError(false);
- setErrorType(null);
- // refetchEvent comes from useApiQuery since event and group data are separately fetched
- refetchEvent();
- fetchData();
- }, [fetchData, refetchEvent]);
- const refetchGroup = useCallback(async () => {
- if (
- group?.status !== ReprocessingStatus.REPROCESSING ||
- loadingGroup ||
- loadingEvent
- ) {
- return;
- }
- setLoadingGroup(true);
- try {
- const updatedGroup = await api.requestPromise(`/issues/${params.groupId}/`, {
- query: getGroupQuery({environments}),
- });
- const reprocessingNewRoute = getReprocessingNewRoute({
- group: updatedGroup,
- event,
- organization,
- router,
- });
- if (reprocessingNewRoute) {
- browserHistory.push(reprocessingNewRoute);
- return;
- }
- setLoadingGroup(false);
- GroupStore.loadInitialData([updatedGroup]);
- } catch (e) {
- handleError(e);
- }
- }, [
- api,
- environments,
- group?.status,
- handleError,
- loadingEvent,
- loadingGroup,
- params.groupId,
- event,
- organization,
- router,
- ]);
- useFetchOnMount({fetchData});
- useRefetchGroupForReprocessing({refetchGroup});
- useEffect(() => {
- return () => {
- GroupStore.reset();
- };
- }, []);
- return {
- project,
- loadingGroup,
- loadingEvent,
- fetchData,
- group,
- event,
- errorType,
- error,
- eventError: isError,
- refetchData,
- refetchGroup,
- };
- }
- function useTrackView({
- group,
- event,
- project,
- }: {
- event: Event | null;
- group: Group | null;
- project: Project | null;
- }) {
- const location = useLocation();
- const {alert_date, alert_rule_id, alert_type, ref_fallback, stream_index, query} =
- location.query;
- useRouteAnalyticsEventNames('issue_details.viewed', 'Issue Details: Viewed');
- useRouteAnalyticsParams({
- ...getAnalyticsDataForGroup(group),
- ...getAnalyticsDataForEvent(event),
- ...getAnalyicsDataForProject(project),
- stream_index: typeof stream_index === 'string' ? Number(stream_index) : undefined,
- query: typeof query === 'string' ? query : undefined,
- // Alert properties track if the user came from email/slack alerts
- alert_date:
- typeof alert_date === 'string' ? getUtcDateString(Number(alert_date)) : undefined,
- alert_rule_id: typeof alert_rule_id === 'string' ? alert_rule_id : undefined,
- alert_type: typeof alert_type === 'string' ? alert_type : undefined,
- ref_fallback,
- // Will be updated by StacktraceLink if there is a stacktrace link
- stacktrace_link_viewed: false,
- // Will be updated by IssueQuickTrace if there is a trace
- trace_status: 'none',
- // Will be updated in GroupDetailsHeader if there are replays
- group_has_replay: false,
- });
- }
- const trackTabChanged = ({
- organization,
- project,
- group,
- event,
- tab,
- }: {
- event: Event | null;
- group: Group;
- organization: Organization;
- project: Project;
- tab: Tab;
- }) => {
- if (!project || !group) {
- return;
- }
- trackAnalytics('issue_details.tab_changed', {
- organization,
- project_id: parseInt(project.id, 10),
- tab,
- ...getAnalyticsDataForGroup(group),
- });
- if (group.issueCategory !== IssueCategory.ERROR) {
- return;
- }
- const analyticsData = event
- ? event.tags
- .filter(({key}) => ['device', 'os', 'browser'].includes(key))
- .reduce((acc, {key, value}) => {
- acc[key] = value;
- return acc;
- }, {})
- : {};
- trackAnalytics('issue_group_details.tab.clicked', {
- organization,
- tab,
- platform: project.platform,
- ...analyticsData,
- });
- };
- function GroupDetailsContentError({
- errorType,
- onRetry,
- }: {
- errorType: Error;
- onRetry: () => void;
- }) {
- const organization = useOrganization();
- const location = useLocation();
- const projectId = location.query.project;
- const {projects} = useProjects();
- const project = projects.find(proj => proj.id === projectId);
- switch (errorType) {
- case ERROR_TYPES.GROUP_NOT_FOUND:
- return (
- <StyledLoadingError
- message={t('The issue you were looking for was not found.')}
- />
- );
- case ERROR_TYPES.MISSING_MEMBERSHIP:
- return <MissingProjectMembership organization={organization} project={project} />;
- default:
- return <StyledLoadingError onRetry={onRetry} />;
- }
- }
- function GroupDetailsContent({
- environments,
- children,
- group,
- project,
- loadingEvent,
- eventError,
- event,
- refetchData,
- }: GroupDetailsContentProps) {
- const organization = useOrganization();
- const router = useRouter();
- const {currentTab, baseUrl} = getCurrentRouteInfo({group, event, router, organization});
- const groupReprocessingStatus = getGroupReprocessingStatus(group);
- useEffect(() => {
- if (
- currentTab === Tab.DETAILS &&
- group &&
- event &&
- group.id !== event?.groupID &&
- !eventError
- ) {
- // if user pastes only the event id into the url, but it's from another group, redirect to correct group/event
- const redirectUrl = `/organizations/${organization.slug}/issues/${event.groupID}/events/${event.id}/`;
- router.push(normalizeUrl(redirectUrl));
- }
- }, [currentTab, event, eventError, group, organization.slug, router]);
- const childProps = {
- environments,
- group,
- project,
- event,
- loadingEvent,
- eventError,
- groupReprocessingStatus,
- onRetry: refetchData,
- baseUrl,
- };
- return (
- <Tabs
- value={currentTab}
- onChange={tab => trackTabChanged({tab, group, project, event, organization})}
- >
- <GroupHeader
- organization={organization}
- groupReprocessingStatus={groupReprocessingStatus}
- event={event ?? undefined}
- group={group}
- baseUrl={baseUrl}
- project={project as Project}
- />
- <GroupTabPanels>
- <TabPanels.Item key={currentTab}>
- {isValidElement(children) ? cloneElement(children, childProps) : children}
- </TabPanels.Item>
- </GroupTabPanels>
- </Tabs>
- );
- }
- function GroupDetailsPageContent(props: GroupDetailsProps & FetchGroupDetailsState) {
- const {
- projects,
- initiallyLoaded: projectsLoaded,
- fetchError: errorFetchingProjects,
- } = useProjects({slugs: props.project?.slug ? [props.project.slug] : []});
- const project =
- (props.project?.slug
- ? projects.find(({slug}) => slug === props.project?.slug)
- : undefined) ?? projects[0];
- if (props.error) {
- return (
- <GroupDetailsContentError errorType={props.errorType} onRetry={props.refetchData} />
- );
- }
- if (errorFetchingProjects) {
- return <StyledLoadingError message={t('Error loading the specified project')} />;
- }
- if (!projectsLoaded || !project || !props.group) {
- return <LoadingIndicator />;
- }
- return <GroupDetailsContent {...props} project={project} group={props.group} />;
- }
- function GroupDetails(props: GroupDetailsProps) {
- const organization = useOrganization();
- const location = useLocation();
- const router = useRouter();
- const {fetchData, project, group, ...fetchGroupDetailsProps} =
- useFetchGroupDetails(props);
- const previousPathname = usePrevious(location.pathname);
- const previousEventId = usePrevious(router.params.eventId);
- const previousIsGlobalSelectionReady = usePrevious(props.isGlobalSelectionReady);
- const {data} = useFetchIssueTagsForDetailsPage(
- {
- groupId: router.params.groupId,
- environment: props.environments,
- },
- // Don't want this query to take precedence over the main requests
- {enabled: props.isGlobalSelectionReady && defined(group)}
- );
- const isSampleError = data?.some(tag => tag.key === 'sample_event') ?? false;
- useEffect(() => {
- const globalSelectionReadyChanged =
- previousIsGlobalSelectionReady !== props.isGlobalSelectionReady;
- if (globalSelectionReadyChanged || location.pathname !== previousPathname) {
- fetchData();
- }
- }, [
- fetchData,
- group,
- location.pathname,
- previousEventId,
- previousIsGlobalSelectionReady,
- previousPathname,
- props.isGlobalSelectionReady,
- router.params.eventId,
- ]);
- const getGroupDetailsTitle = () => {
- const defaultTitle = 'Sentry';
- if (!group) {
- return defaultTitle;
- }
- const {title} = getTitle(group, organization?.features);
- const message = getMessage(group);
- const eventDetails = `${organization.slug} - ${group.project.slug}`;
- if (title && message) {
- return `${title}: ${message} - ${eventDetails}`;
- }
- return `${title || message || defaultTitle} - ${eventDetails}`;
- };
- return (
- <Fragment>
- {isSampleError && project && (
- <SampleEventAlert project={project} organization={organization} />
- )}
- <SentryDocumentTitle noSuffix title={getGroupDetailsTitle()}>
- <PageFiltersContainer skipLoadLastUsed forceProject={project} shouldForceProject>
- <GroupDetailsPageContent
- {...props}
- {...{
- group,
- project,
- fetchData,
- ...fetchGroupDetailsProps,
- }}
- />
- </PageFiltersContainer>
- </SentryDocumentTitle>
- </Fragment>
- );
- }
- export default Sentry.withProfiler(GroupDetails);
- const StyledLoadingError = styled(LoadingError)`
- margin: ${space(2)};
- `;
- const GroupTabPanels = styled(TabPanels)`
- flex-grow: 1;
- display: flex;
- flex-direction: column;
- justify-content: stretch;
- `;
|