index.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 * as Layout from 'sentry/components/layouts/thirds';
  12. import ExternalLink from 'sentry/components/links/externalLink';
  13. import LoadingError from 'sentry/components/loadingError';
  14. import LoadingIndicator from 'sentry/components/loadingIndicator';
  15. import NoProjectMessage from 'sentry/components/noProjectMessage';
  16. import PageHeading from 'sentry/components/pageHeading';
  17. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  18. import SearchBar from 'sentry/components/searchBar';
  19. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  20. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  21. import {IconAdd, IconUser} from 'sentry/icons';
  22. import {t, tct} from 'sentry/locale';
  23. import ProjectsStatsStore from 'sentry/stores/projectsStatsStore';
  24. import space from 'sentry/styles/space';
  25. import {Organization, Project, TeamWithProjects} from 'sentry/types';
  26. import {sortProjects} from 'sentry/utils';
  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<{orgId: string}, {}>;
  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. const currentProjects = selectedTeams.length === 0 ? projects : filteredTeamProjects;
  97. const filteredProjects = (currentProjects ?? projects).filter(project =>
  98. project.slug.includes(projectQuery)
  99. );
  100. const favorites = projects.filter(project => project.isBookmarked);
  101. const showEmptyMessage = projects.length === 0 && favorites.length === 0;
  102. const showResources = projects.length === 1 && !projects[0].firstEvent;
  103. function handleSearch(searchQuery: string) {
  104. setProjectQuery(searchQuery);
  105. }
  106. function handleChangeFilter(activeFilters: string[]) {
  107. const {...currentQuery} = location.query;
  108. router.push({
  109. pathname: location.pathname,
  110. query: {
  111. ...currentQuery,
  112. team: activeFilters.length > 0 ? activeFilters : '',
  113. },
  114. });
  115. }
  116. if (showEmptyMessage) {
  117. return (
  118. <NoProjectMessage organization={organization} superuserNeedsToBeProjectMember />
  119. );
  120. }
  121. return (
  122. <Fragment>
  123. <SentryDocumentTitle title={t('Projects Dashboard')} orgSlug={organization.slug} />
  124. {projects.length > 0 && (
  125. <Fragment>
  126. <ProjectsHeader>
  127. <Title>
  128. <PageHeading>
  129. {t('Projects')}
  130. <PageHeadingQuestionTooltip
  131. title={tct(
  132. "A high-level overview of errors, transactions, and deployments filtered by teams you're part of. [link: Read the docs].",
  133. {
  134. link: (
  135. <ExternalLink href="https://docs.sentry.io/product/projects/" />
  136. ),
  137. }
  138. )}
  139. />
  140. </PageHeading>
  141. </Title>
  142. <Layout.HeaderActions>
  143. <ButtonContainer>
  144. <Button
  145. size="sm"
  146. icon={<IconUser size="xs" />}
  147. title={
  148. canJoinTeam
  149. ? undefined
  150. : t('You do not have permission to join a team.')
  151. }
  152. disabled={!canJoinTeam}
  153. to={`/settings/${organization.slug}/teams/`}
  154. data-test-id="join-team"
  155. >
  156. {t('Join a Team')}
  157. </Button>
  158. <Button
  159. size="sm"
  160. priority="primary"
  161. disabled={!canCreateProjects}
  162. title={
  163. !canCreateProjects
  164. ? t('You do not have permission to create projects')
  165. : undefined
  166. }
  167. to={`/organizations/${organization.slug}/projects/new/`}
  168. icon={<IconAdd size="xs" isCircled />}
  169. data-test-id="create-project"
  170. >
  171. {t('Create Project')}
  172. </Button>
  173. </ButtonContainer>
  174. </Layout.HeaderActions>
  175. </ProjectsHeader>
  176. <Body>
  177. <Layout.Main fullWidth>
  178. <SearchAndSelectorWrapper>
  179. <TeamFilter
  180. selectedTeams={selectedTeams}
  181. handleChangeFilter={handleChangeFilter}
  182. showIsMemberTeams
  183. showSuggestedOptions={false}
  184. showMyTeamsDescription
  185. />
  186. <StyledSearchBar
  187. defaultQuery=""
  188. placeholder={t('Search for projects by name')}
  189. onChange={debouncedSearchQuery}
  190. query={projectQuery}
  191. />
  192. </SearchAndSelectorWrapper>
  193. <ProjectCardList projects={filteredProjects} />
  194. </Layout.Main>
  195. </Body>
  196. {showResources && <Resources organization={organization} />}
  197. </Fragment>
  198. )}
  199. </Fragment>
  200. );
  201. }
  202. const OrganizationDashboard = (props: Props) => (
  203. <OrganizationDashboardWrapper>
  204. <Dashboard {...props} />
  205. </OrganizationDashboardWrapper>
  206. );
  207. const ProjectsHeader = styled(Layout.Header)`
  208. border-bottom: none;
  209. align-items: end;
  210. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  211. padding: 26px ${space(4)} 0 ${space(4)};
  212. }
  213. `;
  214. const Title = styled(Layout.HeaderContent)`
  215. margin-bottom: 0;
  216. `;
  217. const ButtonContainer = styled('div')`
  218. display: inline-flex;
  219. gap: ${space(1)};
  220. `;
  221. const SearchAndSelectorWrapper = styled('div')`
  222. display: flex;
  223. gap: ${space(2)};
  224. justify-content: flex-end;
  225. align-items: flex-end;
  226. margin-bottom: ${space(2)};
  227. @media (max-width: ${p => p.theme.breakpoints.small}) {
  228. display: block;
  229. }
  230. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  231. display: flex;
  232. }
  233. `;
  234. const StyledSearchBar = styled(SearchBar)`
  235. flex-grow: 1;
  236. @media (max-width: ${p => p.theme.breakpoints.small}) {
  237. margin-top: ${space(1)};
  238. }
  239. `;
  240. const Body = styled(Layout.Body)`
  241. padding-top: ${space(2)} !important;
  242. background-color: ${p => p.theme.surface100};
  243. `;
  244. const ProjectCards = styled('div')`
  245. display: grid;
  246. grid-template-columns: minmax(100px, 1fr);
  247. gap: ${space(3)};
  248. @media (min-width: ${p => p.theme.breakpoints.small}) {
  249. grid-template-columns: repeat(2, minmax(100px, 1fr));
  250. }
  251. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  252. grid-template-columns: repeat(3, minmax(100px, 1fr));
  253. }
  254. `;
  255. const OrganizationDashboardWrapper = styled('div')`
  256. display: flex;
  257. flex: 1;
  258. flex-direction: column;
  259. `;
  260. export {Dashboard};
  261. export default withApi(
  262. withOrganization(withTeamsForUser(withProfiler(OrganizationDashboard)))
  263. );