index.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import {Fragment, useEffect, useMemo, useState} 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 debounce from 'lodash/debounce';
  7. import flatten from 'lodash/flatten';
  8. import uniqBy from 'lodash/uniqBy';
  9. import {Client} from 'sentry/api';
  10. import Button from 'sentry/components/button';
  11. import * as Layout from 'sentry/components/layouts/thirds';
  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 SearchBar from 'sentry/components/searchBar';
  17. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  18. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  19. import {IconAdd, IconUser} from 'sentry/icons';
  20. import {t} from 'sentry/locale';
  21. import ProjectsStatsStore from 'sentry/stores/projectsStatsStore';
  22. import space from 'sentry/styles/space';
  23. import {Organization, TeamWithProjects} from 'sentry/types';
  24. import {sortProjects} from 'sentry/utils';
  25. import withApi from 'sentry/utils/withApi';
  26. import withOrganization from 'sentry/utils/withOrganization';
  27. import withTeamsForUser from 'sentry/utils/withTeamsForUser';
  28. import TeamFilter from 'sentry/views/alerts/list/rules/teamFilter';
  29. import ProjectCard from './projectCard';
  30. import Resources from './resources';
  31. import {getTeamParams} from './utils';
  32. type Props = {
  33. api: Client;
  34. error: Error | null;
  35. loadingTeams: boolean;
  36. organization: Organization;
  37. teams: TeamWithProjects[];
  38. } & RouteComponentProps<{orgId: string}, {}>;
  39. function Dashboard({teams, organization, loadingTeams, error, router, location}: Props) {
  40. useEffect(() => {
  41. return function cleanup() {
  42. ProjectsStatsStore.reset();
  43. };
  44. }, []);
  45. const [projectQuery, setProjectQuery] = useState('');
  46. const debouncedSearchQuery = useMemo(
  47. () => debounce(handleSearch, DEFAULT_DEBOUNCE_DURATION),
  48. []
  49. );
  50. if (loadingTeams) {
  51. return <LoadingIndicator />;
  52. }
  53. if (error) {
  54. return <LoadingError message={t('An error occurred while fetching your projects')} />;
  55. }
  56. const canCreateProjects = organization.access.includes('project:admin');
  57. const canJoinTeam = organization.access.includes('team:read');
  58. const hasProjectAccess = organization.access.includes('project:read');
  59. const selectedTeams = getTeamParams(location ? location.query.team : '');
  60. const filteredTeams = teams.filter(team => selectedTeams.includes(team.id));
  61. const filteredTeamProjects = uniqBy(
  62. flatten((filteredTeams ?? teams).map(team => team.projects)),
  63. 'id'
  64. );
  65. const projects = uniqBy(flatten(teams.map(teamObj => teamObj.projects)), 'id');
  66. const currentProjects = selectedTeams.length === 0 ? projects : filteredTeamProjects;
  67. const filteredProjects = (currentProjects ?? projects).filter(project =>
  68. project.slug.includes(projectQuery)
  69. );
  70. const favorites = projects.filter(project => project.isBookmarked);
  71. const showEmptyMessage = projects.length === 0 && favorites.length === 0;
  72. const showResources = projects.length === 1 && !projects[0].firstEvent;
  73. function handleSearch(searchQuery: string) {
  74. setProjectQuery(searchQuery);
  75. }
  76. function handleChangeFilter(activeFilters: string[]) {
  77. const {...currentQuery} = location.query;
  78. router.push({
  79. pathname: location.pathname,
  80. query: {
  81. ...currentQuery,
  82. team: activeFilters.length > 0 ? activeFilters : '',
  83. },
  84. });
  85. }
  86. if (showEmptyMessage) {
  87. return (
  88. <NoProjectMessage organization={organization} superuserNeedsToBeProjectMember />
  89. );
  90. }
  91. return (
  92. <Fragment>
  93. <SentryDocumentTitle title={t('Projects Dashboard')} orgSlug={organization.slug} />
  94. {projects.length > 0 && (
  95. <Fragment>
  96. <ProjectsHeader>
  97. <Title>
  98. <PageHeading>{t('Projects')}</PageHeading>
  99. </Title>
  100. <Layout.HeaderActions>
  101. <ButtonContainer>
  102. <Button
  103. icon={<IconUser size="xs" />}
  104. title={
  105. canJoinTeam
  106. ? undefined
  107. : t('You do not have permission to join a team.')
  108. }
  109. disabled={!canJoinTeam}
  110. to={`/settings/${organization.slug}/teams/`}
  111. data-test-id="join-team"
  112. >
  113. {t('Join a Team')}
  114. </Button>
  115. <Button
  116. priority="primary"
  117. disabled={!canCreateProjects}
  118. title={
  119. !canCreateProjects
  120. ? t('You do not have permission to create projects')
  121. : undefined
  122. }
  123. to={`/organizations/${organization.slug}/projects/new/`}
  124. icon={<IconAdd size="xs" isCircled />}
  125. data-test-id="create-project"
  126. >
  127. {t('Create Project')}
  128. </Button>
  129. </ButtonContainer>
  130. </Layout.HeaderActions>
  131. </ProjectsHeader>
  132. <Body>
  133. <Layout.Main fullWidth>
  134. <SearchAndSelectorWrapper>
  135. <TeamFilter
  136. selectedTeams={selectedTeams}
  137. handleChangeFilter={handleChangeFilter}
  138. showIsMemberTeams
  139. showSuggestedOptions={false}
  140. showMyTeamsDescription
  141. />
  142. <StyledSearchBar
  143. defaultQuery=""
  144. placeholder={t('Search for projects by name')}
  145. onChange={debouncedSearchQuery}
  146. query={projectQuery}
  147. />
  148. </SearchAndSelectorWrapper>
  149. <LazyLoad once debounce={50} height={300} offset={300}>
  150. <ProjectCards>
  151. {sortProjects(filteredProjects).map(project => (
  152. <ProjectCard
  153. data-test-id={project.slug}
  154. key={project.slug}
  155. project={project}
  156. hasProjectAccess={hasProjectAccess}
  157. />
  158. ))}
  159. </ProjectCards>
  160. </LazyLoad>
  161. </Layout.Main>
  162. </Body>
  163. {showResources && <Resources organization={organization} />}
  164. </Fragment>
  165. )}
  166. </Fragment>
  167. );
  168. }
  169. const OrganizationDashboard = (props: Props) => (
  170. <OrganizationDashboardWrapper>
  171. <Dashboard {...props} />
  172. </OrganizationDashboardWrapper>
  173. );
  174. const ProjectsHeader = styled(Layout.Header)`
  175. border-bottom: none;
  176. align-items: end;
  177. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  178. padding: 26px ${space(4)} 0 ${space(4)};
  179. }
  180. `;
  181. const Title = styled(Layout.HeaderContent)`
  182. margin-bottom: 0;
  183. `;
  184. const ButtonContainer = styled('div')`
  185. display: inline-flex;
  186. gap: ${space(1)};
  187. `;
  188. const SearchAndSelectorWrapper = styled('div')`
  189. display: flex;
  190. gap: ${space(2)};
  191. justify-content: flex-end;
  192. align-items: flex-end;
  193. margin-bottom: ${space(2)};
  194. @media (max-width: ${p => p.theme.breakpoints.small}) {
  195. display: block;
  196. }
  197. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  198. display: flex;
  199. }
  200. `;
  201. const StyledSearchBar = styled(SearchBar)`
  202. flex-grow: 1;
  203. @media (max-width: ${p => p.theme.breakpoints.small}) {
  204. margin-top: ${space(1)};
  205. }
  206. `;
  207. const Body = styled(Layout.Body)`
  208. padding-top: ${space(2)} !important;
  209. background-color: ${p => p.theme.surface100};
  210. `;
  211. const ProjectCards = styled('div')`
  212. display: grid;
  213. grid-template-columns: minmax(100px, 1fr);
  214. gap: ${space(3)};
  215. @media (min-width: ${p => p.theme.breakpoints.small}) {
  216. grid-template-columns: repeat(2, minmax(100px, 1fr));
  217. }
  218. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  219. grid-template-columns: repeat(3, minmax(100px, 1fr));
  220. }
  221. `;
  222. const OrganizationDashboardWrapper = styled('div')`
  223. display: flex;
  224. flex: 1;
  225. flex-direction: column;
  226. `;
  227. export {Dashboard};
  228. export default withApi(
  229. withOrganization(withTeamsForUser(withProfiler(OrganizationDashboard)))
  230. );