content.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import {Fragment, useCallback, useEffect, useMemo} from 'react';
  2. import {browserHistory, InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {openModal} from 'sentry/actionCreators/modal';
  6. import Button from 'sentry/components/button';
  7. import DatePageFilter from 'sentry/components/datePageFilter';
  8. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import NoProjectMessage from 'sentry/components/noProjectMessage';
  11. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  12. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  13. import PageHeading from 'sentry/components/pageHeading';
  14. import Pagination from 'sentry/components/pagination';
  15. import {ProfileTransactionsTable} from 'sentry/components/profiling/profileTransactionsTable';
  16. import {ProfilingOnboardingModal} from 'sentry/components/profiling/ProfilingOnboarding/profilingOnboardingModal';
  17. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  18. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  19. import SmartSearchBar, {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  20. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  21. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  22. import {t} from 'sentry/locale';
  23. import {PageContent} from 'sentry/styles/organization';
  24. import space from 'sentry/styles/space';
  25. import {Project} from 'sentry/types';
  26. import {PageFilters} from 'sentry/types/core';
  27. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  28. import {useProfileFilters} from 'sentry/utils/profiling/hooks/useProfileFilters';
  29. import {useProfileTransactions} from 'sentry/utils/profiling/hooks/useProfileTransactions';
  30. import {decodeScalar} from 'sentry/utils/queryString';
  31. import useOrganization from 'sentry/utils/useOrganization';
  32. import usePageFilters from 'sentry/utils/usePageFilters';
  33. import useProjects from 'sentry/utils/useProjects';
  34. import {ProfileCharts} from './landing/profileCharts';
  35. import {ProfilingOnboardingPanel} from './profilingOnboardingPanel';
  36. function hasSetupProfilingForAtLeastOneProject(
  37. selectedProjects: PageFilters['projects'],
  38. projects: Project[]
  39. ): boolean {
  40. const projectIDsToProjectTable = projects.reduce<Record<string, Project>>(
  41. (acc, project) => {
  42. acc[project.id] = project;
  43. return acc;
  44. },
  45. {}
  46. );
  47. if (selectedProjects[0] === ALL_ACCESS_PROJECTS || selectedProjects.length === 0) {
  48. const projectWithProfiles = projects.find(p => {
  49. const project = projectIDsToProjectTable[String(p)];
  50. if (!project) {
  51. // Shouldnt happen, but lets be safe and just not do anything
  52. return false;
  53. }
  54. return project.hasProfiles;
  55. });
  56. return projectWithProfiles !== undefined;
  57. }
  58. const projectWithProfiles = selectedProjects.find(p => {
  59. const project = projectIDsToProjectTable[String(p)];
  60. if (!project) {
  61. // Shouldnt happen, but lets be safe and just not do anything
  62. return false;
  63. }
  64. return project.hasProfiles;
  65. });
  66. return projectWithProfiles !== undefined;
  67. }
  68. interface ProfilingContentProps {
  69. location: Location;
  70. router: InjectedRouter;
  71. }
  72. function ProfilingContent({location, router}: ProfilingContentProps) {
  73. const organization = useOrganization();
  74. const {selection} = usePageFilters();
  75. const cursor = decodeScalar(location.query.cursor);
  76. const query = decodeScalar(location.query.query, '');
  77. const transactionsSort = decodeScalar(location.query.sort, '-count()');
  78. const profileFilters = useProfileFilters({query: '', selection});
  79. const transactions = useProfileTransactions({
  80. cursor,
  81. query,
  82. selection,
  83. sort: transactionsSort,
  84. });
  85. const {projects} = useProjects();
  86. useEffect(() => {
  87. trackAdvancedAnalyticsEvent('profiling_views.landing', {
  88. organization,
  89. });
  90. }, [organization]);
  91. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  92. (searchQuery: string) => {
  93. browserHistory.push({
  94. ...location,
  95. query: {
  96. ...location.query,
  97. cursor: undefined,
  98. query: searchQuery || undefined,
  99. },
  100. });
  101. },
  102. [location]
  103. );
  104. // Open the modal on demand
  105. const onSetupProfilingClick = useCallback(() => {
  106. openModal(props => {
  107. return <ProfilingOnboardingModal {...props} organization={organization} />;
  108. });
  109. }, [organization]);
  110. const shouldShowProfilingOnboardingPanel = useMemo((): boolean => {
  111. if (transactions.type !== 'resolved') {
  112. return false;
  113. }
  114. if (transactions.data.transactions.length > 0) {
  115. return false;
  116. }
  117. return !hasSetupProfilingForAtLeastOneProject(selection.projects, projects);
  118. }, [selection.projects, projects, transactions]);
  119. return (
  120. <SentryDocumentTitle title={t('Profiling')} orgSlug={organization.slug}>
  121. <PageFiltersContainer>
  122. <NoProjectMessage organization={organization}>
  123. <StyledPageContent>
  124. <Layout.Header>
  125. <StyledLayoutHeaderContent>
  126. <StyledHeading>{t('Profiling')}</StyledHeading>
  127. <HeadingActions>
  128. <Button onClick={onSetupProfilingClick}>{t('Set Up Profiling')}</Button>
  129. <Button
  130. priority="primary"
  131. href="https://discord.gg/zrMjKA4Vnz"
  132. external
  133. onClick={() => {
  134. trackAdvancedAnalyticsEvent(
  135. 'profiling_views.visit_discord_channel',
  136. {
  137. organization,
  138. }
  139. );
  140. }}
  141. >
  142. {t('Join Discord')}
  143. </Button>
  144. </HeadingActions>
  145. </StyledLayoutHeaderContent>
  146. </Layout.Header>
  147. <Layout.Body>
  148. <Layout.Main fullWidth>
  149. <ActionBar>
  150. <PageFilterBar condensed>
  151. <ProjectPageFilter />
  152. <EnvironmentPageFilter />
  153. <DatePageFilter alignDropdown="left" />
  154. </PageFilterBar>
  155. <SmartSearchBar
  156. organization={organization}
  157. hasRecentSearches
  158. searchSource="profile_landing"
  159. supportedTags={profileFilters}
  160. query={query}
  161. onSearch={handleSearch}
  162. maxQueryLength={MAX_QUERY_LENGTH}
  163. />
  164. </ActionBar>
  165. {shouldShowProfilingOnboardingPanel ? (
  166. <ProfilingOnboardingPanel>
  167. <Button href="https://docs.sentry.io/product/profiling/" external>
  168. {t('Read Docs')}
  169. </Button>
  170. <Button onClick={onSetupProfilingClick} priority="primary">
  171. {t('Set Up Profiling')}
  172. </Button>
  173. </ProfilingOnboardingPanel>
  174. ) : (
  175. <Fragment>
  176. <ProfileCharts router={router} query={query} selection={selection} />
  177. <ProfileTransactionsTable
  178. error={
  179. transactions.type === 'errored'
  180. ? t('Unable to load profiles')
  181. : null
  182. }
  183. isLoading={transactions.type === 'loading'}
  184. sort={transactionsSort}
  185. transactions={
  186. transactions.type === 'resolved'
  187. ? transactions.data.transactions
  188. : []
  189. }
  190. />
  191. <Pagination
  192. pageLinks={
  193. transactions.type === 'resolved'
  194. ? transactions.data.pageLinks
  195. : null
  196. }
  197. />
  198. </Fragment>
  199. )}
  200. </Layout.Main>
  201. </Layout.Body>
  202. </StyledPageContent>
  203. </NoProjectMessage>
  204. </PageFiltersContainer>
  205. </SentryDocumentTitle>
  206. );
  207. }
  208. const StyledPageContent = styled(PageContent)`
  209. padding: 0;
  210. `;
  211. const StyledLayoutHeaderContent = styled(Layout.HeaderContent)`
  212. display: flex;
  213. justify-content: space-between;
  214. flex-direction: row;
  215. `;
  216. const HeadingActions = styled('div')`
  217. display: flex;
  218. align-items: center;
  219. button:not(:last-child) {
  220. margin-right: ${space(1)};
  221. }
  222. `;
  223. const StyledHeading = styled(PageHeading)`
  224. line-height: 40px;
  225. `;
  226. const ActionBar = styled('div')`
  227. display: grid;
  228. gap: ${space(2)};
  229. grid-template-columns: min-content auto;
  230. margin-bottom: ${space(2)};
  231. `;
  232. export default ProfilingContent;