projectCard.tsx 13 KB

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