index.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import {Component} from 'react';
  2. import {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';
  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 {space} from 'sentry/styles/space';
  16. import {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['plural'];
  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(total, dataCategory, getFormatUsageOptions(dataCategory))}
  79. </CellStat>,
  80. <CellStat key={2}>
  81. {formatUsageWithUnits(
  82. accepted,
  83. dataCategory,
  84. getFormatUsageOptions(dataCategory)
  85. )}
  86. </CellStat>,
  87. <CellStat key={3}>
  88. {formatUsageWithUnits(
  89. filtered,
  90. dataCategory,
  91. getFormatUsageOptions(dataCategory)
  92. )}
  93. </CellStat>,
  94. <CellStat key={4}>
  95. {formatUsageWithUnits(dropped, dataCategory, getFormatUsageOptions(dataCategory))}
  96. </CellStat>,
  97. <CellStat key={5}>
  98. <Button
  99. data-test-id={project.slug}
  100. size="sm"
  101. onClick={() => {
  102. this.loadProject(parseInt(stat.project.id, 10));
  103. }}
  104. >
  105. <StyledIconGraph type="bar" size="sm" />
  106. <span>View Stats</span>
  107. </Button>
  108. <Link to={stat.projectSettingsLink}>
  109. <StyledSettingsButton size="sm">
  110. <SettingsIcon size="sm" />
  111. </StyledSettingsButton>
  112. </Link>
  113. </CellStat>,
  114. ];
  115. }
  116. render() {
  117. const {isEmpty, isLoading, isError, errors, headers, usageStats} = this.props;
  118. if (isError) {
  119. return (
  120. <Panel>
  121. <ErrorPanel height="256px">{this.getErrorMessage(errors)}</ErrorPanel>
  122. </Panel>
  123. );
  124. }
  125. return (
  126. <StyledPanelTable isLoading={isLoading} isEmpty={isEmpty} headers={headers}>
  127. {usageStats.map(s => this.renderTableRow(s))}
  128. </StyledPanelTable>
  129. );
  130. }
  131. }
  132. export default withSentryRouter(UsageTable);
  133. const StyledPanelTable = styled(PanelTable)`
  134. grid-template-columns: repeat(6, auto);
  135. @media (min-width: ${p => p.theme.breakpoints.small}) {
  136. grid-template-columns: 1fr repeat(5, minmax(0, auto));
  137. }
  138. `;
  139. export const CellStat = styled('div')`
  140. display: flex;
  141. align-items: center;
  142. font-variant-numeric: tabular-nums;
  143. justify-content: right;
  144. `;
  145. export const CellProject = styled(CellStat)`
  146. justify-content: left;
  147. `;
  148. const StyledIdBadge = styled(IdBadge)`
  149. overflow: hidden;
  150. white-space: nowrap;
  151. flex-shrink: 1;
  152. `;
  153. const SettingsIcon = styled(IconSettings)`
  154. color: ${p => p.theme.gray300};
  155. align-items: center;
  156. display: inline-flex;
  157. justify-content: space-between;
  158. margin-right: ${space(1.0)};
  159. margin-left: ${space(1.0)};
  160. transition: 0.5s opacity ease-out;
  161. &:hover {
  162. color: ${p => p.theme.textColor};
  163. }
  164. `;
  165. const StyledIconGraph = styled(IconGraph)`
  166. color: ${p => p.theme.gray300};
  167. margin-right: 5px;
  168. &:hover {
  169. color: ${p => p.theme.textColor};
  170. cursor: pointer;
  171. }
  172. `;
  173. const StyledSettingsButton = styled(Button)`
  174. padding: 0px;
  175. margin-left: 7px;
  176. `;