teamSelectForProject.tsx 4.6 KB

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