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 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. aria-label={t('Settings')}
  129. to={`/settings/${organization.slug}/projects/${slug}/`}
  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={`${getPerformanceBaseUrl(organization.slug)}/?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(listener => {
  240. if (typeof listener === 'function') {
  241. listener();
  242. }
  243. });
  244. }
  245. listeners = [
  246. ProjectsStatsStore.listen(itemsBySlug => {
  247. this.onProjectStatsStoreUpdate(itemsBySlug);
  248. }, undefined),
  249. ];
  250. onProjectStatsStoreUpdate(itemsBySlug: (typeof ProjectsStatsStore)['itemsBySlug']) {
  251. const {project} = this.props;
  252. // Don't update state if we already have stats
  253. if (!itemsBySlug[project.slug]) {
  254. return;
  255. }
  256. if (itemsBySlug[project.slug] === this.state.projectDetails) {
  257. return;
  258. }
  259. this.setState({
  260. projectDetails: itemsBySlug[project.slug],
  261. });
  262. }
  263. render() {
  264. const {project, ...props} = this.props;
  265. const {projectDetails} = this.state;
  266. return (
  267. <ProjectCard
  268. {...props}
  269. project={{
  270. ...project,
  271. ...(projectDetails || {}),
  272. }}
  273. />
  274. );
  275. }
  276. }
  277. const ChartContainer = styled('div')`
  278. position: relative;
  279. background: ${p => p.theme.backgroundSecondary};
  280. `;
  281. const CardHeader = styled('div')`
  282. margin: ${space(2)} 13px;
  283. height: 32px;
  284. `;
  285. const SettingsButton = styled(LinkButton)`
  286. margin-left: auto;
  287. margin-top: -${space(0.5)};
  288. padding: 3px;
  289. border-radius: 50%;
  290. `;
  291. const StyledBookmarkStar = styled(BookmarkStar)`
  292. padding: 0;
  293. `;
  294. const HeaderRow = styled('div')`
  295. display: flex;
  296. justify-content: space-between;
  297. align-items: flex-start;
  298. gap: 0 ${space(0.5)};
  299. ${p => p.theme.text.cardTitle};
  300. color: ${p => p.theme.headingColor};
  301. `;
  302. const StyledProjectCard = styled(Panel)`
  303. min-height: 330px;
  304. margin: 0;
  305. `;
  306. const FooterWrapper = styled('div')`
  307. display: grid;
  308. grid-template-columns: 1fr 1fr;
  309. div {
  310. border: none;
  311. box-shadow: none;
  312. font-size: ${p => p.theme.fontSizeMedium};
  313. padding: 0;
  314. }
  315. `;
  316. const ScoreCardWrapper = styled('div')`
  317. margin: ${space(2)} 0 0 ${space(2)};
  318. ${ScorePanel} {
  319. min-height: auto;
  320. }
  321. ${Title} {
  322. color: ${p => p.theme.gray300};
  323. }
  324. ${ScoreWrapper} {
  325. flex-direction: column;
  326. align-items: flex-start;
  327. }
  328. ${Score} {
  329. font-size: 28px;
  330. }
  331. ${Trend} {
  332. margin-left: 0;
  333. margin-top: ${space(0.5)};
  334. }
  335. `;
  336. const DeploysWrapper = styled('div')`
  337. margin-top: ${space(2)};
  338. ${GetStarted} {
  339. display: block;
  340. height: 100%;
  341. }
  342. ${TextOverflow} {
  343. display: grid;
  344. grid-template-columns: 1fr 1fr;
  345. grid-column-gap: ${space(1)};
  346. div {
  347. white-space: nowrap;
  348. text-overflow: ellipsis;
  349. overflow: hidden;
  350. }
  351. a {
  352. display: grid;
  353. }
  354. }
  355. ${DeployRows} {
  356. grid-template-columns: 2fr auto;
  357. margin-right: ${space(2)};
  358. height: auto;
  359. svg {
  360. display: none;
  361. }
  362. }
  363. `;
  364. const ReleaseTitle = styled('span')`
  365. color: ${p => p.theme.gray300};
  366. font-weight: ${p => p.theme.fontWeightBold};
  367. `;
  368. const StyledIdBadge = styled(IdBadge)`
  369. overflow: hidden;
  370. white-space: nowrap;
  371. flex-shrink: 1;
  372. & div {
  373. align-items: flex-start;
  374. }
  375. & span {
  376. padding: 0;
  377. position: relative;
  378. top: -1px;
  379. }
  380. `;
  381. const SummaryLinks = styled('div')`
  382. display: flex;
  383. position: relative;
  384. top: -${space(2)};
  385. align-items: center;
  386. font-weight: ${p => p.theme.fontWeightNormal};
  387. color: ${p => p.theme.subText};
  388. font-size: ${p => p.theme.fontSizeSmall};
  389. /* Need to offset for the project icon and margin */
  390. margin-left: 40px;
  391. a {
  392. color: ${p => p.theme.subText};
  393. :hover {
  394. color: ${p => p.theme.linkHoverColor};
  395. }
  396. }
  397. em {
  398. font-style: normal;
  399. margin: 0 ${space(0.5)};
  400. }
  401. `;
  402. const TransactionsLink = styled(Link)`
  403. display: flex;
  404. align-items: center;
  405. justify-content: space-between;
  406. > span {
  407. margin-left: ${space(0.5)};
  408. }
  409. `;
  410. const SummaryLinkPlaceholder = styled(Placeholder)`
  411. height: 15px;
  412. width: 180px;
  413. margin-top: ${space(0.75)};
  414. margin-bottom: ${space(0.5)};
  415. `;
  416. const FooterPlaceholder = styled(Placeholder)`
  417. height: 40px;
  418. width: auto;
  419. margin-right: ${space(2)};
  420. `;
  421. export {ProjectCard};
  422. export default withOrganization(withApi(ProjectCardContainer));