noProjectMessage.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. /* TODO: replace with I/O when finished */
  4. import img from 'sentry-images/spot/hair-on-fire.svg';
  5. import Button from 'sentry/components/button';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import PageHeading from 'sentry/components/pageHeading';
  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. type Props = {
  14. organization: Organization;
  15. children?: React.ReactNode;
  16. superuserNeedsToBeProjectMember?: boolean;
  17. };
  18. function NoProjectMessage({
  19. children,
  20. organization,
  21. superuserNeedsToBeProjectMember,
  22. }: Props) {
  23. const {projects, initiallyLoaded: projectsLoaded} = useProjects();
  24. const orgSlug = organization.slug;
  25. const canCreateProject = organization.access.includes('project:write');
  26. const canJoinTeam = organization.access.includes('team:read');
  27. const {isSuperuser} = ConfigStore.get('user');
  28. const orgHasProjects = !!projects?.length;
  29. const hasProjectAccess =
  30. isSuperuser && !superuserNeedsToBeProjectMember
  31. ? !!projects?.some(p => p.hasAccess)
  32. : !!projects?.some(p => p.isMember && p.hasAccess);
  33. if (hasProjectAccess || !projectsLoaded) {
  34. return <Fragment>{children}</Fragment>;
  35. }
  36. // If the organization has some projects, but the user doesn't have access to
  37. // those projects, the primary action is to Join a Team. Otherwise the primary
  38. // action is to create a project.
  39. const joinTeamAction = (
  40. <Button
  41. title={canJoinTeam ? undefined : t('You do not have permission to join a team.')}
  42. disabled={!canJoinTeam}
  43. priority={orgHasProjects ? 'primary' : 'default'}
  44. to={`/settings/${orgSlug}/teams/`}
  45. >
  46. {t('Join a Team')}
  47. </Button>
  48. );
  49. const createProjectAction = (
  50. <Button
  51. title={
  52. canCreateProject
  53. ? undefined
  54. : t('You do not have permission to create a project.')
  55. }
  56. disabled={!canCreateProject}
  57. priority={orgHasProjects ? 'default' : 'primary'}
  58. to={`/organizations/${orgSlug}/projects/new/`}
  59. >
  60. {t('Create project')}
  61. </Button>
  62. );
  63. return (
  64. <Wrapper>
  65. <HeightWrapper>
  66. <img src={img} height={350} alt={t('Nothing to see')} />
  67. <Content>
  68. <StyledPageHeading>{t('Remain Calm')}</StyledPageHeading>
  69. <HelpMessage>{t('You need at least one project to use this view')}</HelpMessage>
  70. <Actions gap={1}>
  71. {!orgHasProjects ? (
  72. createProjectAction
  73. ) : (
  74. <Fragment>
  75. {joinTeamAction}
  76. {createProjectAction}
  77. </Fragment>
  78. )}
  79. </Actions>
  80. </Content>
  81. </HeightWrapper>
  82. </Wrapper>
  83. );
  84. }
  85. const StyledPageHeading = styled(PageHeading)`
  86. font-size: 28px;
  87. margin-bottom: ${space(1.5)};
  88. `;
  89. const HelpMessage = styled('div')`
  90. margin-bottom: ${space(2)};
  91. `;
  92. const Wrapper = styled('div')`
  93. display: flex;
  94. flex: 1;
  95. align-items: center;
  96. justify-content: center;
  97. `;
  98. const HeightWrapper = styled('div')`
  99. display: flex;
  100. height: 350px;
  101. `;
  102. const Content = styled('div')`
  103. display: flex;
  104. flex-direction: column;
  105. justify-content: center;
  106. margin-left: 40px;
  107. `;
  108. const Actions = styled(ButtonBar)`
  109. width: fit-content;
  110. `;
  111. export default NoProjectMessage;