vitalDetailContent.tsx 9.9 KB

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