vitalDetailContent.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import {Fragment, useState} from 'react';
  2. import {browserHistory, InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import omit from 'lodash/omit';
  6. import {Client} from 'sentry/api';
  7. import Feature from 'sentry/components/acl/feature';
  8. import Alert from 'sentry/components/alert';
  9. import ButtonBar from 'sentry/components/buttonBar';
  10. import {getInterval} from 'sentry/components/charts/utils';
  11. import {CreateAlertFromViewButton} from 'sentry/components/createAlertButton';
  12. import DatePageFilter from 'sentry/components/datePageFilter';
  13. import DropdownMenuControl from 'sentry/components/dropdownMenuControl';
  14. import {MenuItemProps} from 'sentry/components/dropdownMenuItem';
  15. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  16. import SearchBar from 'sentry/components/events/searchBar';
  17. import * as Layout from 'sentry/components/layouts/thirds';
  18. import LoadingIndicator from 'sentry/components/loadingIndicator';
  19. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  20. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  21. import * as TeamKeyTransactionManager from 'sentry/components/performance/teamKeyTransactionsManager';
  22. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  23. import {IconCheckmark, IconClose} from 'sentry/icons';
  24. import {t} from 'sentry/locale';
  25. import space from 'sentry/styles/space';
  26. import {Organization, Project} from 'sentry/types';
  27. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  28. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  29. import EventView from 'sentry/utils/discover/eventView';
  30. import {WebVital} from 'sentry/utils/fields';
  31. import {Browser} from 'sentry/utils/performance/vitals/constants';
  32. import {decodeScalar} from 'sentry/utils/queryString';
  33. import Teams from 'sentry/utils/teams';
  34. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  35. import withProjects from 'sentry/utils/withProjects';
  36. import Breadcrumb from '../breadcrumb';
  37. import {getTransactionSearchQuery} from '../utils';
  38. import Table from './table';
  39. import {
  40. vitalAbbreviations,
  41. vitalAlertTypes,
  42. vitalDescription,
  43. vitalMap,
  44. vitalSupportedBrowsers,
  45. } from './utils';
  46. import VitalChart from './vitalChart';
  47. import VitalInfo from './vitalInfo';
  48. import VitalsComparison from './vitalsComparison';
  49. const FRONTEND_VITALS = [WebVital.FCP, WebVital.LCP, WebVital.FID, WebVital.CLS];
  50. type Props = {
  51. api: Client;
  52. eventView: EventView;
  53. location: Location;
  54. organization: Organization;
  55. projects: Project[];
  56. router: InjectedRouter;
  57. vitalName: WebVital;
  58. };
  59. function getSummaryConditions(query: string) {
  60. const parsed = new MutableSearch(query);
  61. parsed.freeText = [];
  62. return parsed.formatString();
  63. }
  64. function VitalDetailContent(props: Props) {
  65. const [error, setError] = useState<string | undefined>(undefined);
  66. function handleSearch(query: string) {
  67. const {location} = props;
  68. const queryParams = normalizeDateTimeParams({
  69. ...(location.query || {}),
  70. query,
  71. });
  72. // do not propagate pagination when making a new search
  73. const searchQueryParams = omit(queryParams, 'cursor');
  74. browserHistory.push({
  75. pathname: location.pathname,
  76. query: searchQueryParams,
  77. });
  78. }
  79. function renderCreateAlertButton() {
  80. const {eventView, organization, projects, vitalName} = props;
  81. return (
  82. <CreateAlertFromViewButton
  83. eventView={eventView}
  84. organization={organization}
  85. projects={projects}
  86. aria-label={t('Create Alert')}
  87. alertType={vitalAlertTypes[vitalName]}
  88. referrer="performance"
  89. />
  90. );
  91. }
  92. function renderVitalSwitcher() {
  93. const {vitalName, location, organization} = props;
  94. const position = FRONTEND_VITALS.indexOf(vitalName);
  95. if (position < 0) {
  96. return null;
  97. }
  98. const items: MenuItemProps[] = FRONTEND_VITALS.reduce(
  99. (acc: MenuItemProps[], newVitalName) => {
  100. const itemProps = {
  101. key: newVitalName,
  102. label: vitalAbbreviations[newVitalName],
  103. onAction: function switchWebVital() {
  104. browserHistory.push({
  105. pathname: location.pathname,
  106. query: {
  107. ...location.query,
  108. vitalName: newVitalName,
  109. cursor: undefined,
  110. },
  111. });
  112. trackAdvancedAnalyticsEvent('performance_views.vital_detail.switch_vital', {
  113. organization,
  114. from_vital: vitalAbbreviations[vitalName] ?? 'undefined',
  115. to_vital: vitalAbbreviations[newVitalName] ?? 'undefined',
  116. });
  117. },
  118. };
  119. if (vitalName === newVitalName) {
  120. acc.unshift(itemProps);
  121. } else {
  122. acc.push(itemProps);
  123. }
  124. return acc;
  125. },
  126. []
  127. );
  128. return (
  129. <DropdownMenuControl
  130. items={items}
  131. triggerLabel={vitalAbbreviations[vitalName]}
  132. triggerProps={{
  133. 'aria-label': `Web Vitals: ${vitalAbbreviations[vitalName]}`,
  134. prefix: t('Web Vitals'),
  135. }}
  136. placement="bottom left"
  137. />
  138. );
  139. }
  140. function renderError() {
  141. if (!error) {
  142. return null;
  143. }
  144. return (
  145. <Alert type="error" showIcon>
  146. {error}
  147. </Alert>
  148. );
  149. }
  150. function renderContent(vital: WebVital) {
  151. const {location, organization, eventView, projects} = props;
  152. const {fields, start, end, statsPeriod, environment, project} = eventView;
  153. const query = decodeScalar(location.query.query, '');
  154. const orgSlug = organization.slug;
  155. const localDateStart = start ? getUtcToLocalDateObject(start) : null;
  156. const localDateEnd = end ? getUtcToLocalDateObject(end) : null;
  157. const interval = getInterval(
  158. {start: localDateStart, end: localDateEnd, period: statsPeriod},
  159. 'high'
  160. );
  161. const filterString = getTransactionSearchQuery(location);
  162. const summaryConditions = getSummaryConditions(filterString);
  163. return (
  164. <Fragment>
  165. <FilterActions>
  166. <PageFilterBar condensed>
  167. <ProjectPageFilter />
  168. <EnvironmentPageFilter />
  169. <DatePageFilter alignDropdown="left" />
  170. </PageFilterBar>
  171. <SearchBar
  172. searchSource="performance_vitals"
  173. organization={organization}
  174. projectIds={project}
  175. query={query}
  176. fields={fields}
  177. onSearch={handleSearch}
  178. />
  179. </FilterActions>
  180. {organization.experiments.VitalsAlertExperiment ? (
  181. <VitalsComparison
  182. {...{
  183. organization,
  184. location,
  185. vital,
  186. project,
  187. end,
  188. environment,
  189. statsPeriod,
  190. start,
  191. }}
  192. />
  193. ) : null}
  194. <VitalChart
  195. organization={organization}
  196. query={query}
  197. project={project}
  198. environment={environment}
  199. start={localDateStart}
  200. end={localDateEnd}
  201. statsPeriod={statsPeriod}
  202. interval={interval}
  203. />
  204. <StyledVitalInfo>
  205. <VitalInfo
  206. orgSlug={orgSlug}
  207. location={location}
  208. vital={vital}
  209. project={project}
  210. environment={environment}
  211. start={start}
  212. end={end}
  213. statsPeriod={statsPeriod}
  214. />
  215. </StyledVitalInfo>
  216. <Teams provideUserTeams>
  217. {({teams, initiallyLoaded}) =>
  218. initiallyLoaded ? (
  219. <TeamKeyTransactionManager.Provider
  220. organization={organization}
  221. teams={teams}
  222. selectedTeams={['myteams']}
  223. selectedProjects={project.map(String)}
  224. >
  225. <Table
  226. eventView={eventView}
  227. projects={projects}
  228. organization={organization}
  229. location={location}
  230. setError={setError}
  231. summaryConditions={summaryConditions}
  232. />
  233. </TeamKeyTransactionManager.Provider>
  234. ) : (
  235. <LoadingIndicator />
  236. )
  237. }
  238. </Teams>
  239. </Fragment>
  240. );
  241. }
  242. const {location, organization, vitalName} = props;
  243. const vital = vitalName || WebVital.LCP;
  244. return (
  245. <Fragment>
  246. <Layout.Header>
  247. <Layout.HeaderContent>
  248. <Breadcrumb organization={organization} location={location} vitalName={vital} />
  249. <Layout.Title>{vitalMap[vital]}</Layout.Title>
  250. </Layout.HeaderContent>
  251. <Layout.HeaderActions>
  252. <ButtonBar gap={1}>
  253. {renderVitalSwitcher()}
  254. <Feature organization={organization} features={['incidents']}>
  255. {({hasFeature}) => hasFeature && renderCreateAlertButton()}
  256. </Feature>
  257. </ButtonBar>
  258. </Layout.HeaderActions>
  259. </Layout.Header>
  260. <Layout.Body>
  261. {renderError()}
  262. <Layout.Main fullWidth>
  263. <StyledDescription>{vitalDescription[vitalName]}</StyledDescription>
  264. <SupportedBrowsers>
  265. {Object.values(Browser).map(browser => (
  266. <BrowserItem key={browser}>
  267. {vitalSupportedBrowsers[vitalName]?.includes(browser) ? (
  268. <IconCheckmark color="green300" size="sm" />
  269. ) : (
  270. <IconClose color="red300" size="sm" />
  271. )}
  272. {browser}
  273. </BrowserItem>
  274. ))}
  275. </SupportedBrowsers>
  276. {renderContent(vital)}
  277. </Layout.Main>
  278. </Layout.Body>
  279. </Fragment>
  280. );
  281. }
  282. export default withProjects(VitalDetailContent);
  283. const StyledDescription = styled('div')`
  284. font-size: ${p => p.theme.fontSizeMedium};
  285. margin-bottom: ${space(3)};
  286. `;
  287. const StyledVitalInfo = styled('div')`
  288. margin-bottom: ${space(3)};
  289. `;
  290. const SupportedBrowsers = styled('div')`
  291. display: inline-flex;
  292. gap: ${space(2)};
  293. margin-bottom: ${space(3)};
  294. `;
  295. const BrowserItem = styled('div')`
  296. display: flex;
  297. align-items: center;
  298. gap: ${space(1)};
  299. `;
  300. const FilterActions = styled('div')`
  301. display: grid;
  302. gap: ${space(2)};
  303. margin-bottom: ${space(2)};
  304. @media (min-width: ${p => p.theme.breakpoints.small}) {
  305. grid-template-columns: auto 1fr;
  306. }
  307. `;