utils.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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, Team} from 'sentry/types/organization';
  16. import type {Project} from 'sentry/types/project';
  17. import {getButtonHelpText} from 'sentry/views/settings/organizationTeams/utils';
  18. export type TeamSelectProps = {
  19. /**
  20. * Should button be disabled
  21. */
  22. disabled: boolean;
  23. /**
  24. * callback when teams are added
  25. */
  26. onAddTeam: (teamSlug: string) => void;
  27. /**
  28. * Callback when teams are removed
  29. */
  30. onRemoveTeam: (teamSlug: string) => void;
  31. organization: Organization;
  32. /**
  33. * Used to determine whether we should show a loading state while waiting for teams
  34. */
  35. loadingTeams?: boolean;
  36. /**
  37. * Callback when teams are created
  38. */
  39. onCreateTeam?: (team: Team) => void;
  40. };
  41. export function DropdownAddTeam({
  42. disabled,
  43. isLoadingTeams,
  44. isAddingTeamToMember = false,
  45. isAddingTeamToProject = false,
  46. onSearch,
  47. onSelect,
  48. onCreateTeam,
  49. organization,
  50. selectedTeams,
  51. teams,
  52. project,
  53. }: {
  54. disabled: boolean;
  55. isLoadingTeams: boolean;
  56. onSearch: (teamSlug: string) => void;
  57. onSelect: (teamSlug: string) => void;
  58. organization: Organization;
  59. selectedTeams: string[];
  60. teams: Team[];
  61. canCreateTeam?: boolean;
  62. isAddingTeamToMember?: boolean;
  63. isAddingTeamToProject?: boolean;
  64. onCreateTeam?: (team: Team) => void;
  65. project?: Project;
  66. }) {
  67. const dropdownItems: ItemsBeforeFilter = teams
  68. .filter(team => !selectedTeams.some(slug => slug === team.slug))
  69. .map((team, index) =>
  70. getDropdownOption({
  71. isAddingTeamToMember,
  72. isAddingTeamToProject,
  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 getDropdownOption({
  106. disabled,
  107. index,
  108. isAddingTeamToMember,
  109. team,
  110. }: {
  111. disabled: boolean;
  112. index: number;
  113. isAddingTeamToMember: boolean;
  114. isAddingTeamToProject: boolean;
  115. team: Team;
  116. }): ItemsBeforeFilter[number] {
  117. const isIdpProvisioned = isAddingTeamToMember && team.flags['idp:provisioned'];
  118. const label = isIdpProvisioned ? (
  119. <Tooltip title={getButtonHelpText(isIdpProvisioned)}>
  120. <DropdownTeamBadgeDisabled avatarSize={18} team={team} />
  121. </Tooltip>
  122. ) : (
  123. <DropdownTeamBadge avatarSize={18} team={team} />
  124. );
  125. return {
  126. index,
  127. value: team.slug,
  128. searchKey: team.slug,
  129. label,
  130. disabled: disabled || isIdpProvisioned,
  131. };
  132. }
  133. function renderDropdownHeader({
  134. organization,
  135. project,
  136. onCreateTeam,
  137. }: {
  138. organization: Organization;
  139. onCreateTeam?: (team) => void;
  140. project?: Project;
  141. }) {
  142. const canCreateTeam = hasEveryAccess(['org:write'], {organization, project});
  143. return (
  144. <StyledTeamsLabel>
  145. <span>{t('Teams')}</span>
  146. <Tooltip
  147. disabled={canCreateTeam}
  148. title={t('You must be a Org Owner/Manager to create teams')}
  149. position="top"
  150. >
  151. <StyledCreateTeamLink
  152. to="#create-team"
  153. disabled={!canCreateTeam}
  154. onClick={(e: React.MouseEvent) => {
  155. e.stopPropagation();
  156. e.preventDefault();
  157. openCreateTeamModal({
  158. organization,
  159. project,
  160. onClose: onCreateTeam,
  161. });
  162. }}
  163. >
  164. {t('Create Team')}
  165. </StyledCreateTeamLink>
  166. </Tooltip>
  167. </StyledTeamsLabel>
  168. );
  169. }
  170. const DropdownTeamBadge = styled(TeamBadge)`
  171. font-weight: ${p => p.theme.fontWeightNormal};
  172. font-size: ${p => p.theme.fontSizeMedium};
  173. text-transform: none;
  174. `;
  175. const DropdownTeamBadgeDisabled = styled(TeamBadge)`
  176. font-weight: ${p => p.theme.fontWeightNormal};
  177. font-size: ${p => p.theme.fontSizeMedium};
  178. text-transform: none;
  179. filter: grayscale(1);
  180. `;
  181. const StyledTeamsLabel = styled('div')`
  182. display: flex;
  183. flex-direction: row;
  184. justify-content: space-between;
  185. font-size: 0.875em;
  186. padding: ${space(0.5)} 0px;
  187. text-transform: uppercase;
  188. `;
  189. const StyledCreateTeamLink = styled(Link)`
  190. float: right;
  191. text-transform: none;
  192. ${p =>
  193. p.disabled &&
  194. css`
  195. cursor: not-allowed;
  196. color: ${p.theme.gray300};
  197. opacity: 0.6;
  198. `};
  199. `;