usageTable.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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} from 'sentry/components/button';
  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, {PanelTableHeader} from 'sentry/components/panels/panelTable';
  13. import {IconGraph, IconSettings, IconWarning} from 'sentry/icons';
  14. import {t, tct} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  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. <Button
  107. title="Go to project level stats"
  108. data-test-id={project.slug}
  109. size="xs"
  110. onClick={() => {
  111. this.loadProject(parseInt(stat.project.id, 10));
  112. }}
  113. >
  114. <StyledIconGraph type="bar" size="sm" />
  115. <span>View Stats</span>
  116. </Button>
  117. <Link to={stat.projectSettingsLink}>
  118. <StyledSettingsButton size="xs" title="Go to project settings">
  119. <SettingsIcon size="sm" />
  120. </StyledSettingsButton>
  121. </Link>
  122. </CellStat>,
  123. ];
  124. }
  125. render() {
  126. const {isEmpty, isLoading, isError, errors, headers, usageStats} = this.props;
  127. if (isError) {
  128. return (
  129. <Panel>
  130. <ErrorPanel height="256px">{this.getErrorMessage(errors)}</ErrorPanel>
  131. </Panel>
  132. );
  133. }
  134. return (
  135. <StyledPanelTable isLoading={isLoading} isEmpty={isEmpty} headers={headers}>
  136. {usageStats.map(s => this.renderTableRow(s))}
  137. </StyledPanelTable>
  138. );
  139. }
  140. }
  141. export default withSentryRouter(UsageTable);
  142. const StyledPanelTable = styled(PanelTable)`
  143. grid-template-columns: repeat(6, auto);
  144. @media (min-width: ${p => p.theme.breakpoints.small}) {
  145. grid-template-columns: 1fr repeat(5, minmax(0, auto));
  146. }
  147. ${PanelTableHeader} {
  148. height: 45px;
  149. }
  150. `;
  151. export const CellStat = styled('div')`
  152. display: flex;
  153. align-items: center;
  154. font-variant-numeric: tabular-nums;
  155. justify-content: right;
  156. padding-top: 9px;
  157. padding-bottom: 9px;
  158. `;
  159. export const CellProject = styled(CellStat)`
  160. justify-content: left;
  161. `;
  162. const StyledIdBadge = styled(IdBadge)`
  163. overflow: hidden;
  164. white-space: nowrap;
  165. flex-shrink: 1;
  166. `;
  167. const SettingsIcon = styled(IconSettings)`
  168. color: ${p => p.theme.gray300};
  169. align-items: center;
  170. display: inline-flex;
  171. justify-content: space-between;
  172. margin-right: ${space(1.0)};
  173. margin-left: ${space(1.0)};
  174. transition: 0.5s opacity ease-out;
  175. &:hover {
  176. color: ${p => p.theme.textColor};
  177. }
  178. `;
  179. const StyledIconGraph = styled(IconGraph)`
  180. color: ${p => p.theme.gray300};
  181. margin-right: 5px;
  182. &:hover {
  183. color: ${p => p.theme.textColor};
  184. cursor: pointer;
  185. }
  186. `;
  187. const StyledSettingsButton = styled(Button)`
  188. padding: 0px;
  189. margin-left: 7px;
  190. `;