utils.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 type {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 type {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. team,
  73. index,
  74. disabled,
  75. })
  76. );
  77. const onDropdownChange = debounce<(e: React.ChangeEvent<HTMLInputElement>) => void>(
  78. e => onSearch(e.target.value),
  79. DEFAULT_DEBOUNCE_DURATION
  80. );
  81. return (
  82. <DropdownAutoComplete
  83. items={dropdownItems}
  84. busyItemsStillVisible={isLoadingTeams}
  85. onChange={onDropdownChange}
  86. onSelect={(option: Item) => onSelect(option.value)}
  87. emptyMessage={t('No teams')}
  88. menuHeader={renderDropdownHeader({
  89. organization,
  90. project,
  91. onCreateTeam,
  92. })}
  93. disabled={disabled}
  94. alignMenu="right"
  95. >
  96. {({isOpen}) => (
  97. <DropdownButton isOpen={isOpen} size="xs" disabled={disabled}>
  98. {t('Add Team')}
  99. </DropdownButton>
  100. )}
  101. </DropdownAutoComplete>
  102. );
  103. }
  104. function renderDropdownOption({
  105. disabled,
  106. index,
  107. isAddingTeamToMember,
  108. team,
  109. }: {
  110. disabled: boolean;
  111. index: number;
  112. isAddingTeamToMember: boolean;
  113. isAddingTeamToProject: boolean;
  114. team: Team;
  115. }) {
  116. const isIdpProvisioned = isAddingTeamToMember && team.flags['idp:provisioned'];
  117. const buttonHelpText = getButtonHelpText(isIdpProvisioned);
  118. return {
  119. index,
  120. value: team.slug,
  121. searchKey: team.slug,
  122. label: () => {
  123. if (isIdpProvisioned) {
  124. return (
  125. <Tooltip title={buttonHelpText}>
  126. <DropdownTeamBadgeDisabled avatarSize={18} team={team} />
  127. </Tooltip>
  128. );
  129. }
  130. return <DropdownTeamBadge avatarSize={18} team={team} />;
  131. },
  132. disabled: disabled || isIdpProvisioned,
  133. };
  134. }
  135. function renderDropdownHeader({
  136. organization,
  137. project,
  138. onCreateTeam,
  139. }: {
  140. organization: Organization;
  141. onCreateTeam?: (team) => void;
  142. project?: Project;
  143. }) {
  144. const canCreateTeam = hasEveryAccess(['org:write'], {organization, project});
  145. return (
  146. <StyledTeamsLabel>
  147. <span>{t('Teams')}</span>
  148. <Tooltip
  149. disabled={canCreateTeam}
  150. title={t('You must be a Org Owner/Manager to create teams')}
  151. position="top"
  152. >
  153. <StyledCreateTeamLink
  154. to="#create-team"
  155. disabled={!canCreateTeam}
  156. onClick={(e: React.MouseEvent) => {
  157. e.stopPropagation();
  158. e.preventDefault();
  159. openCreateTeamModal({
  160. organization,
  161. project,
  162. onClose: onCreateTeam,
  163. });
  164. }}
  165. >
  166. {t('Create Team')}
  167. </StyledCreateTeamLink>
  168. </Tooltip>
  169. </StyledTeamsLabel>
  170. );
  171. }
  172. const DropdownTeamBadge = styled(TeamBadge)`
  173. font-weight: normal;
  174. font-size: ${p => p.theme.fontSizeMedium};
  175. text-transform: none;
  176. `;
  177. const DropdownTeamBadgeDisabled = styled(TeamBadge)`
  178. font-weight: normal;
  179. font-size: ${p => p.theme.fontSizeMedium};
  180. text-transform: none;
  181. filter: grayscale(1);
  182. `;
  183. const StyledTeamsLabel = styled('div')`
  184. display: flex;
  185. flex-direction: row;
  186. justify-content: space-between;
  187. font-size: 0.875em;
  188. padding: ${space(0.5)} 0px;
  189. text-transform: uppercase;
  190. `;
  191. const StyledCreateTeamLink = styled(Link)`
  192. float: right;
  193. text-transform: none;
  194. ${p =>
  195. p.disabled &&
  196. css`
  197. cursor: not-allowed;
  198. color: ${p.theme.gray300};
  199. opacity: 0.6;
  200. `};
  201. `;