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