content.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 size="sm" onClick={onSetupProfilingClick}>
  129. {t('Set Up Profiling')}
  130. </Button>
  131. <Button
  132. size="sm"
  133. priority="primary"
  134. href="https://discord.gg/zrMjKA4Vnz"
  135. external
  136. onClick={() => {
  137. trackAdvancedAnalyticsEvent(
  138. 'profiling_views.visit_discord_channel',
  139. {
  140. organization,
  141. }
  142. );
  143. }}
  144. >
  145. {t('Join Discord')}
  146. </Button>
  147. </HeadingActions>
  148. </StyledLayoutHeaderContent>
  149. </Layout.Header>
  150. <Layout.Body>
  151. <Layout.Main fullWidth>
  152. <ActionBar>
  153. <PageFilterBar condensed>
  154. <ProjectPageFilter />
  155. <EnvironmentPageFilter />
  156. <DatePageFilter alignDropdown="left" />
  157. </PageFilterBar>
  158. <SmartSearchBar
  159. organization={organization}
  160. hasRecentSearches
  161. searchSource="profile_landing"
  162. supportedTags={profileFilters}
  163. query={query}
  164. onSearch={handleSearch}
  165. maxQueryLength={MAX_QUERY_LENGTH}
  166. />
  167. </ActionBar>
  168. {shouldShowProfilingOnboardingPanel ? (
  169. <ProfilingOnboardingPanel>
  170. <Button href="https://docs.sentry.io/product/profiling/" external>
  171. {t('Read Docs')}
  172. </Button>
  173. <Button onClick={onSetupProfilingClick} priority="primary">
  174. {t('Set Up Profiling')}
  175. </Button>
  176. </ProfilingOnboardingPanel>
  177. ) : (
  178. <Fragment>
  179. <ProfileCharts router={router} query={query} selection={selection} />
  180. <ProfileTransactionsTable
  181. error={
  182. transactions.type === 'errored'
  183. ? t('Unable to load profiles')
  184. : null
  185. }
  186. isLoading={transactions.type === 'loading'}
  187. sort={transactionsSort}
  188. transactions={
  189. transactions.type === 'resolved'
  190. ? transactions.data.transactions
  191. : []
  192. }
  193. />
  194. <Pagination
  195. pageLinks={
  196. transactions.type === 'resolved'
  197. ? transactions.data.pageLinks
  198. : null
  199. }
  200. />
  201. </Fragment>
  202. )}
  203. </Layout.Main>
  204. </Layout.Body>
  205. </StyledPageContent>
  206. </NoProjectMessage>
  207. </PageFiltersContainer>
  208. </SentryDocumentTitle>
  209. );
  210. }
  211. const StyledPageContent = styled(PageContent)`
  212. padding: 0;
  213. `;
  214. const StyledLayoutHeaderContent = styled(Layout.HeaderContent)`
  215. display: flex;
  216. justify-content: space-between;
  217. flex-direction: row;
  218. `;
  219. const HeadingActions = styled('div')`
  220. display: flex;
  221. align-items: center;
  222. button:not(:last-child) {
  223. margin-right: ${space(1)};
  224. }
  225. `;
  226. const StyledHeading = styled(PageHeading)`
  227. line-height: 40px;
  228. `;
  229. const ActionBar = styled('div')`
  230. display: grid;
  231. gap: ${space(2)};
  232. grid-template-columns: min-content auto;
  233. margin-bottom: ${space(2)};
  234. `;
  235. export default ProfilingContent;