projectCard.tsx 12 KB

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