utils.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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
  99. aria-label={t('Add Team')}
  100. isOpen={isOpen}
  101. size="xs"
  102. disabled={disabled}
  103. >
  104. {t('Add Team')}
  105. </DropdownButton>
  106. )}
  107. </DropdownAutoComplete>
  108. );
  109. }
  110. function renderDropdownOption({
  111. disabled,
  112. index,
  113. isAddingTeamToMember,
  114. organization,
  115. team,
  116. }: {
  117. disabled: boolean;
  118. index: number;
  119. isAddingTeamToMember: boolean;
  120. isAddingTeamToProject: boolean;
  121. organization: Organization;
  122. team: Team;
  123. }) {
  124. const hasOrgAdmin = organization.access.includes('org:admin');
  125. const isIdpProvisioned = isAddingTeamToMember && team.flags['idp:provisioned'];
  126. const isPermissionGroup = isAddingTeamToMember && !!team.orgRole && !hasOrgAdmin;
  127. const buttonHelpText = getButtonHelpText(isIdpProvisioned, isPermissionGroup);
  128. return {
  129. index,
  130. value: team.slug,
  131. searchKey: team.slug,
  132. label: () => {
  133. if (isIdpProvisioned || isPermissionGroup) {
  134. return (
  135. <Tooltip title={buttonHelpText}>
  136. <DropdownTeamBadgeDisabled avatarSize={18} team={team} />
  137. </Tooltip>
  138. );
  139. }
  140. return <DropdownTeamBadge avatarSize={18} team={team} />;
  141. },
  142. disabled: disabled || isIdpProvisioned || isPermissionGroup,
  143. };
  144. }
  145. function renderDropdownHeader({
  146. organization,
  147. project,
  148. onCreateTeam,
  149. }: {
  150. organization: Organization;
  151. onCreateTeam?: (team) => void;
  152. project?: Project;
  153. }) {
  154. const canCreateTeam = hasEveryAccess(['org:write'], {organization, project});
  155. return (
  156. <StyledTeamsLabel>
  157. <span>{t('Teams')}</span>
  158. <Tooltip
  159. disabled={canCreateTeam}
  160. title={t('You must be a Org Owner/Manager to create teams')}
  161. position="top"
  162. >
  163. <StyledCreateTeamLink
  164. to="#create-team"
  165. disabled={!canCreateTeam}
  166. onClick={(e: React.MouseEvent) => {
  167. e.stopPropagation();
  168. e.preventDefault();
  169. openCreateTeamModal({
  170. organization,
  171. project,
  172. onClose: onCreateTeam,
  173. });
  174. }}
  175. >
  176. {t('Create Team')}
  177. </StyledCreateTeamLink>
  178. </Tooltip>
  179. </StyledTeamsLabel>
  180. );
  181. }
  182. const DropdownTeamBadge = styled(TeamBadge)`
  183. font-weight: normal;
  184. font-size: ${p => p.theme.fontSizeMedium};
  185. text-transform: none;
  186. `;
  187. const DropdownTeamBadgeDisabled = styled(TeamBadge)`
  188. font-weight: normal;
  189. font-size: ${p => p.theme.fontSizeMedium};
  190. text-transform: none;
  191. filter: grayscale(1);
  192. `;
  193. const StyledTeamsLabel = styled('div')`
  194. display: flex;
  195. flex-direction: row;
  196. justify-content: space-between;
  197. font-size: 0.875em;
  198. padding: ${space(0.5)} 0px;
  199. text-transform: uppercase;
  200. `;
  201. const StyledCreateTeamLink = styled(Link)`
  202. float: right;
  203. text-transform: none;
  204. ${p =>
  205. p.disabled &&
  206. css`
  207. cursor: not-allowed;
  208. color: ${p.theme.gray300};
  209. opacity: 0.6;
  210. `};
  211. `;