vitalDetailContent.tsx 9.8 KB

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