index.tsx 8.3 KB

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