utils.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, ItemsBeforeFilter} 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: ItemsBeforeFilter = teams
  67. .filter(team => !selectedTeams.some(slug => slug === team.slug))
  68. .map((team, index) =>
  69. getDropdownOption({
  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 getDropdownOption({
  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. }): ItemsBeforeFilter[number] {
  116. const isIdpProvisioned = isAddingTeamToMember && team.flags['idp:provisioned'];
  117. const label = isIdpProvisioned ? (
  118. <Tooltip title={getButtonHelpText(isIdpProvisioned)}>
  119. <DropdownTeamBadgeDisabled avatarSize={18} team={team} />
  120. </Tooltip>
  121. ) : (
  122. <DropdownTeamBadge avatarSize={18} team={team} />
  123. );
  124. return {
  125. index,
  126. value: team.slug,
  127. searchKey: team.slug,
  128. label,
  129. disabled: disabled || isIdpProvisioned,
  130. };
  131. }
  132. function renderDropdownHeader({
  133. organization,
  134. project,
  135. onCreateTeam,
  136. }: {
  137. organization: Organization;
  138. onCreateTeam?: (team) => void;
  139. project?: Project;
  140. }) {
  141. const canCreateTeam = hasEveryAccess(['org:write'], {organization, project});
  142. return (
  143. <StyledTeamsLabel>
  144. <span>{t('Teams')}</span>
  145. <Tooltip
  146. disabled={canCreateTeam}
  147. title={t('You must be a Org Owner/Manager to create teams')}
  148. position="top"
  149. >
  150. <StyledCreateTeamLink
  151. to="#create-team"
  152. disabled={!canCreateTeam}
  153. onClick={(e: React.MouseEvent) => {
  154. e.stopPropagation();
  155. e.preventDefault();
  156. openCreateTeamModal({
  157. organization,
  158. project,
  159. onClose: onCreateTeam,
  160. });
  161. }}
  162. >
  163. {t('Create Team')}
  164. </StyledCreateTeamLink>
  165. </Tooltip>
  166. </StyledTeamsLabel>
  167. );
  168. }
  169. const DropdownTeamBadge = styled(TeamBadge)`
  170. font-weight: normal;
  171. font-size: ${p => p.theme.fontSizeMedium};
  172. text-transform: none;
  173. `;
  174. const DropdownTeamBadgeDisabled = styled(TeamBadge)`
  175. font-weight: normal;
  176. font-size: ${p => p.theme.fontSizeMedium};
  177. text-transform: none;
  178. filter: grayscale(1);
  179. `;
  180. const StyledTeamsLabel = styled('div')`
  181. display: flex;
  182. flex-direction: row;
  183. justify-content: space-between;
  184. font-size: 0.875em;
  185. padding: ${space(0.5)} 0px;
  186. text-transform: uppercase;
  187. `;
  188. const StyledCreateTeamLink = styled(Link)`
  189. float: right;
  190. text-transform: none;
  191. ${p =>
  192. p.disabled &&
  193. css`
  194. cursor: not-allowed;
  195. color: ${p.theme.gray300};
  196. opacity: 0.6;
  197. `};
  198. `;