index.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import {Fragment, useEffect, useMemo, useState} from 'react';
  2. import LazyLoad, {forceCheck} from 'react-lazyload';
  3. import type {RouteComponentProps} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import {withProfiler} from '@sentry/react';
  6. import debounce from 'lodash/debounce';
  7. import uniqBy from 'lodash/uniqBy';
  8. import type {Client} from 'sentry/api';
  9. import {Button} from 'sentry/components/button';
  10. import ButtonBar from 'sentry/components/buttonBar';
  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 {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  16. import {useProjectCreationAccess} from 'sentry/components/projects/useProjectCreationAccess';
  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 type {Organization} from 'sentry/types/organization';
  25. import type {Project, TeamWithProjects} from 'sentry/types/project';
  26. import {
  27. onRenderCallback,
  28. Profiler,
  29. setGroupedEntityTag,
  30. } from 'sentry/utils/performanceForSentry';
  31. import {sortProjects} from 'sentry/utils/project/sortProjects';
  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});
  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. (filteredTeams ?? teams).flatMap(team => team.projects),
  98. 'id'
  99. );
  100. const projects = uniqBy(
  101. teams.flatMap(teamObj => teamObj.projects),
  102. 'id'
  103. );
  104. setGroupedEntityTag('projects.total', 1000, projects.length);
  105. const currentProjects = selectedTeams.length === 0 ? projects : filteredTeamProjects;
  106. const filteredProjects = (currentProjects ?? projects).filter(project =>
  107. project.slug.includes(projectQuery)
  108. );
  109. const showResources = projects.length === 1 && !projects[0].firstEvent;
  110. function handleSearch(searchQuery: string) {
  111. setProjectQuery(searchQuery);
  112. }
  113. function handleChangeFilter(activeFilters: string[]) {
  114. const {...currentQuery} = location.query;
  115. router.push({
  116. pathname: location.pathname,
  117. query: {
  118. ...currentQuery,
  119. team: activeFilters.length > 0 ? activeFilters : '',
  120. },
  121. });
  122. }
  123. return (
  124. <Fragment>
  125. <SentryDocumentTitle title={t('Projects Dashboard')} orgSlug={organization.slug} />
  126. <Layout.Header>
  127. <Layout.HeaderContent>
  128. <Layout.Title>
  129. {t('Projects')}
  130. <PageHeadingQuestionTooltip
  131. docsUrl="https://docs.sentry.io/product/projects/"
  132. title={t(
  133. "A high-level overview of errors, transactions, and deployments filtered by teams you're part of."
  134. )}
  135. />
  136. </Layout.Title>
  137. </Layout.HeaderContent>
  138. <Layout.HeaderActions>
  139. <ButtonBar gap={1}>
  140. <Button
  141. size="sm"
  142. icon={<IconUser />}
  143. title={
  144. canJoinTeam ? undefined : t('You do not have permission to join a team.')
  145. }
  146. disabled={!canJoinTeam}
  147. to={`/settings/${organization.slug}/teams/`}
  148. data-test-id="join-team"
  149. >
  150. {t('Join a Team')}
  151. </Button>
  152. <Button
  153. size="sm"
  154. priority="primary"
  155. disabled={!canCreateProject}
  156. title={
  157. !canCreateProject
  158. ? t('You do not have permission to create projects')
  159. : undefined
  160. }
  161. to={`/organizations/${organization.slug}/projects/new/`}
  162. icon={<IconAdd isCircled />}
  163. data-test-id="create-project"
  164. >
  165. {t('Create Project')}
  166. </Button>
  167. </ButtonBar>
  168. </Layout.HeaderActions>
  169. </Layout.Header>
  170. <Layout.Body>
  171. <Layout.Main fullWidth>
  172. <SearchAndSelectorWrapper>
  173. <TeamFilter
  174. selectedTeams={selectedTeams}
  175. handleChangeFilter={handleChangeFilter}
  176. showIsMemberTeams
  177. showSuggestedOptions={false}
  178. showMyTeamsDescription
  179. />
  180. <StyledSearchBar
  181. defaultQuery=""
  182. placeholder={t('Search for projects by name')}
  183. onChange={debouncedSearchQuery}
  184. query={projectQuery}
  185. />
  186. </SearchAndSelectorWrapper>
  187. <Profiler id="ProjectCardList" onRender={onRenderCallback}>
  188. <ProjectCardList projects={filteredProjects} />
  189. </Profiler>
  190. </Layout.Main>
  191. </Layout.Body>
  192. {showResources && <Resources organization={organization} />}
  193. </Fragment>
  194. );
  195. }
  196. function OrganizationDashboard(props: Props) {
  197. return (
  198. <Layout.Page>
  199. <NoProjectMessage organization={props.organization}>
  200. <Dashboard {...props} />
  201. </NoProjectMessage>
  202. </Layout.Page>
  203. );
  204. }
  205. const SearchAndSelectorWrapper = styled('div')`
  206. display: flex;
  207. gap: ${space(2)};
  208. justify-content: flex-end;
  209. align-items: flex-end;
  210. margin-bottom: ${space(2)};
  211. @media (max-width: ${p => p.theme.breakpoints.small}) {
  212. display: block;
  213. }
  214. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  215. display: flex;
  216. }
  217. `;
  218. const StyledSearchBar = styled(SearchBar)`
  219. flex-grow: 1;
  220. @media (max-width: ${p => p.theme.breakpoints.small}) {
  221. margin-top: ${space(1)};
  222. }
  223. `;
  224. const ProjectCards = styled('div')`
  225. display: grid;
  226. gap: ${space(3)};
  227. grid-template-columns: repeat(auto-fill, minmax(1fr, 400px));
  228. @media (min-width: ${p => p.theme.breakpoints.small}) {
  229. grid-template-columns: repeat(auto-fill, minmax(470px, 1fr));
  230. }
  231. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  232. grid-template-columns: repeat(auto-fill, minmax(450px, 1fr));
  233. }
  234. `;
  235. export {Dashboard};
  236. export default withApi(
  237. withOrganization(withTeamsForUser(withProfiler(OrganizationDashboard)))
  238. );