usageTable.tsx 5.5 KB

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