index.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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<{orgId: string}, {}>;
  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 {orgId} = this.props.params;
  37. const {location} = this.props;
  38. const query = decodeScalar(location.query.query);
  39. return [
  40. [
  41. 'projectList',
  42. `/organizations/${orgId}/projects/`,
  43. {
  44. query: {
  45. query,
  46. per_page: ITEMS_PER_PAGE,
  47. },
  48. },
  49. ],
  50. [
  51. 'projectStats',
  52. `/organizations/${orgId}/stats/`,
  53. {
  54. query: {
  55. since: new Date().getTime() / 1000 - 3600 * 24,
  56. stat: 'generated',
  57. group: 'project',
  58. per_page: ITEMS_PER_PAGE,
  59. },
  60. },
  61. ],
  62. ];
  63. }
  64. getTitle(): string {
  65. const {organization} = this.props;
  66. return routeTitleGen(t('Projects'), organization.slug, false);
  67. }
  68. renderLoading(): React.ReactNode {
  69. return this.renderBody();
  70. }
  71. renderBody(): React.ReactNode {
  72. const {projectList, projectListPageLinks, projectStats} = this.state;
  73. const {organization} = this.props;
  74. const canCreateProjects = new Set(organization.access).has('project:admin');
  75. const action = (
  76. <Button
  77. priority="primary"
  78. size="sm"
  79. disabled={!canCreateProjects}
  80. title={
  81. !canCreateProjects
  82. ? t('You do not have permission to create projects')
  83. : undefined
  84. }
  85. to={`/organizations/${organization.slug}/projects/new/`}
  86. icon={<IconAdd size="xs" isCircled />}
  87. >
  88. {t('Create Project')}
  89. </Button>
  90. );
  91. return (
  92. <Fragment>
  93. <SettingsPageHeader title="Projects" action={action} />
  94. <SearchWrapper>
  95. {this.renderSearchInput({
  96. updateRoute: true,
  97. placeholder: t('Search Projects'),
  98. className: 'search',
  99. })}
  100. </SearchWrapper>
  101. <Panel>
  102. <PanelHeader>{t('Projects')}</PanelHeader>
  103. <PanelBody>
  104. {projectList ? (
  105. sortProjects(projectList).map(project => (
  106. <GridPanelItem key={project.id}>
  107. <ProjectListItemWrapper>
  108. <ProjectListItem project={project} organization={organization} />
  109. </ProjectListItemWrapper>
  110. <ProjectStatsGraphWrapper>
  111. {projectStats ? (
  112. <ProjectStatsGraph
  113. key={project.id}
  114. project={project}
  115. stats={projectStats[project.id]}
  116. />
  117. ) : (
  118. <Placeholder height="25px" />
  119. )}
  120. </ProjectStatsGraphWrapper>
  121. </GridPanelItem>
  122. ))
  123. ) : (
  124. <LoadingIndicator />
  125. )}
  126. {projectList && projectList.length === 0 && (
  127. <EmptyMessage>{t('No projects found.')}</EmptyMessage>
  128. )}
  129. </PanelBody>
  130. </Panel>
  131. {projectListPageLinks && (
  132. <Pagination pageLinks={projectListPageLinks} {...this.props} />
  133. )}
  134. </Fragment>
  135. );
  136. }
  137. }
  138. export default withOrganization(OrganizationProjects);
  139. const SearchWrapper = styled('div')`
  140. margin-bottom: ${space(2)};
  141. `;
  142. const GridPanelItem = styled(PanelItem)`
  143. display: flex;
  144. align-items: center;
  145. padding: 0;
  146. `;
  147. const ProjectListItemWrapper = styled('div')`
  148. padding: ${space(2)};
  149. flex: 1;
  150. `;
  151. const ProjectStatsGraphWrapper = styled('div')`
  152. padding: ${space(2)};
  153. width: 25%;
  154. margin-left: ${space(2)};
  155. `;