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