index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import {Fragment, useEffect} from 'react';
  2. import LazyLoad from 'react-lazyload';
  3. import {RouteComponentProps} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import {withProfiler} from '@sentry/react';
  6. import flatten from 'lodash/flatten';
  7. import uniqBy from 'lodash/uniqBy';
  8. import {Client} from 'sentry/api';
  9. import Button from 'sentry/components/button';
  10. import IdBadge from 'sentry/components/idBadge';
  11. import Link from 'sentry/components/links/link';
  12. import LoadingError from 'sentry/components/loadingError';
  13. import LoadingIndicator from 'sentry/components/loadingIndicator';
  14. import NoProjectMessage from 'sentry/components/noProjectMessage';
  15. import PageHeading from 'sentry/components/pageHeading';
  16. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  17. import {IconAdd, IconUser} from 'sentry/icons';
  18. import {t} from 'sentry/locale';
  19. import ProjectsStatsStore from 'sentry/stores/projectsStatsStore';
  20. import space from 'sentry/styles/space';
  21. import {Organization, TeamWithProjects} from 'sentry/types';
  22. import {sortProjects} from 'sentry/utils';
  23. import withApi from 'sentry/utils/withApi';
  24. import withOrganization from 'sentry/utils/withOrganization';
  25. import withTeamsForUser from 'sentry/utils/withTeamsForUser';
  26. import ProjectCard from './projectCard';
  27. import Resources from './resources';
  28. import TeamSection from './teamSection';
  29. type Props = {
  30. api: Client;
  31. error: Error | null;
  32. loadingTeams: boolean;
  33. organization: Organization;
  34. teams: TeamWithProjects[];
  35. } & RouteComponentProps<{orgId: string}, {}>;
  36. function Dashboard({teams, params, organization, loadingTeams, error}: Props) {
  37. useEffect(() => {
  38. return function cleanup() {
  39. ProjectsStatsStore.reset();
  40. };
  41. }, []);
  42. if (loadingTeams) {
  43. return <LoadingIndicator />;
  44. }
  45. if (error) {
  46. return <LoadingError message={t('An error occurred while fetching your projects')} />;
  47. }
  48. const filteredTeams = teams.filter(team => team.projects.length);
  49. filteredTeams.sort((team1, team2) => team1.slug.localeCompare(team2.slug));
  50. const projects = uniqBy(flatten(teams.map(teamObj => teamObj.projects)), 'id');
  51. const favorites = projects.filter(project => project.isBookmarked);
  52. const canCreateProjects = organization.access.includes('project:admin');
  53. const canJoinTeam = organization.access.includes('team:read');
  54. const hasTeamAdminAccess = organization.access.includes('team:admin');
  55. const hasProjectAccess = organization.access.includes('project:read');
  56. const hasProjectRedesign = organization.features.includes('projects-page-redesign');
  57. const showEmptyMessage = projects.length === 0 && favorites.length === 0;
  58. const showResources = projects.length === 1 && !projects[0].firstEvent;
  59. if (showEmptyMessage) {
  60. return (
  61. <NoProjectMessage organization={organization} superuserNeedsToBeProjectMember />
  62. );
  63. }
  64. return (
  65. <Fragment>
  66. <SentryDocumentTitle title={t('Projects Dashboard')} orgSlug={organization.slug} />
  67. {projects.length > 0 && (
  68. <Fragment>
  69. <ProjectsHeader>
  70. <PageHeading>{t('Projects')}</PageHeading>
  71. <ButtonContainer>
  72. {hasProjectRedesign && (
  73. <Button
  74. size="small"
  75. icon={<IconUser size="xs" />}
  76. title={
  77. canJoinTeam
  78. ? undefined
  79. : t('You do not have permission to join a team.')
  80. }
  81. disabled={!canJoinTeam}
  82. to={`/settings/${organization.slug}/teams/`}
  83. data-test-id="join-team"
  84. >
  85. {t('Join a Team')}
  86. </Button>
  87. )}
  88. <Button
  89. size="small"
  90. priority={hasProjectRedesign ? 'primary' : 'default'}
  91. disabled={!canCreateProjects}
  92. title={
  93. !canCreateProjects
  94. ? t('You do not have permission to create projects')
  95. : undefined
  96. }
  97. to={`/organizations/${organization.slug}/projects/new/`}
  98. icon={<IconAdd size="xs" isCircled />}
  99. data-test-id="create-project"
  100. >
  101. {t('Create Project')}
  102. </Button>
  103. </ButtonContainer>
  104. </ProjectsHeader>
  105. </Fragment>
  106. )}
  107. {hasProjectRedesign ? (
  108. <LazyLoad once debounce={50} height={300} offset={300}>
  109. <ProjectCardsContainer>
  110. <ProjectCards>
  111. {projects.map(project => (
  112. <ProjectCard
  113. data-test-id={project.slug}
  114. key={project.slug}
  115. project={project}
  116. hasProjectAccess={hasProjectAccess}
  117. />
  118. ))}
  119. </ProjectCards>
  120. </ProjectCardsContainer>
  121. </LazyLoad>
  122. ) : (
  123. filteredTeams.map((team, index) => (
  124. <LazyLoad key={team.slug} once debounce={50} height={300} offset={300}>
  125. <TeamSection
  126. orgId={params.orgId}
  127. team={team}
  128. showBorder={index !== teams.length - 1}
  129. title={
  130. hasTeamAdminAccess ? (
  131. <TeamLink to={`/settings/${organization.slug}/teams/${team.slug}/`}>
  132. <IdBadge team={team} avatarSize={22} />
  133. </TeamLink>
  134. ) : (
  135. <IdBadge team={team} avatarSize={22} />
  136. )
  137. }
  138. projects={sortProjects(team.projects)}
  139. access={new Set(organization.access)}
  140. />
  141. </LazyLoad>
  142. ))
  143. )}
  144. {showResources && <Resources organization={organization} />}
  145. </Fragment>
  146. );
  147. }
  148. const OrganizationDashboard = (props: Props) => (
  149. <OrganizationDashboardWrapper>
  150. <Dashboard {...props} />
  151. </OrganizationDashboardWrapper>
  152. );
  153. const TeamLink = styled(Link)`
  154. display: flex;
  155. align-items: center;
  156. `;
  157. const ProjectsHeader = styled('div')`
  158. padding: ${space(3)} ${space(4)} 0 ${space(4)};
  159. display: flex;
  160. align-items: center;
  161. justify-content: space-between;
  162. `;
  163. const ButtonContainer = styled('div')`
  164. display: inline-flex;
  165. gap: ${space(1)};
  166. `;
  167. const ProjectCardsContainer = styled('div')`
  168. padding: 20px 30px 0 30px;
  169. `;
  170. const ProjectCards = styled('div')`
  171. display: grid;
  172. grid-template-columns: minmax(100px, 1fr);
  173. gap: ${space(3)};
  174. @media (min-width: ${p => p.theme.breakpoints[0]}) {
  175. grid-template-columns: repeat(2, minmax(100px, 1fr));
  176. }
  177. @media (min-width: ${p => p.theme.breakpoints[3]}) {
  178. grid-template-columns: repeat(3, minmax(100px, 1fr));
  179. }
  180. `;
  181. const OrganizationDashboardWrapper = styled('div')`
  182. display: flex;
  183. flex: 1;
  184. flex-direction: column;
  185. `;
  186. export {Dashboard};
  187. export default withApi(
  188. withOrganization(withTeamsForUser(withProfiler(OrganizationDashboard)))
  189. );