teamSelectForProject.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import Confirm from 'sentry/components/confirm';
  5. import EmptyMessage from 'sentry/components/emptyMessage';
  6. import {TeamBadge} from 'sentry/components/idBadge/teamBadge';
  7. import Link from 'sentry/components/links/link';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import Panel from 'sentry/components/panels/panel';
  10. import PanelBody from 'sentry/components/panels/panelBody';
  11. import PanelHeader from 'sentry/components/panels/panelHeader';
  12. import PanelItem from 'sentry/components/panels/panelItem';
  13. import {IconSubtract} from 'sentry/icons';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import {Organization, Project, Team} from 'sentry/types';
  17. import {useTeams} from 'sentry/utils/useTeams';
  18. import {DropdownAddTeam, TeamSelectProps} from './utils';
  19. type Props = TeamSelectProps & {
  20. canCreateTeam: boolean;
  21. project: Project;
  22. /**
  23. * Used when showing Teams for a Project
  24. */
  25. selectedTeams: Team[];
  26. };
  27. function TeamSelect({
  28. disabled,
  29. canCreateTeam,
  30. project,
  31. selectedTeams,
  32. organization,
  33. onAddTeam,
  34. onRemoveTeam,
  35. onCreateTeam,
  36. }: Props) {
  37. const renderBody = () => {
  38. const numTeams = selectedTeams.length;
  39. if (numTeams === 0) {
  40. return <EmptyMessage>{t('No Teams assigned')}</EmptyMessage>;
  41. }
  42. // If the user is not a team-admin in any parent teams of this project, they will
  43. // not be able to edit the configuration. Warn the user if this is their last team
  44. // where they have team-admin role.
  45. const isUserLastTeamWrite =
  46. selectedTeams.reduce(
  47. (count, team) => (team.access.includes('team:write') ? count + 1 : count),
  48. 0
  49. ) === 1;
  50. const isOnlyTeam = numTeams === 1;
  51. const confirmMessage = isUserLastTeamWrite
  52. ? t(
  53. "This is the last team that grants Team Admin access to you for this project. After removing this team, you will not be able to edit this project's configuration."
  54. )
  55. : isOnlyTeam
  56. ? t(
  57. 'This is the last team with access to this project. After removing this team, only organization owners and managers will be able to access the project pages.'
  58. )
  59. : t(
  60. 'Removing this team from the project means that members of the team can no longer access this project. Do you want to continue?'
  61. );
  62. return (
  63. <Fragment>
  64. {selectedTeams.map(team => (
  65. <TeamRow
  66. key={team.slug}
  67. disabled={disabled || !team.access.includes('team:write')}
  68. confirmMessage={confirmMessage}
  69. organization={organization}
  70. team={team}
  71. onRemoveTeam={slug => onRemoveTeam(slug)}
  72. />
  73. ))}
  74. </Fragment>
  75. );
  76. };
  77. const {teams, onSearch, fetching: isLoadingTeams} = useTeams();
  78. return (
  79. <Panel>
  80. <PanelHeader hasButtons>
  81. {t('Team')}
  82. <DropdownAddTeam
  83. disabled={disabled}
  84. isLoadingTeams={isLoadingTeams}
  85. isAddingTeamToProject
  86. canCreateTeam={canCreateTeam}
  87. onSearch={onSearch}
  88. onSelect={onAddTeam}
  89. onCreateTeam={onCreateTeam}
  90. organization={organization}
  91. selectedTeams={selectedTeams.map(tm => tm.slug)}
  92. teams={teams}
  93. project={project}
  94. />
  95. </PanelHeader>
  96. <PanelBody>{isLoadingTeams ? <LoadingIndicator /> : renderBody()}</PanelBody>
  97. </Panel>
  98. );
  99. }
  100. function TeamRow({
  101. organization,
  102. team,
  103. onRemoveTeam,
  104. disabled,
  105. confirmMessage,
  106. }: {
  107. confirmMessage: string | null;
  108. disabled: boolean;
  109. onRemoveTeam: Props['onRemoveTeam'];
  110. organization: Organization;
  111. team: Team;
  112. }) {
  113. return (
  114. <TeamPanelItem data-test-id="team-row-for-project">
  115. <TeamPanelItemLeft>
  116. <Link to={`/settings/${organization.slug}/teams/${team.slug}/`}>
  117. <TeamBadge team={team} />
  118. </Link>
  119. </TeamPanelItemLeft>
  120. <Confirm
  121. message={confirmMessage}
  122. bypass={!confirmMessage}
  123. onConfirm={() => onRemoveTeam(team.slug)}
  124. disabled={disabled}
  125. confirmText="Remove Team"
  126. >
  127. <Button size="xs" icon={<IconSubtract isCircled />} disabled={disabled}>
  128. {t('Remove')}
  129. </Button>
  130. </Confirm>
  131. </TeamPanelItem>
  132. );
  133. }
  134. const TeamPanelItem = styled(PanelItem)`
  135. padding: ${space(2)};
  136. align-items: center;
  137. justify-content: space-between;
  138. `;
  139. const TeamPanelItemLeft = styled('div')`
  140. flex-grow: 4;
  141. `;
  142. export default TeamSelect;