index.tsx 8.2 KB

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