vitalDetailContent.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import {Fragment, useState} from 'react';
  2. import type {InjectedRouter} from 'react-router';
  3. import {browserHistory} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import type {Location} from 'history';
  6. import omit from 'lodash/omit';
  7. import type {Client} from 'sentry/api';
  8. import Feature from 'sentry/components/acl/feature';
  9. import {Alert} from 'sentry/components/alert';
  10. import ButtonBar from 'sentry/components/buttonBar';
  11. import {getInterval} from 'sentry/components/charts/utils';
  12. import {CreateAlertFromViewButton} from 'sentry/components/createAlertButton';
  13. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  14. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  15. import SearchBar from 'sentry/components/events/searchBar';
  16. import * as Layout from 'sentry/components/layouts/thirds';
  17. import LoadingIndicator from 'sentry/components/loadingIndicator';
  18. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  19. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  20. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  21. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  22. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  23. import * as TeamKeyTransactionManager from 'sentry/components/performance/teamKeyTransactionsManager';
  24. import {IconCheckmark, IconClose} from 'sentry/icons';
  25. import {t} from 'sentry/locale';
  26. import {space} from 'sentry/styles/space';
  27. import type {Organization, Project} from 'sentry/types';
  28. import {trackAnalytics} from 'sentry/utils/analytics';
  29. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  30. import type EventView from 'sentry/utils/discover/eventView';
  31. import {WebVital} from 'sentry/utils/fields';
  32. import {Browser} from 'sentry/utils/performance/vitals/constants';
  33. import {decodeScalar} from 'sentry/utils/queryString';
  34. import Teams from 'sentry/utils/teams';
  35. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  36. import withProjects from 'sentry/utils/withProjects';
  37. import Breadcrumb from '../breadcrumb';
  38. import {getTransactionSearchQuery} from '../utils';
  39. import Table from './table';
  40. import {
  41. vitalAbbreviations,
  42. vitalAlertTypes,
  43. vitalDescription,
  44. vitalMap,
  45. vitalSupportedBrowsers,
  46. } from './utils';
  47. import VitalChart from './vitalChart';
  48. import VitalInfo from './vitalInfo';
  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. trackAnalytics('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. <DropdownMenu
  130. items={items}
  131. triggerLabel={vitalAbbreviations[vitalName]}
  132. triggerProps={{
  133. 'aria-label': `Web Vitals: ${vitalAbbreviations[vitalName]}`,
  134. prefix: t('Web Vitals'),
  135. }}
  136. position="bottom-start"
  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 />
  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. <VitalChart
  181. organization={organization}
  182. query={query}
  183. project={project}
  184. environment={environment}
  185. start={localDateStart}
  186. end={localDateEnd}
  187. statsPeriod={statsPeriod}
  188. interval={interval}
  189. />
  190. <StyledVitalInfo>
  191. <VitalInfo
  192. orgSlug={orgSlug}
  193. location={location}
  194. vital={vital}
  195. project={project}
  196. environment={environment}
  197. start={start}
  198. end={end}
  199. statsPeriod={statsPeriod}
  200. />
  201. </StyledVitalInfo>
  202. <Teams provideUserTeams>
  203. {({teams, initiallyLoaded}) =>
  204. initiallyLoaded ? (
  205. <TeamKeyTransactionManager.Provider
  206. organization={organization}
  207. teams={teams}
  208. selectedTeams={['myteams']}
  209. selectedProjects={project.map(String)}
  210. >
  211. <Table
  212. eventView={eventView}
  213. projects={projects}
  214. organization={organization}
  215. location={location}
  216. setError={setError}
  217. summaryConditions={summaryConditions}
  218. />
  219. </TeamKeyTransactionManager.Provider>
  220. ) : (
  221. <LoadingIndicator />
  222. )
  223. }
  224. </Teams>
  225. </Fragment>
  226. );
  227. }
  228. const {location, organization, vitalName} = props;
  229. const vital = vitalName || WebVital.LCP;
  230. return (
  231. <Fragment>
  232. <Layout.Header>
  233. <Layout.HeaderContent>
  234. <Breadcrumb organization={organization} location={location} vitalName={vital} />
  235. <Layout.Title>{vitalMap[vital]}</Layout.Title>
  236. </Layout.HeaderContent>
  237. <Layout.HeaderActions>
  238. <ButtonBar gap={1}>
  239. {renderVitalSwitcher()}
  240. <Feature organization={organization} features="incidents">
  241. {({hasFeature}) => hasFeature && renderCreateAlertButton()}
  242. </Feature>
  243. </ButtonBar>
  244. </Layout.HeaderActions>
  245. </Layout.Header>
  246. <Layout.Body>
  247. {renderError()}
  248. <Layout.Main fullWidth>
  249. <StyledDescription>{vitalDescription[vitalName]}</StyledDescription>
  250. <SupportedBrowsers>
  251. {Object.values(Browser).map(browser => (
  252. <BrowserItem key={browser}>
  253. {vitalSupportedBrowsers[vitalName]?.includes(browser) ? (
  254. <IconCheckmark color="successText" size="sm" />
  255. ) : (
  256. <IconClose color="dangerText" size="sm" />
  257. )}
  258. {browser}
  259. </BrowserItem>
  260. ))}
  261. </SupportedBrowsers>
  262. {renderContent(vital)}
  263. </Layout.Main>
  264. </Layout.Body>
  265. </Fragment>
  266. );
  267. }
  268. export default withProjects(VitalDetailContent);
  269. const StyledDescription = styled('div')`
  270. font-size: ${p => p.theme.fontSizeMedium};
  271. margin-bottom: ${space(3)};
  272. `;
  273. const StyledVitalInfo = styled('div')`
  274. margin-bottom: ${space(3)};
  275. `;
  276. const SupportedBrowsers = styled('div')`
  277. display: inline-flex;
  278. gap: ${space(2)};
  279. margin-bottom: ${space(3)};
  280. `;
  281. const BrowserItem = styled('div')`
  282. display: flex;
  283. align-items: center;
  284. gap: ${space(1)};
  285. `;
  286. const FilterActions = styled('div')`
  287. display: grid;
  288. gap: ${space(2)};
  289. margin-bottom: ${space(2)};
  290. @media (min-width: ${p => p.theme.breakpoints.small}) {
  291. grid-template-columns: auto 1fr;
  292. }
  293. `;