index.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. size="sm"
  104. icon={<IconUser size="xs" />}
  105. title={
  106. canJoinTeam
  107. ? undefined
  108. : t('You do not have permission to join a team.')
  109. }
  110. disabled={!canJoinTeam}
  111. to={`/settings/${organization.slug}/teams/`}
  112. data-test-id="join-team"
  113. >
  114. {t('Join a Team')}
  115. </Button>
  116. <Button
  117. size="sm"
  118. priority="primary"
  119. disabled={!canCreateProjects}
  120. title={
  121. !canCreateProjects
  122. ? t('You do not have permission to create projects')
  123. : undefined
  124. }
  125. to={`/organizations/${organization.slug}/projects/new/`}
  126. icon={<IconAdd size="xs" isCircled />}
  127. data-test-id="create-project"
  128. >
  129. {t('Create Project')}
  130. </Button>
  131. </ButtonContainer>
  132. </Layout.HeaderActions>
  133. </ProjectsHeader>
  134. <Body>
  135. <Layout.Main fullWidth>
  136. <SearchAndSelectorWrapper>
  137. <TeamFilter
  138. selectedTeams={selectedTeams}
  139. handleChangeFilter={handleChangeFilter}
  140. showIsMemberTeams
  141. showSuggestedOptions={false}
  142. showMyTeamsDescription
  143. />
  144. <StyledSearchBar
  145. defaultQuery=""
  146. placeholder={t('Search for projects by name')}
  147. onChange={debouncedSearchQuery}
  148. query={projectQuery}
  149. />
  150. </SearchAndSelectorWrapper>
  151. <LazyLoad once debounce={50} height={300} offset={300}>
  152. <ProjectCards>
  153. {sortProjects(filteredProjects).map(project => (
  154. <ProjectCard
  155. data-test-id={project.slug}
  156. key={project.slug}
  157. project={project}
  158. hasProjectAccess={hasProjectAccess}
  159. />
  160. ))}
  161. </ProjectCards>
  162. </LazyLoad>
  163. </Layout.Main>
  164. </Body>
  165. {showResources && <Resources organization={organization} />}
  166. </Fragment>
  167. )}
  168. </Fragment>
  169. );
  170. }
  171. const OrganizationDashboard = (props: Props) => (
  172. <OrganizationDashboardWrapper>
  173. <Dashboard {...props} />
  174. </OrganizationDashboardWrapper>
  175. );
  176. const ProjectsHeader = styled(Layout.Header)`
  177. border-bottom: none;
  178. align-items: end;
  179. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  180. padding: 26px ${space(4)} 0 ${space(4)};
  181. }
  182. `;
  183. const Title = styled(Layout.HeaderContent)`
  184. margin-bottom: 0;
  185. `;
  186. const ButtonContainer = styled('div')`
  187. display: inline-flex;
  188. gap: ${space(1)};
  189. `;
  190. const SearchAndSelectorWrapper = styled('div')`
  191. display: flex;
  192. gap: ${space(2)};
  193. justify-content: flex-end;
  194. align-items: flex-end;
  195. margin-bottom: ${space(2)};
  196. @media (max-width: ${p => p.theme.breakpoints.small}) {
  197. display: block;
  198. }
  199. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  200. display: flex;
  201. }
  202. `;
  203. const StyledSearchBar = styled(SearchBar)`
  204. flex-grow: 1;
  205. @media (max-width: ${p => p.theme.breakpoints.small}) {
  206. margin-top: ${space(1)};
  207. }
  208. `;
  209. const Body = styled(Layout.Body)`
  210. padding-top: ${space(2)} !important;
  211. background-color: ${p => p.theme.surface100};
  212. `;
  213. const ProjectCards = styled('div')`
  214. display: grid;
  215. grid-template-columns: minmax(100px, 1fr);
  216. gap: ${space(3)};
  217. @media (min-width: ${p => p.theme.breakpoints.small}) {
  218. grid-template-columns: repeat(2, minmax(100px, 1fr));
  219. }
  220. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  221. grid-template-columns: repeat(3, minmax(100px, 1fr));
  222. }
  223. `;
  224. const OrganizationDashboardWrapper = styled('div')`
  225. display: flex;
  226. flex: 1;
  227. flex-direction: column;
  228. `;
  229. export {Dashboard};
  230. export default withApi(
  231. withOrganization(withTeamsForUser(withProfiler(OrganizationDashboard)))
  232. );