index.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 {setTag, 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} 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 setProjectDataTags(totalProjects: number) {
  71. const countGroup = [0, 1, 5, 10, 50, 100, 500, 1000, Infinity].find(
  72. n => totalProjects <= n
  73. );
  74. setTag('projects.total.grouped', `<=${countGroup}`);
  75. }
  76. function Dashboard({teams, organization, loadingTeams, error, router, location}: Props) {
  77. useEffect(() => {
  78. return function cleanup() {
  79. ProjectsStatsStore.reset();
  80. };
  81. }, []);
  82. const [projectQuery, setProjectQuery] = useState('');
  83. const debouncedSearchQuery = useMemo(
  84. () => debounce(handleSearch, DEFAULT_DEBOUNCE_DURATION),
  85. []
  86. );
  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 canCreateProjects = organization.access.includes('project:admin');
  94. const canJoinTeam = organization.access.includes('team:read');
  95. const selectedTeams = getTeamParams(location ? location.query.team : '');
  96. const filteredTeams = teams.filter(team => selectedTeams.includes(team.id));
  97. const filteredTeamProjects = uniqBy(
  98. flatten((filteredTeams ?? teams).map(team => team.projects)),
  99. 'id'
  100. );
  101. const projects = uniqBy(flatten(teams.map(teamObj => teamObj.projects)), 'id');
  102. setProjectDataTags(projects.length);
  103. const currentProjects = selectedTeams.length === 0 ? projects : filteredTeamProjects;
  104. const filteredProjects = (currentProjects ?? projects).filter(project =>
  105. project.slug.includes(projectQuery)
  106. );
  107. const showResources = projects.length === 1 && !projects[0].firstEvent;
  108. function handleSearch(searchQuery: string) {
  109. setProjectQuery(searchQuery);
  110. }
  111. function handleChangeFilter(activeFilters: string[]) {
  112. const {...currentQuery} = location.query;
  113. router.push({
  114. pathname: location.pathname,
  115. query: {
  116. ...currentQuery,
  117. team: activeFilters.length > 0 ? activeFilters : '',
  118. },
  119. });
  120. }
  121. return (
  122. <Fragment>
  123. <SentryDocumentTitle title={t('Projects Dashboard')} orgSlug={organization.slug} />
  124. <Layout.Header>
  125. <Layout.HeaderContent>
  126. <Layout.Title>
  127. {t('Projects')}
  128. <PageHeadingQuestionTooltip
  129. docsUrl="https://docs.sentry.io/product/projects/"
  130. title={t(
  131. "A high-level overview of errors, transactions, and deployments filtered by teams you're part of."
  132. )}
  133. />
  134. </Layout.Title>
  135. </Layout.HeaderContent>
  136. <Layout.HeaderActions>
  137. <ButtonBar gap={1}>
  138. <Button
  139. size="sm"
  140. icon={<IconUser size="xs" />}
  141. title={
  142. canJoinTeam ? undefined : t('You do not have permission to join a team.')
  143. }
  144. disabled={!canJoinTeam}
  145. to={`/settings/${organization.slug}/teams/`}
  146. data-test-id="join-team"
  147. >
  148. {t('Join a Team')}
  149. </Button>
  150. <Button
  151. size="sm"
  152. priority="primary"
  153. disabled={!canCreateProjects}
  154. title={
  155. !canCreateProjects
  156. ? t('You do not have permission to create projects')
  157. : undefined
  158. }
  159. to={`/organizations/${organization.slug}/projects/new/`}
  160. icon={<IconAdd size="xs" isCircled />}
  161. data-test-id="create-project"
  162. >
  163. {t('Create Project')}
  164. </Button>
  165. </ButtonBar>
  166. </Layout.HeaderActions>
  167. </Layout.Header>
  168. <Layout.Body>
  169. <Layout.Main fullWidth>
  170. <SearchAndSelectorWrapper>
  171. <TeamFilter
  172. selectedTeams={selectedTeams}
  173. handleChangeFilter={handleChangeFilter}
  174. showIsMemberTeams
  175. showSuggestedOptions={false}
  176. showMyTeamsDescription
  177. />
  178. <StyledSearchBar
  179. defaultQuery=""
  180. placeholder={t('Search for projects by name')}
  181. onChange={debouncedSearchQuery}
  182. query={projectQuery}
  183. />
  184. </SearchAndSelectorWrapper>
  185. <Profiler id="ProjectCardList" onRender={onRenderCallback}>
  186. <ProjectCardList projects={filteredProjects} />
  187. </Profiler>
  188. </Layout.Main>
  189. </Layout.Body>
  190. {showResources && <Resources organization={organization} />}
  191. </Fragment>
  192. );
  193. }
  194. const OrganizationDashboard = (props: Props) => (
  195. <Layout.Page>
  196. <NoProjectMessage organization={props.organization}>
  197. <Dashboard {...props} />
  198. </NoProjectMessage>
  199. </Layout.Page>
  200. );
  201. const SearchAndSelectorWrapper = styled('div')`
  202. display: flex;
  203. gap: ${space(2)};
  204. justify-content: flex-end;
  205. align-items: flex-end;
  206. margin-bottom: ${space(2)};
  207. @media (max-width: ${p => p.theme.breakpoints.small}) {
  208. display: block;
  209. }
  210. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  211. display: flex;
  212. }
  213. `;
  214. const StyledSearchBar = styled(SearchBar)`
  215. flex-grow: 1;
  216. @media (max-width: ${p => p.theme.breakpoints.small}) {
  217. margin-top: ${space(1)};
  218. }
  219. `;
  220. const ProjectCards = styled('div')`
  221. display: grid;
  222. gap: ${space(3)};
  223. grid-template-columns: repeat(auto-fill, minmax(1fr, 400px));
  224. @media (min-width: ${p => p.theme.breakpoints.small}) {
  225. grid-template-columns: repeat(auto-fill, minmax(470px, 1fr));
  226. }
  227. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  228. grid-template-columns: repeat(auto-fill, minmax(450px, 1fr));
  229. }
  230. `;
  231. export {Dashboard};
  232. export default withApi(
  233. withOrganization(withTeamsForUser(withProfiler(OrganizationDashboard)))
  234. );