projectCard.tsx 12 KB

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