usageTable.tsx 5.6 KB

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