utils.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import {css} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import debounce from 'lodash/debounce';
  4. import {openCreateTeamModal} from 'sentry/actionCreators/modal';
  5. import {hasEveryAccess} from 'sentry/components/acl/access';
  6. import DropdownAutoComplete from 'sentry/components/dropdownAutoComplete';
  7. import {Item} from 'sentry/components/dropdownAutoComplete/types';
  8. import DropdownButton from 'sentry/components/dropdownButton';
  9. import {TeamBadge} from 'sentry/components/idBadge/teamBadge';
  10. import Link from 'sentry/components/links/link';
  11. import {Tooltip} from 'sentry/components/tooltip';
  12. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {Organization, Project, Team} from 'sentry/types';
  16. import {getButtonHelpText} from 'sentry/views/settings/organizationTeams/utils';
  17. export type TeamSelectProps = {
  18. /**
  19. * Should button be disabled
  20. */
  21. disabled: boolean;
  22. /**
  23. * callback when teams are added
  24. */
  25. onAddTeam: (teamSlug: string) => void;
  26. /**
  27. * Callback when teams are removed
  28. */
  29. onRemoveTeam: (teamSlug: string) => void;
  30. organization: Organization;
  31. /**
  32. * Used to determine whether we should show a loading state while waiting for teams
  33. */
  34. loadingTeams?: boolean;
  35. /**
  36. * Callback when teams are created
  37. */
  38. onCreateTeam?: (team: Team) => void;
  39. };
  40. export function DropdownAddTeam({
  41. disabled,
  42. isLoadingTeams,
  43. isAddingTeamToMember = false,
  44. isAddingTeamToProject = false,
  45. onSearch,
  46. onSelect,
  47. onCreateTeam,
  48. organization,
  49. selectedTeams,
  50. teams,
  51. project,
  52. }: {
  53. disabled: boolean;
  54. isLoadingTeams: boolean;
  55. onSearch: (teamSlug: string) => void;
  56. onSelect: (teamSlug: string) => void;
  57. organization: Organization;
  58. selectedTeams: string[];
  59. teams: Team[];
  60. canCreateTeam?: boolean;
  61. isAddingTeamToMember?: boolean;
  62. isAddingTeamToProject?: boolean;
  63. onCreateTeam?: (team: Team) => void;
  64. project?: Project;
  65. }) {
  66. const dropdownItems = teams
  67. .filter(team => !selectedTeams.some(slug => slug === team.slug))
  68. .map((team, index) =>
  69. renderDropdownOption({
  70. isAddingTeamToMember,
  71. isAddingTeamToProject,
  72. organization,
  73. team,
  74. index,
  75. disabled,
  76. })
  77. );
  78. const onDropdownChange = debounce<(e: React.ChangeEvent<HTMLInputElement>) => void>(
  79. e => onSearch(e.target.value),
  80. DEFAULT_DEBOUNCE_DURATION
  81. );
  82. return (
  83. <DropdownAutoComplete
  84. items={dropdownItems}
  85. busyItemsStillVisible={isLoadingTeams}
  86. onChange={onDropdownChange}
  87. onSelect={(option: Item) => onSelect(option.value)}
  88. emptyMessage={t('No teams')}
  89. menuHeader={renderDropdownHeader({
  90. organization,
  91. project,
  92. onCreateTeam,
  93. })}
  94. disabled={disabled}
  95. alignMenu="right"
  96. >
  97. {({isOpen}) => (
  98. <DropdownButton isOpen={isOpen} size="xs" disabled={disabled}>
  99. {t('Add Team')}
  100. </DropdownButton>
  101. )}
  102. </DropdownAutoComplete>
  103. );
  104. }
  105. function renderDropdownOption({
  106. disabled,
  107. index,
  108. isAddingTeamToMember,
  109. organization,
  110. team,
  111. }: {
  112. disabled: boolean;
  113. index: number;
  114. isAddingTeamToMember: boolean;
  115. isAddingTeamToProject: boolean;
  116. organization: Organization;
  117. team: Team;
  118. }) {
  119. const hasOrgAdmin = organization.access.includes('org:admin');
  120. const isIdpProvisioned = isAddingTeamToMember && team.flags['idp:provisioned'];
  121. const isPermissionGroup = isAddingTeamToMember && !!team.orgRole && !hasOrgAdmin;
  122. const buttonHelpText = getButtonHelpText(isIdpProvisioned, isPermissionGroup);
  123. return {
  124. index,
  125. value: team.slug,
  126. searchKey: team.slug,
  127. label: () => {
  128. if (isIdpProvisioned || isPermissionGroup) {
  129. return (
  130. <Tooltip title={buttonHelpText}>
  131. <DropdownTeamBadgeDisabled avatarSize={18} team={team} />
  132. </Tooltip>
  133. );
  134. }
  135. return <DropdownTeamBadge avatarSize={18} team={team} />;
  136. },
  137. disabled: disabled || isIdpProvisioned || isPermissionGroup,
  138. };
  139. }
  140. function renderDropdownHeader({
  141. organization,
  142. project,
  143. onCreateTeam,
  144. }: {
  145. organization: Organization;
  146. onCreateTeam?: (team) => void;
  147. project?: Project;
  148. }) {
  149. const canCreateTeam = hasEveryAccess(['org:write'], {organization, project});
  150. return (
  151. <StyledTeamsLabel>
  152. <span>{t('Teams')}</span>
  153. <Tooltip
  154. disabled={canCreateTeam}
  155. title={t('You must be a Org Owner/Manager to create teams')}
  156. position="top"
  157. >
  158. <StyledCreateTeamLink
  159. to="#create-team"
  160. disabled={!canCreateTeam}
  161. onClick={(e: React.MouseEvent) => {
  162. e.stopPropagation();
  163. e.preventDefault();
  164. openCreateTeamModal({
  165. organization,
  166. project,
  167. onClose: onCreateTeam,
  168. });
  169. }}
  170. >
  171. {t('Create Team')}
  172. </StyledCreateTeamLink>
  173. </Tooltip>
  174. </StyledTeamsLabel>
  175. );
  176. }
  177. const DropdownTeamBadge = styled(TeamBadge)`
  178. font-weight: normal;
  179. font-size: ${p => p.theme.fontSizeMedium};
  180. text-transform: none;
  181. `;
  182. const DropdownTeamBadgeDisabled = styled(TeamBadge)`
  183. font-weight: normal;
  184. font-size: ${p => p.theme.fontSizeMedium};
  185. text-transform: none;
  186. filter: grayscale(1);
  187. `;
  188. const StyledTeamsLabel = styled('div')`
  189. display: flex;
  190. flex-direction: row;
  191. justify-content: space-between;
  192. font-size: 0.875em;
  193. padding: ${space(0.5)} 0px;
  194. text-transform: uppercase;
  195. `;
  196. const StyledCreateTeamLink = styled(Link)`
  197. float: right;
  198. text-transform: none;
  199. ${p =>
  200. p.disabled &&
  201. css`
  202. cursor: not-allowed;
  203. color: ${p.theme.gray300};
  204. opacity: 0.6;
  205. `};
  206. `;