index.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. import {Fragment} from 'react';
  2. import {forceCheck} from 'react-lazyload';
  3. import {RouteComponentProps} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import pick from 'lodash/pick';
  6. import {fetchTagValues} from 'sentry/actionCreators/tags';
  7. import Alert from 'sentry/components/alert';
  8. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  9. import DatePageFilter from 'sentry/components/datePageFilter';
  10. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  11. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  12. import ExternalLink from 'sentry/components/links/externalLink';
  13. import LoadingIndicator from 'sentry/components/loadingIndicator';
  14. import NoProjectMessage from 'sentry/components/noProjectMessage';
  15. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  16. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  17. import {getRelativeSummary} from 'sentry/components/organizations/timeRangeSelector/utils';
  18. import PageHeading from 'sentry/components/pageHeading';
  19. import Pagination from 'sentry/components/pagination';
  20. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  21. import SmartSearchBar from 'sentry/components/smartSearchBar';
  22. import {ItemType} from 'sentry/components/smartSearchBar/types';
  23. import {DEFAULT_STATS_PERIOD} from 'sentry/constants';
  24. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  25. import {releaseHealth} from 'sentry/data/platformCategories';
  26. import {t} from 'sentry/locale';
  27. import ProjectsStore from 'sentry/stores/projectsStore';
  28. import {PageContent, PageHeader} from 'sentry/styles/organization';
  29. import space from 'sentry/styles/space';
  30. import {
  31. Organization,
  32. PageFilters,
  33. Project,
  34. Release,
  35. ReleaseStatus,
  36. Tag,
  37. } from 'sentry/types';
  38. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  39. import {SEMVER_TAGS} from 'sentry/utils/discover/fields';
  40. import Projects from 'sentry/utils/projects';
  41. import routeTitleGen from 'sentry/utils/routeTitle';
  42. import withOrganization from 'sentry/utils/withOrganization';
  43. import withPageFilters from 'sentry/utils/withPageFilters';
  44. import withProjects from 'sentry/utils/withProjects';
  45. import AsyncView from 'sentry/views/asyncView';
  46. import ReleaseArchivedNotice from '../detail/overview/releaseArchivedNotice';
  47. import {isMobileRelease} from '../utils';
  48. import ReleaseCard from './releaseCard';
  49. import ReleasesAdoptionChart from './releasesAdoptionChart';
  50. import ReleasesDisplayOptions, {ReleasesDisplayOption} from './releasesDisplayOptions';
  51. import ReleasesPromo from './releasesPromo';
  52. import ReleasesRequest from './releasesRequest';
  53. import ReleasesSortOptions, {ReleasesSortOption} from './releasesSortOptions';
  54. import ReleasesStatusOptions, {ReleasesStatusOption} from './releasesStatusOptions';
  55. type RouteParams = {
  56. orgId: string;
  57. };
  58. type Props = RouteComponentProps<RouteParams, {}> & {
  59. organization: Organization;
  60. projects: Project[];
  61. selection: PageFilters;
  62. };
  63. type State = {
  64. releases: Release[];
  65. } & AsyncView['state'];
  66. class ReleasesList extends AsyncView<Props, State> {
  67. shouldReload = true;
  68. shouldRenderBadRequests = true;
  69. getTitle() {
  70. return routeTitleGen(t('Releases'), this.props.organization.slug, false);
  71. }
  72. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  73. const {organization, location} = this.props;
  74. const {statsPeriod} = location.query;
  75. const activeSort = this.getSort();
  76. const activeStatus = this.getStatus();
  77. const query = {
  78. ...pick(location.query, ['project', 'environment', 'cursor', 'query', 'sort']),
  79. summaryStatsPeriod: statsPeriod,
  80. per_page: 20,
  81. flatten: activeSort === ReleasesSortOption.DATE ? 0 : 1,
  82. adoptionStages: 1,
  83. status:
  84. activeStatus === ReleasesStatusOption.ARCHIVED
  85. ? ReleaseStatus.Archived
  86. : ReleaseStatus.Active,
  87. };
  88. const endpoints: ReturnType<AsyncView['getEndpoints']> = [
  89. [
  90. 'releases',
  91. `/organizations/${organization.slug}/releases/`,
  92. {query},
  93. {disableEntireQuery: true},
  94. ],
  95. ];
  96. return endpoints;
  97. }
  98. componentDidUpdate(prevProps: Props, prevState: State) {
  99. super.componentDidUpdate(prevProps, prevState);
  100. if (prevState.releases !== this.state.releases) {
  101. /**
  102. * Manually trigger checking for elements in viewport.
  103. * Helpful when LazyLoad components enter the viewport without resize or scroll events,
  104. * https://github.com/twobin/react-lazyload#forcecheck
  105. *
  106. * HealthStatsCharts are being rendered only when they are scrolled into viewport.
  107. * This is how we re-check them without scrolling once releases change as this view
  108. * uses shouldReload=true and there is no reloading happening.
  109. */
  110. forceCheck();
  111. }
  112. }
  113. getQuery() {
  114. const {query} = this.props.location.query;
  115. return typeof query === 'string' ? query : undefined;
  116. }
  117. getSort(): ReleasesSortOption {
  118. const {environments} = this.props.selection;
  119. const {sort} = this.props.location.query;
  120. // Require 1 environment for date adopted
  121. if (sort === ReleasesSortOption.ADOPTION && environments.length !== 1) {
  122. return ReleasesSortOption.DATE;
  123. }
  124. const sortExists = Object.values(ReleasesSortOption).includes(sort);
  125. if (sortExists) {
  126. return sort;
  127. }
  128. return ReleasesSortOption.DATE;
  129. }
  130. getDisplay(): ReleasesDisplayOption {
  131. const {display} = this.props.location.query;
  132. switch (display) {
  133. case ReleasesDisplayOption.USERS:
  134. return ReleasesDisplayOption.USERS;
  135. default:
  136. return ReleasesDisplayOption.SESSIONS;
  137. }
  138. }
  139. getStatus(): ReleasesStatusOption {
  140. const {status} = this.props.location.query;
  141. switch (status) {
  142. case ReleasesStatusOption.ARCHIVED:
  143. return ReleasesStatusOption.ARCHIVED;
  144. default:
  145. return ReleasesStatusOption.ACTIVE;
  146. }
  147. }
  148. getSelectedProject(): Project | undefined {
  149. const {selection, projects} = this.props;
  150. const selectedProjectId =
  151. selection.projects && selection.projects.length === 1 && selection.projects[0];
  152. return projects?.find(p => p.id === `${selectedProjectId}`);
  153. }
  154. get projectHasSessions() {
  155. return this.getSelectedProject()?.hasSessions ?? null;
  156. }
  157. handleSearch = (query: string) => {
  158. const {location, router} = this.props;
  159. router.push({
  160. ...location,
  161. query: {...location.query, cursor: undefined, query},
  162. });
  163. };
  164. handleSortBy = (sort: string) => {
  165. const {location, router} = this.props;
  166. router.push({
  167. ...location,
  168. query: {...location.query, cursor: undefined, sort},
  169. });
  170. };
  171. handleDisplay = (display: string) => {
  172. const {location, router} = this.props;
  173. let sort = location.query.sort;
  174. if (
  175. sort === ReleasesSortOption.USERS_24_HOURS &&
  176. display === ReleasesDisplayOption.SESSIONS
  177. ) {
  178. sort = ReleasesSortOption.SESSIONS_24_HOURS;
  179. } else if (
  180. sort === ReleasesSortOption.SESSIONS_24_HOURS &&
  181. display === ReleasesDisplayOption.USERS
  182. ) {
  183. sort = ReleasesSortOption.USERS_24_HOURS;
  184. } else if (
  185. sort === ReleasesSortOption.CRASH_FREE_USERS &&
  186. display === ReleasesDisplayOption.SESSIONS
  187. ) {
  188. sort = ReleasesSortOption.CRASH_FREE_SESSIONS;
  189. } else if (
  190. sort === ReleasesSortOption.CRASH_FREE_SESSIONS &&
  191. display === ReleasesDisplayOption.USERS
  192. ) {
  193. sort = ReleasesSortOption.CRASH_FREE_USERS;
  194. }
  195. router.push({
  196. ...location,
  197. query: {...location.query, cursor: undefined, display, sort},
  198. });
  199. };
  200. handleStatus = (status: string) => {
  201. const {location, router} = this.props;
  202. router.push({
  203. ...location,
  204. query: {...location.query, cursor: undefined, status},
  205. });
  206. };
  207. trackAddReleaseHealth = () => {
  208. const {organization, selection} = this.props;
  209. if (organization.id && selection.projects[0]) {
  210. trackAnalyticsEvent({
  211. eventKey: `releases_list.click_add_release_health`,
  212. eventName: `Releases List: Click Add Release Health`,
  213. organization_id: parseInt(organization.id, 10),
  214. project_id: selection.projects[0],
  215. });
  216. }
  217. };
  218. tagValueLoader = (key: string, search: string) => {
  219. const {location, organization} = this.props;
  220. const {project: projectId} = location.query;
  221. return fetchTagValues(
  222. this.api,
  223. organization.slug,
  224. key,
  225. search,
  226. projectId ? [projectId] : null,
  227. location.query
  228. );
  229. };
  230. getTagValues = async (tag: Tag, currentQuery: string): Promise<string[]> => {
  231. const values = await this.tagValueLoader(tag.key, currentQuery);
  232. return values.map(({value}) => value);
  233. };
  234. shouldShowLoadingIndicator() {
  235. const {loading, releases, reloading} = this.state;
  236. return (loading && !reloading) || (loading && !releases?.length);
  237. }
  238. renderLoading() {
  239. return this.renderBody();
  240. }
  241. renderError() {
  242. return this.renderBody();
  243. }
  244. renderEmptyMessage() {
  245. const {location, organization, selection} = this.props;
  246. const {statsPeriod} = location.query;
  247. const searchQuery = this.getQuery();
  248. const activeSort = this.getSort();
  249. const activeStatus = this.getStatus();
  250. if (searchQuery && searchQuery.length) {
  251. return (
  252. <EmptyStateWarning small>{`${t(
  253. 'There are no releases that match'
  254. )}: '${searchQuery}'.`}</EmptyStateWarning>
  255. );
  256. }
  257. if (activeSort === ReleasesSortOption.USERS_24_HOURS) {
  258. return (
  259. <EmptyStateWarning small>
  260. {t('There are no releases with active user data (users in the last 24 hours).')}
  261. </EmptyStateWarning>
  262. );
  263. }
  264. if (activeSort === ReleasesSortOption.SESSIONS_24_HOURS) {
  265. return (
  266. <EmptyStateWarning small>
  267. {t(
  268. 'There are no releases with active session data (sessions in the last 24 hours).'
  269. )}
  270. </EmptyStateWarning>
  271. );
  272. }
  273. if (
  274. activeSort === ReleasesSortOption.BUILD ||
  275. activeSort === ReleasesSortOption.SEMVER
  276. ) {
  277. return (
  278. <EmptyStateWarning small>
  279. {t('There are no releases with semantic versioning.')}
  280. </EmptyStateWarning>
  281. );
  282. }
  283. if (activeSort !== ReleasesSortOption.DATE) {
  284. const relativePeriod = getRelativeSummary(
  285. statsPeriod || DEFAULT_STATS_PERIOD
  286. ).toLowerCase();
  287. return (
  288. <EmptyStateWarning small>
  289. {`${t('There are no releases with data in the')} ${relativePeriod}.`}
  290. </EmptyStateWarning>
  291. );
  292. }
  293. if (activeStatus === ReleasesStatusOption.ARCHIVED) {
  294. return (
  295. <EmptyStateWarning small>
  296. {t('There are no archived releases.')}
  297. </EmptyStateWarning>
  298. );
  299. }
  300. return (
  301. <ReleasesPromo
  302. organization={organization}
  303. projectId={selection.projects.filter(p => p !== ALL_ACCESS_PROJECTS)[0]}
  304. />
  305. );
  306. }
  307. renderHealthCta() {
  308. const {organization} = this.props;
  309. const {releases} = this.state;
  310. const selectedProject = this.getSelectedProject();
  311. if (!selectedProject || this.projectHasSessions !== false || !releases?.length) {
  312. return null;
  313. }
  314. return (
  315. <Projects orgId={organization.slug} slugs={[selectedProject.slug]}>
  316. {({projects, initiallyLoaded, fetchError}) => {
  317. const project = projects && projects.length === 1 && projects[0];
  318. const projectCanHaveReleases =
  319. project && project.platform && releaseHealth.includes(project.platform);
  320. if (!initiallyLoaded || fetchError || !projectCanHaveReleases) {
  321. return null;
  322. }
  323. return (
  324. <Alert type="info" showIcon>
  325. <AlertText>
  326. <div>
  327. {t(
  328. 'To track user adoption, crash rates, session data and more, add Release Health to your current setup.'
  329. )}
  330. </div>
  331. <ExternalLink
  332. href="https://docs.sentry.io/product/releases/health/setup/"
  333. onClick={this.trackAddReleaseHealth}
  334. >
  335. {t('Add Release Health')}
  336. </ExternalLink>
  337. </AlertText>
  338. </Alert>
  339. );
  340. }}
  341. </Projects>
  342. );
  343. }
  344. renderInnerBody(
  345. activeDisplay: ReleasesDisplayOption,
  346. showReleaseAdoptionStages: boolean
  347. ) {
  348. const {location, selection, organization, router} = this.props;
  349. const {releases, reloading, releasesPageLinks} = this.state;
  350. if (this.shouldShowLoadingIndicator()) {
  351. return <LoadingIndicator />;
  352. }
  353. if (!releases?.length) {
  354. return this.renderEmptyMessage();
  355. }
  356. return (
  357. <ReleasesRequest
  358. releases={releases.map(({version}) => version)}
  359. organization={organization}
  360. selection={selection}
  361. location={location}
  362. display={[this.getDisplay()]}
  363. releasesReloading={reloading}
  364. healthStatsPeriod={location.query.healthStatsPeriod}
  365. >
  366. {({isHealthLoading, getHealthData}) => {
  367. const singleProjectSelected =
  368. selection.projects?.length === 1 &&
  369. selection.projects[0] !== ALL_ACCESS_PROJECTS;
  370. const selectedProject = this.getSelectedProject();
  371. const isMobileProject =
  372. selectedProject?.platform && isMobileRelease(selectedProject.platform);
  373. return (
  374. <Fragment>
  375. {singleProjectSelected && this.projectHasSessions && isMobileProject && (
  376. <ReleasesAdoptionChart
  377. organization={organization}
  378. selection={selection}
  379. location={location}
  380. router={router}
  381. activeDisplay={activeDisplay}
  382. />
  383. )}
  384. {releases.map((release, index) => (
  385. <ReleaseCard
  386. key={`${release.version}-${release.projects[0].slug}`}
  387. activeDisplay={activeDisplay}
  388. release={release}
  389. organization={organization}
  390. location={location}
  391. selection={selection}
  392. reloading={reloading}
  393. showHealthPlaceholders={isHealthLoading}
  394. isTopRelease={index === 0}
  395. getHealthData={getHealthData}
  396. showReleaseAdoptionStages={showReleaseAdoptionStages}
  397. />
  398. ))}
  399. <Pagination pageLinks={releasesPageLinks} />
  400. </Fragment>
  401. );
  402. }}
  403. </ReleasesRequest>
  404. );
  405. }
  406. renderBody() {
  407. const {organization, selection} = this.props;
  408. const {releases, reloading, error} = this.state;
  409. const activeSort = this.getSort();
  410. const activeStatus = this.getStatus();
  411. const activeDisplay = this.getDisplay();
  412. const hasAnyMobileProject = selection.projects
  413. .map(id => `${id}`)
  414. .map(ProjectsStore.getById)
  415. .some(project => project?.platform && isMobileRelease(project.platform));
  416. const showReleaseAdoptionStages =
  417. hasAnyMobileProject && selection.environments.length === 1;
  418. const hasReleasesSetup = releases && releases.length > 0;
  419. return (
  420. <PageFiltersContainer
  421. showAbsolute={false}
  422. timeRangeHint={t(
  423. 'Changing this date range will recalculate the release metrics.'
  424. )}
  425. hideGlobalHeader
  426. >
  427. <PageContent>
  428. <NoProjectMessage organization={organization}>
  429. <PageHeader>
  430. <PageHeading>{t('Releases')}</PageHeading>
  431. </PageHeader>
  432. {this.renderHealthCta()}
  433. <ReleasesPageFilterBar>
  434. <ProjectPageFilter />
  435. <EnvironmentPageFilter />
  436. <DatePageFilter alignDropdown="left" />
  437. </ReleasesPageFilterBar>
  438. <SortAndFilterWrapper>
  439. <GuideAnchor
  440. target="releases_search"
  441. position="bottom"
  442. disabled={!hasReleasesSetup}
  443. >
  444. <GuideAnchor
  445. target="release_stages"
  446. position="bottom"
  447. disabled={!showReleaseAdoptionStages}
  448. >
  449. <SmartSearchBar
  450. searchSource="releases"
  451. query={this.getQuery()}
  452. placeholder={t('Search by version, build, package, or stage')}
  453. maxSearchItems={5}
  454. hasRecentSearches={false}
  455. supportedTags={{
  456. ...SEMVER_TAGS,
  457. release: {
  458. key: 'release',
  459. name: 'release',
  460. },
  461. }}
  462. supportedTagType={ItemType.PROPERTY}
  463. onSearch={this.handleSearch}
  464. onGetTagValues={this.getTagValues}
  465. />
  466. </GuideAnchor>
  467. </GuideAnchor>
  468. <DropdownsWrapper>
  469. <ReleasesStatusOptions
  470. selected={activeStatus}
  471. onSelect={this.handleStatus}
  472. />
  473. <ReleasesSortOptions
  474. selected={activeSort}
  475. selectedDisplay={activeDisplay}
  476. onSelect={this.handleSortBy}
  477. environments={selection.environments}
  478. />
  479. <ReleasesDisplayOptions
  480. selected={activeDisplay}
  481. onSelect={this.handleDisplay}
  482. />
  483. </DropdownsWrapper>
  484. </SortAndFilterWrapper>
  485. {!reloading &&
  486. activeStatus === ReleasesStatusOption.ARCHIVED &&
  487. !!releases?.length && <ReleaseArchivedNotice multi />}
  488. {error
  489. ? super.renderError(new Error('Unable to load all required endpoints'))
  490. : this.renderInnerBody(activeDisplay, showReleaseAdoptionStages)}
  491. </NoProjectMessage>
  492. </PageContent>
  493. </PageFiltersContainer>
  494. );
  495. }
  496. }
  497. const AlertText = styled('div')`
  498. display: flex;
  499. align-items: flex-start;
  500. justify-content: flex-start;
  501. gap: ${space(2)};
  502. > *:nth-child(1) {
  503. flex: 1;
  504. }
  505. flex-direction: column;
  506. @media (min-width: ${p => p.theme.breakpoints[1]}) {
  507. flex-direction: row;
  508. }
  509. `;
  510. const ReleasesPageFilterBar = styled(PageFilterBar)`
  511. width: max-content;
  512. max-width: 100%;
  513. margin-bottom: ${space(1)};
  514. `;
  515. const SortAndFilterWrapper = styled('div')`
  516. display: flex;
  517. flex-direction: column;
  518. justify-content: stretch;
  519. margin-bottom: ${space(2)};
  520. > *:nth-child(1) {
  521. flex: 1;
  522. }
  523. /* Below this width search bar needs its own row no to wrap placeholder text
  524. * Above this width search bar and controls can be on the same row */
  525. @media (min-width: ${p => p.theme.breakpoints[2]}) {
  526. flex-direction: row;
  527. }
  528. `;
  529. const DropdownsWrapper = styled('div')`
  530. display: flex;
  531. flex-direction: column;
  532. & > * {
  533. margin-top: ${space(2)};
  534. }
  535. /* At the narrower widths wrapper is on its own in a row
  536. * Expand the dropdown controls to fill the empty space */
  537. & button {
  538. width: 100%;
  539. }
  540. /* At narrower widths space bar needs a separate row
  541. * Divide space evenly when 3 dropdowns are in their own row */
  542. @media (min-width: ${p => p.theme.breakpoints[0]}) {
  543. margin-top: ${space(2)};
  544. & > * {
  545. margin-top: ${space(0)};
  546. margin-left: ${space(1)};
  547. }
  548. & > *:nth-child(1) {
  549. margin-left: ${space(0)};
  550. }
  551. display: grid;
  552. grid-template-columns: 1fr 1fr 1fr;
  553. }
  554. /* At wider widths everything is in 1 row
  555. * Auto space dropdowns when they are in the same row with search bar */
  556. @media (min-width: ${p => p.theme.breakpoints[2]}) {
  557. margin-top: ${space(0)};
  558. & > * {
  559. margin-left: ${space(1)} !important;
  560. }
  561. display: grid;
  562. grid-template-columns: auto auto auto;
  563. }
  564. `;
  565. export default withProjects(withOrganization(withPageFilters(ReleasesList)));
  566. export {ReleasesList};