usageTable.tsx 5.4 KB

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