vitalDetailContent.tsx 9.8 KB

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