projectCard.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 {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 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. dataset: DiscoverDatasets.METRICS_ENHANCED,
  54. sessionStats: '1',
  55. },
  56. });
  57. }
  58. get hasPerformance() {
  59. return this.props.organization.features.includes('performance-view');
  60. }
  61. get crashFreeTrend() {
  62. const {currentCrashFreeRate, previousCrashFreeRate} =
  63. this.props.project.sessionStats || {};
  64. if (!defined(currentCrashFreeRate) || !defined(previousCrashFreeRate)) {
  65. return undefined;
  66. }
  67. return round(
  68. currentCrashFreeRate - previousCrashFreeRate,
  69. currentCrashFreeRate > CRASH_FREE_DECIMAL_THRESHOLD ? 3 : 0
  70. );
  71. }
  72. renderMissingFeatureCard() {
  73. const {organization, project} = this.props;
  74. return (
  75. <ScoreCard
  76. title={t('Crash Free Sessions')}
  77. score={
  78. <MissingReleasesButtons
  79. organization={organization}
  80. health
  81. platform={project.platform}
  82. />
  83. }
  84. />
  85. );
  86. }
  87. renderTrend() {
  88. const {currentCrashFreeRate} = this.props.project.sessionStats || {};
  89. if (!defined(currentCrashFreeRate) || !defined(this.crashFreeTrend)) {
  90. return null;
  91. }
  92. return (
  93. <div>
  94. {this.crashFreeTrend >= 0 ? (
  95. <IconArrow direction="up" size="xs" />
  96. ) : (
  97. <IconArrow direction="down" size="xs" />
  98. )}
  99. {`${formatAbbreviatedNumber(Math.abs(this.crashFreeTrend))}\u0025`}
  100. </div>
  101. );
  102. }
  103. render() {
  104. const {organization, project, hasProjectAccess} = this.props;
  105. const {stats, slug, transactionStats, sessionStats} = project;
  106. const {hasHealthData, currentCrashFreeRate} = sessionStats || {};
  107. const totalErrors = stats?.reduce((sum, [_, value]) => sum + value, 0) ?? 0;
  108. const totalTransactions =
  109. transactionStats?.reduce((sum, [_, value]) => sum + value, 0) ?? 0;
  110. const zeroTransactions = totalTransactions === 0;
  111. const hasFirstEvent = Boolean(project.firstEvent || project.firstTransactionEvent);
  112. return (
  113. <div data-test-id={slug}>
  114. <StyledProjectCard>
  115. <CardHeader>
  116. <HeaderRow>
  117. <StyledIdBadge
  118. project={project}
  119. avatarSize={32}
  120. hideOverflow
  121. disableLink={!hasProjectAccess}
  122. />
  123. <SettingsButton
  124. borderless
  125. size="zero"
  126. icon={<IconSettings color="subText" />}
  127. aria-label={t('Settings')}
  128. to={`/settings/${organization.slug}/projects/${slug}/`}
  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(listener => {
  239. if (typeof listener === 'function') {
  240. listener();
  241. }
  242. });
  243. }
  244. listeners = [
  245. ProjectsStatsStore.listen(itemsBySlug => {
  246. this.onProjectStatsStoreUpdate(itemsBySlug);
  247. }, undefined),
  248. ];
  249. onProjectStatsStoreUpdate(itemsBySlug: (typeof ProjectsStatsStore)['itemsBySlug']) {
  250. const {project} = this.props;
  251. // Don't update state if we already have stats
  252. if (!itemsBySlug[project.slug]) {
  253. return;
  254. }
  255. if (itemsBySlug[project.slug] === this.state.projectDetails) {
  256. return;
  257. }
  258. this.setState({
  259. projectDetails: itemsBySlug[project.slug],
  260. });
  261. }
  262. render() {
  263. const {project, ...props} = this.props;
  264. const {projectDetails} = this.state;
  265. return (
  266. <ProjectCard
  267. {...props}
  268. project={{
  269. ...project,
  270. ...(projectDetails || {}),
  271. }}
  272. />
  273. );
  274. }
  275. }
  276. const ChartContainer = styled('div')`
  277. position: relative;
  278. background: ${p => p.theme.backgroundSecondary};
  279. `;
  280. const CardHeader = styled('div')`
  281. margin: ${space(2)} 13px;
  282. height: 32px;
  283. `;
  284. const SettingsButton = styled(Button)`
  285. margin-left: auto;
  286. margin-top: -${space(0.5)};
  287. padding: 3px;
  288. border-radius: 50%;
  289. `;
  290. const StyledBookmarkStar = styled(BookmarkStar)`
  291. padding: 0;
  292. `;
  293. const HeaderRow = styled('div')`
  294. display: flex;
  295. justify-content: space-between;
  296. align-items: flex-start;
  297. gap: 0 ${space(0.5)};
  298. ${p => p.theme.text.cardTitle};
  299. color: ${p => p.theme.headingColor};
  300. `;
  301. const StyledProjectCard = styled(Panel)`
  302. min-height: 330px;
  303. margin: 0;
  304. `;
  305. const FooterWrapper = styled('div')`
  306. display: grid;
  307. grid-template-columns: 1fr 1fr;
  308. div {
  309. border: none;
  310. box-shadow: none;
  311. font-size: ${p => p.theme.fontSizeMedium};
  312. padding: 0;
  313. }
  314. `;
  315. const ScoreCardWrapper = styled('div')`
  316. margin: ${space(2)} 0 0 ${space(2)};
  317. ${ScorePanel} {
  318. min-height: auto;
  319. }
  320. ${Title} {
  321. color: ${p => p.theme.gray300};
  322. }
  323. ${ScoreWrapper} {
  324. flex-direction: column;
  325. align-items: flex-start;
  326. }
  327. ${Score} {
  328. font-size: 28px;
  329. }
  330. ${Trend} {
  331. margin-left: 0;
  332. margin-top: ${space(0.5)};
  333. }
  334. `;
  335. const DeploysWrapper = styled('div')`
  336. margin-top: ${space(2)};
  337. ${GetStarted} {
  338. display: block;
  339. height: 100%;
  340. }
  341. ${TextOverflow} {
  342. display: grid;
  343. grid-template-columns: 1fr 1fr;
  344. grid-column-gap: ${space(1)};
  345. div {
  346. white-space: nowrap;
  347. text-overflow: ellipsis;
  348. overflow: hidden;
  349. }
  350. a {
  351. display: grid;
  352. }
  353. }
  354. ${DeployRows} {
  355. grid-template-columns: 2fr auto;
  356. margin-right: ${space(2)};
  357. height: auto;
  358. svg {
  359. display: none;
  360. }
  361. }
  362. `;
  363. const ReleaseTitle = styled('span')`
  364. color: ${p => p.theme.gray300};
  365. font-weight: ${p => p.theme.fontWeightBold};
  366. `;
  367. const StyledIdBadge = styled(IdBadge)`
  368. overflow: hidden;
  369. white-space: nowrap;
  370. flex-shrink: 1;
  371. & div {
  372. align-items: flex-start;
  373. }
  374. & span {
  375. padding: 0;
  376. position: relative;
  377. top: -1px;
  378. }
  379. `;
  380. const SummaryLinks = styled('div')`
  381. display: flex;
  382. position: relative;
  383. top: -${space(2)};
  384. align-items: center;
  385. font-weight: ${p => p.theme.fontWeightNormal};
  386. color: ${p => p.theme.subText};
  387. font-size: ${p => p.theme.fontSizeSmall};
  388. /* Need to offset for the project icon and margin */
  389. margin-left: 40px;
  390. a {
  391. color: ${p => p.theme.subText};
  392. :hover {
  393. color: ${p => p.theme.linkHoverColor};
  394. }
  395. }
  396. em {
  397. font-style: normal;
  398. margin: 0 ${space(0.5)};
  399. }
  400. `;
  401. const TransactionsLink = styled(Link)`
  402. display: flex;
  403. align-items: center;
  404. justify-content: space-between;
  405. > span {
  406. margin-left: ${space(0.5)};
  407. }
  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));