selectOwners.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import {Component, createRef} from 'react';
  2. import {findDOMNode} from 'react-dom';
  3. import type {MultiValueProps} from 'react-select';
  4. import styled from '@emotion/styled';
  5. import debounce from 'lodash/debounce';
  6. import isEqual from 'lodash/isEqual';
  7. import {addTeamToProject} from 'sentry/actionCreators/projects';
  8. import type {Client} from 'sentry/api';
  9. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  10. import {Button} from 'sentry/components/button';
  11. import SelectControl from 'sentry/components/forms/controls/selectControl';
  12. import IdBadge from 'sentry/components/idBadge';
  13. import {Tooltip} from 'sentry/components/tooltip';
  14. import {IconAdd} from 'sentry/icons';
  15. import {t} from 'sentry/locale';
  16. import MemberListStore from 'sentry/stores/memberListStore';
  17. import ProjectsStore from 'sentry/stores/projectsStore';
  18. import TeamStore from 'sentry/stores/teamStore';
  19. import {space} from 'sentry/styles/space';
  20. import type {Actor} from 'sentry/types/core';
  21. import type {Member, Organization, Team} from 'sentry/types/organization';
  22. import type {Project} from 'sentry/types/project';
  23. import type {User} from 'sentry/types/user';
  24. import {buildTeamId, buildUserId} from 'sentry/utils';
  25. import withApi from 'sentry/utils/withApi';
  26. import withProjects from 'sentry/utils/withProjects';
  27. export type Owner = {
  28. actor: Actor;
  29. label: React.ReactNode;
  30. searchKey: string;
  31. value: string;
  32. disabled?: boolean;
  33. };
  34. function ValueComponent({data, removeProps}: MultiValueProps<Owner>) {
  35. return (
  36. <ValueWrapper onClick={removeProps.onClick}>
  37. <ActorAvatar actor={data.actor} size={28} />
  38. </ValueWrapper>
  39. );
  40. }
  41. const getSearchKeyForUser = (user: User) =>
  42. `${user.email?.toLowerCase()} ${user.name?.toLowerCase()}`;
  43. type Props = {
  44. api: Client;
  45. disabled: boolean;
  46. onChange: (owners: Owner[]) => void;
  47. organization: Organization;
  48. project: Project;
  49. projects: Project[];
  50. value: any;
  51. onInputChange?: (text: string) => void;
  52. };
  53. type State = {
  54. inputValue: string;
  55. loading: boolean;
  56. };
  57. class SelectOwners extends Component<Props, State> {
  58. state: State = {
  59. loading: false,
  60. inputValue: '',
  61. };
  62. componentDidUpdate(prevProps: Props) {
  63. // Once a team has been added to the project the menu can be closed.
  64. if (!isEqual(this.props.projects, prevProps.projects)) {
  65. this.closeSelectMenu();
  66. }
  67. }
  68. private selectRef = createRef<any>();
  69. renderUserBadge = (user: User) => (
  70. <IdBadge avatarSize={24} user={user} hideEmail disableLink />
  71. );
  72. createMentionableUser = (user: User): Owner => ({
  73. value: buildUserId(user.id),
  74. label: this.renderUserBadge(user),
  75. searchKey: getSearchKeyForUser(user),
  76. actor: {
  77. type: 'user' as const,
  78. id: user.id,
  79. name: user.name,
  80. },
  81. });
  82. createUnmentionableUser = ({user}): Owner => ({
  83. ...this.createMentionableUser(user),
  84. disabled: true,
  85. label: (
  86. <DisabledLabel>
  87. <Tooltip
  88. position="left"
  89. title={t('%s is not a member of project', user.name || user.email)}
  90. >
  91. {this.renderUserBadge(user)}
  92. </Tooltip>
  93. </DisabledLabel>
  94. ),
  95. });
  96. createMentionableTeam = (team: Team): Owner => ({
  97. value: buildTeamId(team.id),
  98. label: <IdBadge team={team} />,
  99. searchKey: `#${team.slug}`,
  100. actor: {
  101. type: 'team' as const,
  102. id: team.id,
  103. name: team.slug,
  104. },
  105. });
  106. createUnmentionableTeam = (team: Team): Owner => {
  107. const {organization} = this.props;
  108. const canAddTeam = organization.access.includes('project:write');
  109. return {
  110. ...this.createMentionableTeam(team),
  111. disabled: true,
  112. label: (
  113. <Container>
  114. <DisabledLabel>
  115. <Tooltip
  116. position="left"
  117. title={t('%s is not a member of project', `#${team.slug}`)}
  118. >
  119. <IdBadge team={team} />
  120. </Tooltip>
  121. </DisabledLabel>
  122. <Tooltip
  123. title={
  124. canAddTeam
  125. ? t('Add %s to project', `#${team.slug}`)
  126. : t('You do not have permission to add team to project.')
  127. }
  128. >
  129. <AddToProjectButton
  130. size="zero"
  131. borderless
  132. disabled={!canAddTeam}
  133. onClick={this.handleAddTeamToProject.bind(this, team)}
  134. icon={<IconAdd isCircled />}
  135. aria-label={t('Add %s to project', `#${team.slug}`)}
  136. />
  137. </Tooltip>
  138. </Container>
  139. ),
  140. };
  141. };
  142. getMentionableUsers() {
  143. return MemberListStore.getAll().map(this.createMentionableUser);
  144. }
  145. getMentionableTeams() {
  146. const {project} = this.props;
  147. const projectData = ProjectsStore.getBySlug(project.slug);
  148. if (!projectData) {
  149. return [];
  150. }
  151. return projectData.teams.map(this.createMentionableTeam);
  152. }
  153. /**
  154. * Get list of teams that are not in the current project, for use in `MultiSelectMenu`
  155. */
  156. getTeamsNotInProject(teamsInProject: Owner[] = []) {
  157. const teams = TeamStore.getAll() || [];
  158. const excludedTeamIds = teamsInProject.map(({actor}) => actor.id);
  159. return teams
  160. .filter(team => !excludedTeamIds.includes(team.id))
  161. .map(this.createUnmentionableTeam);
  162. }
  163. /**
  164. * Closes the select menu by blurring input if possible since that seems to be the only
  165. * way to close it.
  166. */
  167. closeSelectMenu() {
  168. // Close select menu
  169. if (this.selectRef.current) {
  170. // eslint-disable-next-line react/no-find-dom-node
  171. const node = findDOMNode(this.selectRef.current);
  172. const input: HTMLInputElement | null = (node as Element)?.querySelector(
  173. '.Select-input input'
  174. );
  175. if (input) {
  176. // I don't think there's another way to close `react-select`
  177. input.blur();
  178. }
  179. }
  180. }
  181. async handleAddTeamToProject(team) {
  182. const {api, organization, project, value} = this.props;
  183. // Copy old value
  184. const oldValue = [...value];
  185. // Optimistic update
  186. this.props.onChange([...this.props.value, this.createMentionableTeam(team)]);
  187. try {
  188. // Try to add team to project
  189. // Note: we can't close select menu here because we have to wait for ProjectsStore to update first
  190. // The reason for this is because we have little control over `react-select`'s `AsyncSelect`
  191. // We can't control when `handleLoadOptions` gets called, but it gets called when select closes, so
  192. // wait for store to update before closing the menu. Otherwise, we'll have stale items in the select menu
  193. await addTeamToProject(api, organization.slug, project.slug, team);
  194. } catch (err) {
  195. // Unable to add team to project, revert select menu value
  196. this.props.onChange(oldValue);
  197. this.closeSelectMenu();
  198. }
  199. }
  200. handleChange = (newValue: Owner[]) => {
  201. this.props.onChange(newValue);
  202. };
  203. handleInputChange = (inputValue: string) => {
  204. this.setState({inputValue});
  205. if (this.props.onInputChange) {
  206. this.props.onInputChange(inputValue);
  207. }
  208. };
  209. queryMembers = debounce((query, cb) => {
  210. const {api, organization} = this.props;
  211. // Because this function is debounced, the component can potentially be
  212. // unmounted before this fires, in which case, `this.api` is null
  213. if (!api) {
  214. return null;
  215. }
  216. return api
  217. .requestPromise(`/organizations/${organization.slug}/members/`, {
  218. query: {query},
  219. })
  220. .then(
  221. (data: Member[]) => cb(null, data),
  222. err => cb(err)
  223. );
  224. }, 250);
  225. handleLoadOptions = () => {
  226. const usersInProject = this.getMentionableUsers();
  227. const teamsInProject = this.getMentionableTeams();
  228. const teamsNotInProject = this.getTeamsNotInProject(teamsInProject);
  229. const usersInProjectById = usersInProject.map(({actor}) => actor.id);
  230. // Return a promise for `react-select`
  231. return new Promise((resolve, reject) => {
  232. this.queryMembers(this.state.inputValue, (err, result) => {
  233. if (err) {
  234. reject(err);
  235. } else {
  236. resolve(result);
  237. }
  238. });
  239. })
  240. .then(members =>
  241. // Be careful here as we actually want the `users` object, otherwise it means user
  242. // has not registered for sentry yet, but has been invited
  243. members
  244. ? (members as Member[])
  245. .filter(({user}) => user && !usersInProjectById.includes(user.id))
  246. .map(this.createUnmentionableUser)
  247. : []
  248. )
  249. .then(members => {
  250. return [...usersInProject, ...teamsInProject, ...teamsNotInProject, ...members];
  251. });
  252. };
  253. render() {
  254. return (
  255. <SelectControl
  256. multiple
  257. name="owners"
  258. filterOption={(option, filterText) =>
  259. option.data.searchKey.indexOf(filterText) > -1
  260. }
  261. ref={this.selectRef}
  262. loadOptions={this.handleLoadOptions}
  263. defaultOptions
  264. async
  265. clearable
  266. disabled={this.props.disabled}
  267. cache={false}
  268. aria-label={t('Rule owner')}
  269. placeholder={t('Owners')}
  270. components={{
  271. MultiValue: ValueComponent,
  272. }}
  273. onInputChange={this.handleInputChange}
  274. onChange={this.handleChange}
  275. value={this.props.value}
  276. css={{width: 300}}
  277. />
  278. );
  279. }
  280. }
  281. export default withApi(withProjects(SelectOwners));
  282. const Container = styled('div')`
  283. display: flex;
  284. justify-content: space-between;
  285. `;
  286. const DisabledLabel = styled('div')`
  287. opacity: 0.5;
  288. overflow: hidden; /* Needed so that "Add to team" button can fit */
  289. `;
  290. const AddToProjectButton = styled(Button)`
  291. flex-shrink: 0;
  292. `;
  293. const ValueWrapper = styled('a')`
  294. margin-right: ${space(0.5)};
  295. `;