projectCard.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. hasHealthData ? (
  201. <ScoreCard
  202. title={t('Crash Free Sessions')}
  203. score={
  204. defined(currentCrashFreeRate)
  205. ? displayCrashFreePercent(currentCrashFreeRate)
  206. : '\u2014'
  207. }
  208. trend={this.renderTrend()}
  209. trendStatus={
  210. this.crashFreeTrend
  211. ? this.crashFreeTrend > 0
  212. ? 'good'
  213. : 'bad'
  214. : undefined
  215. }
  216. />
  217. ) : (
  218. this.renderMissingFeatureCard()
  219. )
  220. ) : (
  221. <Fragment>
  222. <ReleaseTitle>{t('Crash Free Sessions')}</ReleaseTitle>
  223. <FooterPlaceholder />
  224. </Fragment>
  225. )}
  226. </ScoreCardWrapper>
  227. <DeploysWrapper>
  228. <ReleaseTitle>{t('Latest Deploys')}</ReleaseTitle>
  229. {stats ? <Deploys project={project} shorten /> : <FooterPlaceholder />}
  230. </DeploysWrapper>
  231. </FooterWrapper>
  232. </StyledProjectCard>
  233. </div>
  234. );
  235. }
  236. }
  237. type ContainerProps = {
  238. api: Client;
  239. hasProjectAccess: boolean;
  240. organization: Organization;
  241. project: Project;
  242. };
  243. type ContainerState = {
  244. projectDetails: Project | null;
  245. };
  246. class ProjectCardContainer extends Component<ContainerProps, ContainerState> {
  247. state = this.getInitialState();
  248. getInitialState(): ContainerState {
  249. const {project} = this.props;
  250. const initialState = ProjectsStatsStore.getInitialState() || {};
  251. return {
  252. projectDetails: initialState[project.slug] || null,
  253. };
  254. }
  255. componentWillUnmount() {
  256. this.listeners.forEach(listener => {
  257. if (typeof listener === 'function') {
  258. listener();
  259. }
  260. });
  261. }
  262. listeners = [
  263. ProjectsStatsStore.listen((itemsBySlug: any) => {
  264. this.onProjectStatsStoreUpdate(itemsBySlug);
  265. }, undefined),
  266. ];
  267. onProjectStatsStoreUpdate(itemsBySlug: (typeof ProjectsStatsStore)['itemsBySlug']) {
  268. const {project} = this.props;
  269. // Don't update state if we already have stats
  270. if (!itemsBySlug[project.slug]) {
  271. return;
  272. }
  273. if (itemsBySlug[project.slug] === this.state.projectDetails) {
  274. return;
  275. }
  276. this.setState({
  277. projectDetails: itemsBySlug[project.slug]!,
  278. });
  279. }
  280. render() {
  281. const {project, ...props} = this.props;
  282. const {projectDetails} = this.state;
  283. return (
  284. <ProjectCard
  285. {...props}
  286. project={{
  287. ...project,
  288. ...projectDetails,
  289. }}
  290. />
  291. );
  292. }
  293. }
  294. const ChartContainer = styled('div')`
  295. position: relative;
  296. background: ${p => p.theme.backgroundSecondary};
  297. `;
  298. const CardHeader = styled('div')`
  299. margin: ${space(2)} 13px;
  300. height: 32px;
  301. `;
  302. const SettingsButton = styled(LinkButton)`
  303. margin-left: auto;
  304. margin-top: -${space(0.5)};
  305. padding: 3px;
  306. border-radius: 50%;
  307. width: 24px;
  308. height: 24px;
  309. `;
  310. const StyledBookmarkStar = styled(BookmarkStar)`
  311. padding: 0;
  312. `;
  313. const HeaderRow = styled('div')`
  314. display: flex;
  315. justify-content: space-between;
  316. align-items: flex-start;
  317. gap: 0 ${space(0.5)};
  318. color: ${p => p.theme.headingColor};
  319. /* @TODO(jonasbadalic) This should be a title component and not a div */
  320. font-size: 1rem;
  321. font-weight: ${p => p.theme.fontWeightBold};
  322. line-height: 1.2;
  323. `;
  324. const StyledProjectCard = styled(Panel)`
  325. min-height: 330px;
  326. margin: 0;
  327. `;
  328. const FooterWrapper = styled('div')`
  329. display: grid;
  330. grid-template-columns: 1fr 1fr;
  331. div {
  332. border: none;
  333. box-shadow: none;
  334. font-size: ${p => p.theme.fontSizeMedium};
  335. padding: 0;
  336. }
  337. `;
  338. const ScoreCardWrapper = styled('div')`
  339. margin: ${space(2)} 0 0 ${space(2)};
  340. ${ScorePanel} {
  341. min-height: auto;
  342. }
  343. ${Title} {
  344. color: ${p => p.theme.gray300};
  345. }
  346. ${ScoreWrapper} {
  347. flex-direction: column;
  348. align-items: flex-start;
  349. }
  350. ${Score} {
  351. font-size: 28px;
  352. }
  353. ${Trend} {
  354. margin-left: 0;
  355. margin-top: ${space(0.5)};
  356. }
  357. `;
  358. const DeploysWrapper = styled('div')`
  359. margin-top: ${space(2)};
  360. ${GetStarted} {
  361. display: block;
  362. height: 100%;
  363. }
  364. ${TextOverflow} {
  365. display: grid;
  366. grid-template-columns: 1fr 1fr;
  367. grid-column-gap: ${space(1)};
  368. div {
  369. white-space: nowrap;
  370. text-overflow: ellipsis;
  371. overflow: hidden;
  372. }
  373. a {
  374. display: grid;
  375. }
  376. }
  377. ${DeployRows} {
  378. grid-template-columns: 2fr auto;
  379. margin-right: ${space(2)};
  380. height: auto;
  381. svg {
  382. display: none;
  383. }
  384. }
  385. `;
  386. const ReleaseTitle = styled('span')`
  387. color: ${p => p.theme.gray300};
  388. font-weight: ${p => p.theme.fontWeightBold};
  389. `;
  390. const StyledIdBadge = styled(IdBadge)`
  391. overflow: hidden;
  392. white-space: nowrap;
  393. flex-shrink: 1;
  394. & div {
  395. align-items: flex-start;
  396. }
  397. & span {
  398. padding: 0;
  399. position: relative;
  400. top: -1px;
  401. }
  402. `;
  403. const SummaryLinks = styled('div')`
  404. display: flex;
  405. position: relative;
  406. top: -${space(2)};
  407. align-items: center;
  408. font-weight: ${p => p.theme.fontWeightNormal};
  409. color: ${p => p.theme.subText};
  410. font-size: ${p => p.theme.fontSizeSmall};
  411. /* Need to offset for the project icon and margin */
  412. margin-left: 40px;
  413. a {
  414. color: ${p => p.theme.subText};
  415. :hover {
  416. color: ${p => p.theme.linkHoverColor};
  417. }
  418. }
  419. em {
  420. font-style: normal;
  421. margin: 0 ${space(0.5)};
  422. }
  423. `;
  424. const TransactionsLink = styled(Link)`
  425. display: flex;
  426. align-items: center;
  427. justify-content: space-between;
  428. > span {
  429. margin-left: ${space(0.5)};
  430. }
  431. `;
  432. const SummaryLinkPlaceholder = styled(Placeholder)`
  433. height: 15px;
  434. width: 180px;
  435. margin-top: ${space(0.75)};
  436. margin-bottom: ${space(0.5)};
  437. `;
  438. const FooterPlaceholder = styled(Placeholder)`
  439. height: 40px;
  440. width: auto;
  441. margin-right: ${space(2)};
  442. `;
  443. export {ProjectCard};
  444. export default withOrganization(withApi(ProjectCardContainer));