usageTable.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import {Component} from 'react';
  2. import type {WithRouterProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {updateProjects} from 'sentry/actionCreators/pageFilters';
  5. import {Button, LinkButton} from 'sentry/components/button';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import ErrorPanel from 'sentry/components/charts/errorPanel';
  8. import EmptyMessage from 'sentry/components/emptyMessage';
  9. import IdBadge from 'sentry/components/idBadge';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import Link from 'sentry/components/links/link';
  12. import Panel from 'sentry/components/panels/panel';
  13. import {PanelTable} from 'sentry/components/panels/panelTable';
  14. import {IconGraph, IconSettings, IconWarning} from 'sentry/icons';
  15. import {t, tct} from 'sentry/locale';
  16. import type {DataCategoryInfo, Project} from 'sentry/types';
  17. import withSentryRouter from 'sentry/utils/withSentryRouter';
  18. import {formatUsageWithUnits, getFormatUsageOptions} from './utils';
  19. const DOCS_URL = 'https://docs.sentry.io/product/accounts/membership/#restricting-access';
  20. type Props = {
  21. dataCategory: DataCategoryInfo;
  22. headers: React.ReactNode[];
  23. usageStats: TableStat[];
  24. errors?: Record<string, Error>;
  25. isEmpty?: boolean;
  26. isError?: boolean;
  27. isLoading?: boolean;
  28. } & WithRouterProps<{}, {}>;
  29. export type TableStat = {
  30. accepted: number;
  31. dropped: number;
  32. filtered: number;
  33. project: Project;
  34. projectLink: string;
  35. projectSettingsLink: string;
  36. total: number;
  37. };
  38. class UsageTable extends Component<Props> {
  39. getErrorMessage = errorMessage => {
  40. if (errorMessage.projectStats.responseJSON.detail === 'No projects available') {
  41. return (
  42. <EmptyMessage
  43. icon={<IconWarning color="gray300" legacySize="48px" />}
  44. title={t(
  45. "You don't have access to any projects, or your organization has no projects."
  46. )}
  47. description={tct('Learn more about [link:Project Access]', {
  48. link: <ExternalLink href={DOCS_URL} />,
  49. })}
  50. />
  51. );
  52. }
  53. return <IconWarning color="gray300" legacySize="48px" />;
  54. };
  55. loadProject(projectId: number) {
  56. updateProjects([projectId], this.props.router, {
  57. save: true,
  58. environments: [], // Clear environments when switching projects
  59. });
  60. window.scrollTo({top: 0, left: 0, behavior: 'smooth'});
  61. }
  62. renderTableRow(stat: TableStat & {project: Project}) {
  63. const {dataCategory} = this.props;
  64. const {project, total, accepted, filtered, dropped} = stat;
  65. return [
  66. <CellProject key={0}>
  67. <Link to={stat.projectLink}>
  68. <StyledIdBadge
  69. avatarSize={16}
  70. disableLink
  71. hideOverflow
  72. project={project}
  73. displayName={project.slug}
  74. />
  75. </Link>
  76. </CellProject>,
  77. <CellStat key={1}>
  78. {formatUsageWithUnits(
  79. total,
  80. dataCategory.plural,
  81. getFormatUsageOptions(dataCategory.plural)
  82. )}
  83. </CellStat>,
  84. <CellStat key={2}>
  85. {formatUsageWithUnits(
  86. accepted,
  87. dataCategory.plural,
  88. getFormatUsageOptions(dataCategory.plural)
  89. )}
  90. </CellStat>,
  91. <CellStat key={3}>
  92. {formatUsageWithUnits(
  93. filtered,
  94. dataCategory.plural,
  95. getFormatUsageOptions(dataCategory.plural)
  96. )}
  97. </CellStat>,
  98. <CellStat key={4}>
  99. {formatUsageWithUnits(
  100. dropped,
  101. dataCategory.plural,
  102. getFormatUsageOptions(dataCategory.plural)
  103. )}
  104. </CellStat>,
  105. <CellStat key={5}>
  106. <ButtonBar gap={1}>
  107. <Button
  108. icon={<IconGraph type="bar" />}
  109. title="Go to project level stats"
  110. data-test-id={project.slug}
  111. size="xs"
  112. onClick={() => {
  113. this.loadProject(parseInt(stat.project.id, 10));
  114. }}
  115. >
  116. {t('View Stats')}
  117. </Button>
  118. <LinkButton
  119. icon={<IconSettings />}
  120. size="xs"
  121. aria-label={t('Project Settings')}
  122. title={t('Go to project settings')}
  123. to={stat.projectSettingsLink}
  124. />
  125. </ButtonBar>
  126. </CellStat>,
  127. ];
  128. }
  129. render() {
  130. const {isEmpty, isLoading, isError, errors, headers, usageStats} = this.props;
  131. if (isError) {
  132. return (
  133. <Panel>
  134. <ErrorPanel height="256px">{this.getErrorMessage(errors)}</ErrorPanel>
  135. </Panel>
  136. );
  137. }
  138. return (
  139. <StyledPanelTable isLoading={isLoading} isEmpty={isEmpty} headers={headers}>
  140. {usageStats.map(s => this.renderTableRow(s))}
  141. </StyledPanelTable>
  142. );
  143. }
  144. }
  145. export default withSentryRouter(UsageTable);
  146. const StyledPanelTable = styled(PanelTable)`
  147. grid-template-columns: repeat(6, auto);
  148. @media (min-width: ${p => p.theme.breakpoints.small}) {
  149. grid-template-columns: 1fr repeat(5, minmax(0, auto));
  150. }
  151. `;
  152. export const CellStat = styled('div')`
  153. display: flex;
  154. align-items: center;
  155. font-variant-numeric: tabular-nums;
  156. justify-content: right;
  157. `;
  158. export const CellProject = styled(CellStat)`
  159. justify-content: left;
  160. `;
  161. const StyledIdBadge = styled(IdBadge)`
  162. overflow: hidden;
  163. white-space: nowrap;
  164. flex-shrink: 1;
  165. `;