index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import ErrorPanel from 'sentry/components/charts/errorPanel';
  4. import EmptyMessage from 'sentry/components/emptyMessage';
  5. import IdBadge from 'sentry/components/idBadge';
  6. import ExternalLink from 'sentry/components/links/externalLink';
  7. import Link from 'sentry/components/links/link';
  8. import {Panel} from 'sentry/components/panels';
  9. import PanelTable from 'sentry/components/panels/panelTable';
  10. import {IconSettings, IconWarning} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import {DataCategoryInfo, Project} from 'sentry/types';
  14. import {formatUsageWithUnits, getFormatUsageOptions} from '../utils';
  15. const DOCS_URL = 'https://docs.sentry.io/product/accounts/membership/#restricting-access';
  16. type Props = {
  17. dataCategory: DataCategoryInfo['plural'];
  18. headers: React.ReactNode[];
  19. usageStats: TableStat[];
  20. errors?: Record<string, Error>;
  21. isEmpty?: boolean;
  22. isError?: boolean;
  23. isLoading?: boolean;
  24. };
  25. export type TableStat = {
  26. accepted: number;
  27. dropped: number;
  28. filtered: number;
  29. project: Project;
  30. projectLink: string;
  31. projectSettingsLink: string;
  32. total: number;
  33. };
  34. class UsageTable extends Component<Props> {
  35. getErrorMessage = errorMessage => {
  36. if (errorMessage.projectStats.responseJSON.detail === 'No projects available') {
  37. return (
  38. <EmptyMessage
  39. icon={<IconWarning color="gray300" legacySize="48px" />}
  40. title={t(
  41. "You don't have access to any projects, or your organization has no projects."
  42. )}
  43. description={tct('Learn more about [link:Project Access]', {
  44. link: <ExternalLink href={DOCS_URL} />,
  45. })}
  46. />
  47. );
  48. }
  49. return <IconWarning color="gray300" legacySize="48px" />;
  50. };
  51. renderTableRow(stat: TableStat & {project: Project}) {
  52. const {dataCategory} = this.props;
  53. const {project, total, accepted, filtered, dropped} = stat;
  54. return [
  55. <CellProject key={0}>
  56. <Link to={stat.projectLink}>
  57. <StyledIdBadge
  58. avatarSize={16}
  59. disableLink
  60. hideOverflow
  61. project={project}
  62. displayName={project.slug}
  63. />
  64. </Link>
  65. <SettingsIconLink to={stat.projectSettingsLink}>
  66. <IconSettings size="sm" />
  67. </SettingsIconLink>
  68. </CellProject>,
  69. <CellStat key={1}>
  70. {formatUsageWithUnits(total, dataCategory, getFormatUsageOptions(dataCategory))}
  71. </CellStat>,
  72. <CellStat key={2}>
  73. {formatUsageWithUnits(
  74. accepted,
  75. dataCategory,
  76. getFormatUsageOptions(dataCategory)
  77. )}
  78. </CellStat>,
  79. <CellStat key={3}>
  80. {formatUsageWithUnits(
  81. filtered,
  82. dataCategory,
  83. getFormatUsageOptions(dataCategory)
  84. )}
  85. </CellStat>,
  86. <CellStat key={4}>
  87. {formatUsageWithUnits(dropped, dataCategory, getFormatUsageOptions(dataCategory))}
  88. </CellStat>,
  89. ];
  90. }
  91. render() {
  92. const {isEmpty, isLoading, isError, errors, headers, usageStats} = this.props;
  93. if (isError) {
  94. return (
  95. <Panel>
  96. <ErrorPanel height="256px">{this.getErrorMessage(errors)}</ErrorPanel>
  97. </Panel>
  98. );
  99. }
  100. return (
  101. <StyledPanelTable isLoading={isLoading} isEmpty={isEmpty} headers={headers}>
  102. {usageStats.map(s => this.renderTableRow(s))}
  103. </StyledPanelTable>
  104. );
  105. }
  106. }
  107. export default UsageTable;
  108. const StyledPanelTable = styled(PanelTable)`
  109. grid-template-columns: repeat(5, auto);
  110. @media (min-width: ${p => p.theme.breakpoints.small}) {
  111. grid-template-columns: 1fr repeat(4, minmax(0, auto));
  112. }
  113. `;
  114. export const CellStat = styled('div')`
  115. flex-shrink: 1;
  116. text-align: right;
  117. font-variant-numeric: tabular-nums;
  118. `;
  119. export const CellProject = styled(CellStat)`
  120. display: flex;
  121. align-items: center;
  122. text-align: left;
  123. `;
  124. const StyledIdBadge = styled(IdBadge)`
  125. overflow: hidden;
  126. white-space: nowrap;
  127. flex-shrink: 1;
  128. `;
  129. const SettingsIconLink = styled(Link)`
  130. color: ${p => p.theme.gray300};
  131. align-items: center;
  132. display: inline-flex;
  133. justify-content: space-between;
  134. margin-right: ${space(1.5)};
  135. margin-left: ${space(1.0)};
  136. transition: 0.5s opacity ease-out;
  137. &:hover {
  138. color: ${p => p.theme.textColor};
  139. }
  140. `;