projectCard.tsx 13 KB

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