projectCard.tsx 13 KB

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