projectCard.tsx 12 KB

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