content.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Alert} from 'sentry/components/alert';
  4. import FeatureBadge from 'sentry/components/badge/featureBadge';
  5. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  6. import * as Layout from 'sentry/components/layouts/thirds';
  7. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  8. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  9. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  10. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  11. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  12. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  13. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import {browserHistory} from 'sentry/utils/browserHistory';
  17. import {decodeInteger} from 'sentry/utils/queryString';
  18. import {useLocation} from 'sentry/utils/useLocation';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import {ExploreContent} from 'sentry/views/explore/content';
  21. import * as ModuleLayout from 'sentry/views/insights/common/components/moduleLayout';
  22. import {usePageParams} from './hooks/usePageParams';
  23. import {useTraces} from './hooks/useTraces';
  24. import {TracesChart} from './tracesChart';
  25. import {TracesSearchBar} from './tracesSearchBar';
  26. import {TracesTable} from './tracesTable';
  27. import {normalizeTraces} from './utils';
  28. const TRACE_EXPLORER_DOCS_URL = 'https://docs.sentry.io/product/explore/traces/';
  29. const DEFAULT_STATS_PERIOD = '24h';
  30. const DEFAULT_PER_PAGE = 50;
  31. export default function Wrapper(props: any) {
  32. const location = useLocation();
  33. const organization = useOrganization();
  34. if (
  35. location.query.view !== 'trace' &&
  36. organization.features.includes('visibility-explore-view')
  37. ) {
  38. return <ExploreContent {...props} />;
  39. }
  40. return <Content {...props} />;
  41. }
  42. function Content() {
  43. const location = useLocation();
  44. const organization = useOrganization();
  45. const limit = useMemo(() => {
  46. return decodeInteger(location.query.perPage, DEFAULT_PER_PAGE);
  47. }, [location.query.perPage]);
  48. const {queries} = usePageParams(location);
  49. const handleSearch = useCallback(
  50. (searchIndex: number, searchQuery: string) => {
  51. const newQueries = [...queries];
  52. if (newQueries.length === 0) {
  53. // In the odd case someone wants to add search bars before any query has been made, we add both the default one shown and a new one.
  54. newQueries[0] = '';
  55. }
  56. newQueries[searchIndex] = searchQuery;
  57. browserHistory.push({
  58. ...location,
  59. query: {
  60. ...location.query,
  61. cursor: undefined,
  62. query: typeof searchQuery === 'string' ? newQueries : queries,
  63. },
  64. });
  65. },
  66. [location, queries]
  67. );
  68. const handleClearSearch = useCallback(
  69. (searchIndex: number) => {
  70. const newQueries = [...queries];
  71. // TODO: do we need to return false when `newQueries[searchIndex] === undefined`?
  72. delete newQueries[searchIndex];
  73. browserHistory.push({
  74. ...location,
  75. query: {
  76. ...location.query,
  77. cursor: undefined,
  78. query: newQueries,
  79. },
  80. });
  81. return true;
  82. },
  83. [location, queries]
  84. );
  85. const tracesQuery = useTraces({
  86. limit,
  87. query: queries,
  88. });
  89. const isLoading = tracesQuery.isFetching;
  90. const isError = !isLoading && tracesQuery.isError;
  91. const isEmpty = !isLoading && !isError && (tracesQuery?.data?.data?.length ?? 0) === 0;
  92. const rawData = !isLoading && !isError ? tracesQuery?.data?.data : undefined;
  93. const data = normalizeTraces(rawData);
  94. return (
  95. <SentryDocumentTitle title={t('Traces')} orgSlug={organization.slug}>
  96. <PageFiltersContainer
  97. defaultSelection={{
  98. datetime: {start: null, end: null, utc: null, period: DEFAULT_STATS_PERIOD},
  99. }}
  100. >
  101. <Layout.Page>
  102. <Layout.Header>
  103. <Layout.HeaderContent>
  104. <HeaderContentBar>
  105. <Layout.Title>
  106. {t('Traces')}
  107. <PageHeadingQuestionTooltip
  108. docsUrl={TRACE_EXPLORER_DOCS_URL}
  109. title={t(
  110. 'Traces lets you search for individual spans that make up a trace, linked by a trace id.'
  111. )}
  112. />
  113. <FeatureBadge type="beta" />
  114. </Layout.Title>
  115. <FeedbackWidgetButton />
  116. </HeaderContentBar>
  117. </Layout.HeaderContent>
  118. </Layout.Header>
  119. <Layout.Body>
  120. <LayoutMain fullWidth>
  121. <PageFilterBar condensed>
  122. <ProjectPageFilter />
  123. <EnvironmentPageFilter />
  124. <DatePageFilter defaultPeriod="2h" />
  125. </PageFilterBar>
  126. {isError && typeof tracesQuery.error?.responseJSON?.detail === 'string' ? (
  127. <StyledAlert type="error" showIcon>
  128. {tracesQuery.error?.responseJSON?.detail}
  129. </StyledAlert>
  130. ) : null}
  131. <TracesSearchBar
  132. queries={queries}
  133. handleSearch={handleSearch}
  134. handleClearSearch={handleClearSearch}
  135. />
  136. <ModuleLayout.Full>
  137. <TracesChart />
  138. </ModuleLayout.Full>
  139. <TracesTable
  140. isEmpty={isEmpty}
  141. isError={isError}
  142. isLoading={isLoading}
  143. queries={queries}
  144. data={data}
  145. />
  146. </LayoutMain>
  147. </Layout.Body>
  148. </Layout.Page>
  149. </PageFiltersContainer>
  150. </SentryDocumentTitle>
  151. );
  152. }
  153. const HeaderContentBar = styled('div')`
  154. display: flex;
  155. align-items: center;
  156. justify-content: space-between;
  157. flex-direction: row;
  158. `;
  159. const LayoutMain = styled(Layout.Main)`
  160. display: flex;
  161. flex-direction: column;
  162. gap: ${space(2)};
  163. `;
  164. const StyledAlert = styled(Alert)`
  165. margin-bottom: 0;
  166. `;