noProjectMessage.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. 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} = useProjectCreationAccess(organization);
  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. <NoProjectEmptyState />
  66. <Content>
  67. <Layout.Title>{t('Remain Calm')}</Layout.Title>
  68. <HelpMessage>{t('You need at least one project to use this view')}</HelpMessage>
  69. <Actions gap={1}>
  70. {!orgHasProjects ? (
  71. createProjectAction
  72. ) : (
  73. <Fragment>
  74. {joinTeamAction}
  75. {createProjectAction}
  76. </Fragment>
  77. )}
  78. </Actions>
  79. </Content>
  80. </Wrapper>
  81. );
  82. }
  83. export default NoProjectMessage;
  84. const HelpMessage = styled('div')`
  85. margin-bottom: ${space(2)};
  86. `;
  87. const Wrapper = styled('div')`
  88. display: flex;
  89. flex: 1;
  90. align-items: center;
  91. justify-content: center;
  92. `;
  93. const Content = styled('div')`
  94. display: flex;
  95. flex-direction: column;
  96. justify-content: center;
  97. margin-left: 40px;
  98. `;
  99. const Actions = styled(ButtonBar)`
  100. width: fit-content;
  101. `;