index.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {Button} from 'sentry/components/button';
  6. import EmptyMessage from 'sentry/components/emptyMessage';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import Pagination from 'sentry/components/pagination';
  9. import {Panel, PanelBody, PanelHeader, PanelItem} from 'sentry/components/panels';
  10. import Placeholder from 'sentry/components/placeholder';
  11. import {IconAdd} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {Organization, Project} from 'sentry/types';
  15. import {sortProjects} from 'sentry/utils';
  16. import {decodeScalar} from 'sentry/utils/queryString';
  17. import routeTitleGen from 'sentry/utils/routeTitle';
  18. import withOrganization from 'sentry/utils/withOrganization';
  19. import AsyncView from 'sentry/views/asyncView';
  20. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  21. import ProjectListItem from 'sentry/views/settings/components/settingsProjectItem';
  22. import ProjectStatsGraph from './projectStatsGraph';
  23. const ITEMS_PER_PAGE = 50;
  24. type Props = {
  25. location: Location;
  26. organization: Organization;
  27. } & RouteComponentProps<{}, {}>;
  28. type ProjectStats = Record<string, Required<Project['stats']>>;
  29. type State = AsyncView['state'] & {
  30. projectList: Project[] | null;
  31. projectListPageLinks: string | null;
  32. projectStats: ProjectStats | null;
  33. };
  34. class OrganizationProjects extends AsyncView<Props, State> {
  35. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  36. const {location, organization} = this.props;
  37. const query = decodeScalar(location.query.query);
  38. return [
  39. [
  40. 'projectList',
  41. `/organizations/${organization.slug}/projects/`,
  42. {
  43. query: {
  44. query,
  45. per_page: ITEMS_PER_PAGE,
  46. },
  47. },
  48. ],
  49. [
  50. 'projectStats',
  51. `/organizations/${organization.slug}/stats/`,
  52. {
  53. query: {
  54. since: new Date().getTime() / 1000 - 3600 * 24,
  55. stat: 'generated',
  56. group: 'project',
  57. per_page: ITEMS_PER_PAGE,
  58. },
  59. },
  60. ],
  61. ];
  62. }
  63. getTitle(): string {
  64. const {organization} = this.props;
  65. return routeTitleGen(t('Projects'), organization.slug, false);
  66. }
  67. renderLoading(): React.ReactNode {
  68. return this.renderBody();
  69. }
  70. renderBody(): React.ReactNode {
  71. const {projectList, projectListPageLinks, projectStats} = this.state;
  72. const {organization} = this.props;
  73. const canCreateProjects = new Set(organization.access).has('project:admin');
  74. const action = (
  75. <Button
  76. priority="primary"
  77. size="sm"
  78. disabled={!canCreateProjects}
  79. title={
  80. !canCreateProjects
  81. ? t('You do not have permission to create projects')
  82. : undefined
  83. }
  84. to={`/organizations/${organization.slug}/projects/new/`}
  85. icon={<IconAdd size="xs" isCircled />}
  86. >
  87. {t('Create Project')}
  88. </Button>
  89. );
  90. return (
  91. <Fragment>
  92. <SettingsPageHeader title="Projects" action={action} />
  93. <SearchWrapper>
  94. {this.renderSearchInput({
  95. updateRoute: true,
  96. placeholder: t('Search Projects'),
  97. className: 'search',
  98. })}
  99. </SearchWrapper>
  100. <Panel>
  101. <PanelHeader>{t('Projects')}</PanelHeader>
  102. <PanelBody>
  103. {projectList ? (
  104. sortProjects(projectList).map(project => (
  105. <GridPanelItem key={project.id}>
  106. <ProjectListItemWrapper>
  107. <ProjectListItem project={project} organization={organization} />
  108. </ProjectListItemWrapper>
  109. <ProjectStatsGraphWrapper>
  110. {projectStats ? (
  111. <ProjectStatsGraph
  112. key={project.id}
  113. project={project}
  114. stats={projectStats[project.id]}
  115. />
  116. ) : (
  117. <Placeholder height="25px" />
  118. )}
  119. </ProjectStatsGraphWrapper>
  120. </GridPanelItem>
  121. ))
  122. ) : (
  123. <LoadingIndicator />
  124. )}
  125. {projectList && projectList.length === 0 && (
  126. <EmptyMessage>{t('No projects found.')}</EmptyMessage>
  127. )}
  128. </PanelBody>
  129. </Panel>
  130. {projectListPageLinks && (
  131. <Pagination pageLinks={projectListPageLinks} {...this.props} />
  132. )}
  133. </Fragment>
  134. );
  135. }
  136. }
  137. export default withOrganization(OrganizationProjects);
  138. const SearchWrapper = styled('div')`
  139. margin-bottom: ${space(2)};
  140. `;
  141. const GridPanelItem = styled(PanelItem)`
  142. display: flex;
  143. align-items: center;
  144. padding: 0;
  145. `;
  146. const ProjectListItemWrapper = styled('div')`
  147. padding: ${space(2)};
  148. flex: 1;
  149. `;
  150. const ProjectStatsGraphWrapper = styled('div')`
  151. padding: ${space(2)};
  152. width: 25%;
  153. margin-left: ${space(2)};
  154. `;