index.tsx 8.0 KB

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