projectCard.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import round from 'lodash/round';
  4. import {loadStatsForProject} from 'sentry/actionCreators/projects';
  5. import type {Client} from 'sentry/api';
  6. import {Button} from 'sentry/components/button';
  7. import IdBadge from 'sentry/components/idBadge';
  8. import Link from 'sentry/components/links/link';
  9. import Panel from 'sentry/components/panels/panel';
  10. import Placeholder from 'sentry/components/placeholder';
  11. import BookmarkStar from 'sentry/components/projects/bookmarkStar';
  12. import QuestionTooltip from 'sentry/components/questionTooltip';
  13. import ScoreCard, {
  14. Score,
  15. ScorePanel,
  16. ScoreWrapper,
  17. Title,
  18. Trend,
  19. } from 'sentry/components/scoreCard';
  20. import {releaseHealth} from 'sentry/data/platformCategories';
  21. import {IconArrow, IconSettings} from 'sentry/icons';
  22. import {t} from 'sentry/locale';
  23. import ProjectsStatsStore from 'sentry/stores/projectsStatsStore';
  24. import {space} from 'sentry/styles/space';
  25. import type {Organization} from 'sentry/types/organization';
  26. import type {Project} from 'sentry/types/project';
  27. import {defined} from 'sentry/utils';
  28. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  29. import {formatAbbreviatedNumber} from 'sentry/utils/formatters';
  30. import withApi from 'sentry/utils/withApi';
  31. import withOrganization from 'sentry/utils/withOrganization';
  32. import MissingReleasesButtons from 'sentry/views/projectDetail/missingFeatureButtons/missingReleasesButtons';
  33. import {
  34. CRASH_FREE_DECIMAL_THRESHOLD,
  35. displayCrashFreePercent,
  36. } from 'sentry/views/releases/utils';
  37. import Chart from './chart';
  38. import Deploys, {DeployRows, GetStarted, TextOverflow} from './deploys';
  39. type Props = {
  40. api: Client;
  41. hasProjectAccess: boolean;
  42. organization: Organization;
  43. project: Project;
  44. };
  45. class ProjectCard extends Component<Props> {
  46. componentDidMount() {
  47. const {organization, project, api} = this.props;
  48. // fetch project stats
  49. loadStatsForProject(api, project.id, {
  50. orgId: organization.slug,
  51. projectId: project.id,
  52. query: {
  53. transactionStats: this.hasPerformance ? '1' : undefined,
  54. dataset: DiscoverDatasets.METRICS_ENHANCED,
  55. sessionStats: '1',
  56. },
  57. });
  58. }
  59. get hasPerformance() {
  60. return this.props.organization.features.includes('performance-view');
  61. }
  62. get crashFreeTrend() {
  63. const {currentCrashFreeRate, previousCrashFreeRate} =
  64. this.props.project.sessionStats || {};
  65. if (!defined(currentCrashFreeRate) || !defined(previousCrashFreeRate)) {
  66. return undefined;
  67. }
  68. return round(
  69. currentCrashFreeRate - previousCrashFreeRate,
  70. currentCrashFreeRate > CRASH_FREE_DECIMAL_THRESHOLD ? 3 : 0
  71. );
  72. }
  73. renderMissingFeatureCard() {
  74. const {organization, project} = this.props;
  75. if (project.platform && releaseHealth.includes(project.platform)) {
  76. return (
  77. <ScoreCard
  78. title={t('Crash Free Sessions')}
  79. score={<MissingReleasesButtons organization={organization} health />}
  80. />
  81. );
  82. }
  83. return (
  84. <ScoreCard
  85. title={t('Crash Free Sessions')}
  86. score={
  87. <NotAvailable>
  88. {t('Not Available')}
  89. <QuestionTooltip
  90. title={t('Release Health is not yet supported on this platform.')}
  91. size="xs"
  92. />
  93. </NotAvailable>
  94. }
  95. />
  96. );
  97. }
  98. renderTrend() {
  99. const {currentCrashFreeRate} = this.props.project.sessionStats || {};
  100. if (!defined(currentCrashFreeRate) || !defined(this.crashFreeTrend)) {
  101. return null;
  102. }
  103. return (
  104. <div>
  105. {this.crashFreeTrend >= 0 ? (
  106. <IconArrow direction="up" size="xs" />
  107. ) : (
  108. <IconArrow direction="down" size="xs" />
  109. )}
  110. {`${formatAbbreviatedNumber(Math.abs(this.crashFreeTrend))}\u0025`}
  111. </div>
  112. );
  113. }
  114. render() {
  115. const {organization, project, hasProjectAccess} = this.props;
  116. const {stats, slug, transactionStats, sessionStats} = project;
  117. const {hasHealthData, currentCrashFreeRate} = sessionStats || {};
  118. const totalErrors = stats?.reduce((sum, [_, value]) => sum + value, 0) ?? 0;
  119. const totalTransactions =
  120. transactionStats?.reduce((sum, [_, value]) => sum + value, 0) ?? 0;
  121. const zeroTransactions = totalTransactions === 0;
  122. const hasFirstEvent = Boolean(project.firstEvent || project.firstTransactionEvent);
  123. return (
  124. <div data-test-id={slug}>
  125. <StyledProjectCard>
  126. <CardHeader>
  127. <HeaderRow>
  128. <StyledIdBadge
  129. project={project}
  130. avatarSize={32}
  131. hideOverflow
  132. disableLink={!hasProjectAccess}
  133. />
  134. <SettingsButton
  135. borderless
  136. size="zero"
  137. icon={<IconSettings color="subText" />}
  138. aria-label={t('Settings')}
  139. to={`/settings/${organization.slug}/projects/${slug}/`}
  140. />
  141. <StyledBookmarkStar organization={organization} project={project} />
  142. </HeaderRow>
  143. <SummaryLinks data-test-id="summary-links">
  144. {stats ? (
  145. <Fragment>
  146. <Link
  147. data-test-id="project-errors"
  148. to={`/organizations/${organization.slug}/issues/?project=${project.id}`}
  149. >
  150. {t('Errors: %s', formatAbbreviatedNumber(totalErrors))}
  151. </Link>
  152. {this.hasPerformance && (
  153. <Fragment>
  154. <em>|</em>
  155. <TransactionsLink
  156. data-test-id="project-transactions"
  157. to={`/organizations/${organization.slug}/performance/?project=${project.id}`}
  158. >
  159. {t(
  160. 'Transactions: %s',
  161. formatAbbreviatedNumber(totalTransactions)
  162. )}
  163. {zeroTransactions && (
  164. <QuestionTooltip
  165. title={t(
  166. 'Click here to learn more about performance monitoring'
  167. )}
  168. position="top"
  169. size="xs"
  170. />
  171. )}
  172. </TransactionsLink>
  173. </Fragment>
  174. )}
  175. </Fragment>
  176. ) : (
  177. <SummaryLinkPlaceholder />
  178. )}
  179. </SummaryLinks>
  180. </CardHeader>
  181. <ChartContainer data-test-id="chart-container">
  182. {stats ? (
  183. <Chart
  184. firstEvent={hasFirstEvent}
  185. stats={stats}
  186. transactionStats={transactionStats}
  187. />
  188. ) : (
  189. <Placeholder height="150px" />
  190. )}
  191. </ChartContainer>
  192. <FooterWrapper>
  193. <ScoreCardWrapper>
  194. {!stats ? (
  195. <Fragment>
  196. <ReleaseTitle>{t('Crash Free Sessions')}</ReleaseTitle>
  197. <FooterPlaceholder />
  198. </Fragment>
  199. ) : hasHealthData ? (
  200. <ScoreCard
  201. title={t('Crash Free Sessions')}
  202. score={
  203. defined(currentCrashFreeRate)
  204. ? displayCrashFreePercent(currentCrashFreeRate)
  205. : '\u2014'
  206. }
  207. trend={this.renderTrend()}
  208. trendStatus={
  209. this.crashFreeTrend
  210. ? this.crashFreeTrend > 0
  211. ? 'good'
  212. : 'bad'
  213. : undefined
  214. }
  215. />
  216. ) : (
  217. this.renderMissingFeatureCard()
  218. )}
  219. </ScoreCardWrapper>
  220. <DeploysWrapper>
  221. <ReleaseTitle>{t('Latest Deploys')}</ReleaseTitle>
  222. {stats ? <Deploys project={project} shorten /> : <FooterPlaceholder />}
  223. </DeploysWrapper>
  224. </FooterWrapper>
  225. </StyledProjectCard>
  226. </div>
  227. );
  228. }
  229. }
  230. type ContainerProps = {
  231. api: Client;
  232. hasProjectAccess: boolean;
  233. organization: Organization;
  234. project: Project;
  235. };
  236. type ContainerState = {
  237. projectDetails: Project | null;
  238. };
  239. class ProjectCardContainer extends Component<ContainerProps, ContainerState> {
  240. state = this.getInitialState();
  241. getInitialState(): ContainerState {
  242. const {project} = this.props;
  243. const initialState = ProjectsStatsStore.getInitialState() || {};
  244. return {
  245. projectDetails: initialState[project.slug] || null,
  246. };
  247. }
  248. componentWillUnmount() {
  249. this.listeners.forEach(listener => {
  250. if (typeof listener === 'function') {
  251. listener();
  252. }
  253. });
  254. }
  255. listeners = [
  256. ProjectsStatsStore.listen(itemsBySlug => {
  257. this.onProjectStatsStoreUpdate(itemsBySlug);
  258. }, undefined),
  259. ];
  260. onProjectStatsStoreUpdate(itemsBySlug: (typeof ProjectsStatsStore)['itemsBySlug']) {
  261. const {project} = this.props;
  262. // Don't update state if we already have stats
  263. if (!itemsBySlug[project.slug]) {
  264. return;
  265. }
  266. if (itemsBySlug[project.slug] === this.state.projectDetails) {
  267. return;
  268. }
  269. this.setState({
  270. projectDetails: itemsBySlug[project.slug],
  271. });
  272. }
  273. render() {
  274. const {project, ...props} = this.props;
  275. const {projectDetails} = this.state;
  276. return (
  277. <ProjectCard
  278. {...props}
  279. project={{
  280. ...project,
  281. ...(projectDetails || {}),
  282. }}
  283. />
  284. );
  285. }
  286. }
  287. const ChartContainer = styled('div')`
  288. position: relative;
  289. background: ${p => p.theme.backgroundSecondary};
  290. `;
  291. const CardHeader = styled('div')`
  292. margin: ${space(2)} 13px;
  293. height: 32px;
  294. `;
  295. const SettingsButton = styled(Button)`
  296. margin-left: auto;
  297. margin-top: -${space(0.5)};
  298. padding: 3px;
  299. border-radius: 50%;
  300. `;
  301. const StyledBookmarkStar = styled(BookmarkStar)`
  302. padding: 0;
  303. `;
  304. const HeaderRow = styled('div')`
  305. display: flex;
  306. justify-content: space-between;
  307. align-items: flex-start;
  308. gap: 0 ${space(0.5)};
  309. ${p => p.theme.text.cardTitle};
  310. color: ${p => p.theme.headingColor};
  311. `;
  312. const StyledProjectCard = styled(Panel)`
  313. min-height: 330px;
  314. margin: 0;
  315. `;
  316. const FooterWrapper = styled('div')`
  317. display: grid;
  318. grid-template-columns: 1fr 1fr;
  319. div {
  320. border: none;
  321. box-shadow: none;
  322. font-size: ${p => p.theme.fontSizeMedium};
  323. padding: 0;
  324. }
  325. `;
  326. const ScoreCardWrapper = styled('div')`
  327. margin: ${space(2)} 0 0 ${space(2)};
  328. ${ScorePanel} {
  329. min-height: auto;
  330. }
  331. ${Title} {
  332. color: ${p => p.theme.gray300};
  333. }
  334. ${ScoreWrapper} {
  335. flex-direction: column;
  336. align-items: flex-start;
  337. }
  338. ${Score} {
  339. font-size: 28px;
  340. }
  341. ${Trend} {
  342. margin-left: 0;
  343. margin-top: ${space(0.5)};
  344. }
  345. `;
  346. const DeploysWrapper = styled('div')`
  347. margin-top: ${space(2)};
  348. ${GetStarted} {
  349. display: block;
  350. height: 100%;
  351. }
  352. ${TextOverflow} {
  353. display: grid;
  354. grid-template-columns: 1fr 1fr;
  355. grid-column-gap: ${space(1)};
  356. div {
  357. white-space: nowrap;
  358. text-overflow: ellipsis;
  359. overflow: hidden;
  360. }
  361. a {
  362. display: grid;
  363. }
  364. }
  365. ${DeployRows} {
  366. grid-template-columns: 2fr auto;
  367. margin-right: ${space(2)};
  368. height: auto;
  369. svg {
  370. display: none;
  371. }
  372. }
  373. `;
  374. const ReleaseTitle = styled('span')`
  375. color: ${p => p.theme.gray300};
  376. font-weight: 600;
  377. `;
  378. const StyledIdBadge = styled(IdBadge)`
  379. overflow: hidden;
  380. white-space: nowrap;
  381. flex-shrink: 1;
  382. & div {
  383. align-items: flex-start;
  384. }
  385. & span {
  386. padding: 0;
  387. position: relative;
  388. top: -1px;
  389. }
  390. `;
  391. const SummaryLinks = styled('div')`
  392. display: flex;
  393. position: relative;
  394. top: -${space(2)};
  395. align-items: center;
  396. font-weight: 400;
  397. color: ${p => p.theme.subText};
  398. font-size: ${p => p.theme.fontSizeSmall};
  399. /* Need to offset for the project icon and margin */
  400. margin-left: 40px;
  401. a {
  402. color: ${p => p.theme.subText};
  403. :hover {
  404. color: ${p => p.theme.linkHoverColor};
  405. }
  406. }
  407. em {
  408. font-style: normal;
  409. margin: 0 ${space(0.5)};
  410. }
  411. `;
  412. const TransactionsLink = styled(Link)`
  413. display: flex;
  414. align-items: center;
  415. justify-content: space-between;
  416. > span {
  417. margin-left: ${space(0.5)};
  418. }
  419. `;
  420. const NotAvailable = styled('div')`
  421. font-size: ${p => p.theme.fontSizeMedium};
  422. font-weight: normal;
  423. display: grid;
  424. grid-template-columns: auto auto;
  425. gap: ${space(0.5)};
  426. align-items: center;
  427. `;
  428. const SummaryLinkPlaceholder = styled(Placeholder)`
  429. height: 15px;
  430. width: 180px;
  431. margin-top: ${space(0.75)};
  432. margin-bottom: ${space(0.5)};
  433. `;
  434. const FooterPlaceholder = styled(Placeholder)`
  435. height: 40px;
  436. width: auto;
  437. margin-right: ${space(2)};
  438. `;
  439. export {ProjectCard};
  440. export default withOrganization(withApi(ProjectCardContainer));