index.tsx 20 KB

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