projectCard.tsx 13 KB

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