noProjectMessage.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import ButtonBar from 'sentry/components/buttonBar';
  5. import NoProjectEmptyState from 'sentry/components/illustrations/NoProjectEmptyState';
  6. import * as Layout from 'sentry/components/layouts/thirds';
  7. import {useProjectCreationAccess} from 'sentry/components/projects/useProjectCreationAccess';
  8. import {t} from 'sentry/locale';
  9. import ConfigStore from 'sentry/stores/configStore';
  10. import {space} from 'sentry/styles/space';
  11. import {Organization} from 'sentry/types';
  12. import useProjects from 'sentry/utils/useProjects';
  13. import {useTeams} from 'sentry/utils/useTeams';
  14. type Props = {
  15. organization: Organization;
  16. children?: React.ReactNode;
  17. superuserNeedsToBeProjectMember?: boolean;
  18. };
  19. function NoProjectMessage({
  20. children,
  21. organization,
  22. superuserNeedsToBeProjectMember,
  23. }: Props) {
  24. const {projects, initiallyLoaded: projectsLoaded} = useProjects();
  25. const {teams, initiallyLoaded: teamsLoaded} = useTeams();
  26. const orgSlug = organization.slug;
  27. const {canCreateProject} = useProjectCreationAccess({organization, teams});
  28. const canJoinTeam = organization.access.includes('team:read');
  29. const {isSuperuser} = ConfigStore.get('user');
  30. const orgHasProjects = !!projects?.length;
  31. const hasProjectAccess =
  32. isSuperuser && !superuserNeedsToBeProjectMember
  33. ? !!projects?.some(p => p.hasAccess)
  34. : !!projects?.some(p => p.isMember && p.hasAccess);
  35. if (hasProjectAccess || !projectsLoaded || !teamsLoaded) {
  36. return <Fragment>{children}</Fragment>;
  37. }
  38. // If the organization has some projects, but the user doesn't have access to
  39. // those projects, the primary action is to Join a Team. Otherwise the primary
  40. // action is to create a project.
  41. const joinTeamAction = (
  42. <Button
  43. title={canJoinTeam ? undefined : t('You do not have permission to join a team.')}
  44. disabled={!canJoinTeam}
  45. priority={orgHasProjects ? 'primary' : 'default'}
  46. to={`/settings/${orgSlug}/teams/`}
  47. >
  48. {t('Join a Team')}
  49. </Button>
  50. );
  51. const createProjectAction = (
  52. <Button
  53. title={
  54. canCreateProject
  55. ? undefined
  56. : t('You do not have permission to create a project.')
  57. }
  58. disabled={!canCreateProject}
  59. priority={orgHasProjects ? 'default' : 'primary'}
  60. to={`/organizations/${orgSlug}/projects/new/`}
  61. >
  62. {t('Create project')}
  63. </Button>
  64. );
  65. return (
  66. <Wrapper>
  67. <NoProjectEmptyState />
  68. <Content>
  69. <Layout.Title>{t('Remain Calm')}</Layout.Title>
  70. <HelpMessage>{t('You need at least one project to use this view')}</HelpMessage>
  71. <Actions gap={1}>
  72. {!orgHasProjects ? (
  73. createProjectAction
  74. ) : (
  75. <Fragment>
  76. {joinTeamAction}
  77. {createProjectAction}
  78. </Fragment>
  79. )}
  80. </Actions>
  81. </Content>
  82. </Wrapper>
  83. );
  84. }
  85. export default NoProjectMessage;
  86. const HelpMessage = styled('div')`
  87. margin-bottom: ${space(2)};
  88. `;
  89. const Wrapper = styled('div')`
  90. display: flex;
  91. flex: 1;
  92. align-items: center;
  93. justify-content: center;
  94. `;
  95. const Content = styled('div')`
  96. display: flex;
  97. flex-direction: column;
  98. justify-content: center;
  99. margin-left: 40px;
  100. `;
  101. const Actions = styled(ButtonBar)`
  102. width: fit-content;
  103. `;