noProjectMessage.tsx 3.1 KB

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