selectOwners.tsx 9.2 KB

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