index.tsx 8.4 KB

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