1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { Autocomplete } from 'components/autocomplete/autocomplete';
7 import { connect, DispatchProp } from 'react-redux';
8 import { ServiceRepository } from 'services/services';
9 import { FilterBuilder } from '../../services/api/filter-builder';
10 import { debounce } from 'debounce';
11 import { ListItemText, Typography } from '@mui/material';
12 import { noop } from 'lodash/fp';
13 import { GroupClass, GroupResource } from 'models/group';
14 import { getUserDetailsString, getUserDisplayName, UserResource } from 'models/user';
15 import { Resource, ResourceKind } from 'models/resource';
16 import { ListResults } from 'services/common-service/common-service';
18 export interface Participant {
24 type ParticipantResource = GroupResource | UserResource;
26 interface ParticipantSelectProps {
28 excludedParticipants?: string[];
35 onBlur?: (event: React.FocusEvent<HTMLInputElement>) => void;
36 onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
37 onCreate?: (person: Participant) => void;
38 onDelete?: (index: number) => void;
39 onSelect?: (person: Participant) => void;
42 interface ParticipantSelectState {
44 suggestions: ParticipantResource[];
47 const getDisplayName = (item: GroupResource | UserResource, detailed: boolean) => {
49 case ResourceKind.USER:
50 return getUserDisplayName(item, detailed, detailed);
51 case ResourceKind.GROUP:
52 return item.name + `(${`(${(item as Resource).uuid})`})`;
54 return (item as Resource).uuid;
58 const getDisplayTooltip = (item: GroupResource | UserResource) => {
60 case ResourceKind.USER:
61 return getUserDetailsString(item);
62 case ResourceKind.GROUP:
63 return item.name + `(${`(${(item as Resource).uuid})`})`;
65 return (item as Resource).uuid;
69 export const ParticipantSelect = connect()(
70 class ParticipantSelect extends React.Component<ParticipantSelectProps & DispatchProp, ParticipantSelectState> {
71 state: ParticipantSelectState = {
77 const { label = 'Add people and groups' } = this.props;
82 value={this.state.value}
83 items={this.props.items}
84 suggestions={this.state.suggestions}
85 autofocus={this.props.autofocus}
86 onChange={this.handleChange}
87 onCreate={this.handleCreate}
88 onSelect={this.handleSelect}
89 onDelete={this.props.onDelete && !this.props.disabled ? this.handleDelete : undefined}
90 onFocus={this.props.onFocus}
92 renderChipValue={this.renderChipValue}
93 renderChipTooltip={this.renderChipTooltip}
94 renderSuggestion={this.renderSuggestion}
95 disabled={this.props.disabled} />
100 if (this.props.onBlur) {
101 this.props.onBlur(e);
103 setTimeout(() => this.setState({ value: '', suggestions: [] }), 200);
106 renderChipValue(chipValue: Participant) {
107 const { name, uuid } = chipValue;
111 renderChipTooltip(item: Participant) {
115 renderSuggestion(item: ParticipantResource) {
118 <Typography noWrap>{getDisplayName(item, true)}</Typography>
123 handleDelete = (_: Participant, index: number) => {
124 const { onDelete = noop } = this.props;
128 handleCreate = () => {
129 const { onCreate } = this.props;
131 this.setState({ value: '', suggestions: [] });
135 uuid: this.state.value,
140 handleSelect = (selection: ParticipantResource) => {
141 const { uuid } = selection;
142 const { onSelect = noop } = this.props;
143 this.setState({ value: '', suggestions: [] });
145 name: getDisplayName(selection, false),
146 tooltip: getDisplayTooltip(selection),
151 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
152 this.setState({ value: event.target.value }, this.getSuggestions);
155 getSuggestions = debounce(() => this.props.dispatch<any>(this.requestSuggestions), 500);
157 requestSuggestions = async (_: void, __: void, { userService, groupsService }: ServiceRepository) => {
158 const { value } = this.state;
159 const limit = 5; // FIXME: Does this provide a good UX?
161 const filterUsers = new FilterBuilder()
162 .addILike('any', value)
163 .addEqual('is_active', this.props.onlyActive || undefined)
164 .addNotIn('uuid', this.props.excludedParticipants)
166 const userItems: ListResults<any> = await userService.list({ filters: filterUsers, limit, count: "none" });
168 const filterGroups = new FilterBuilder()
169 .addNotIn('group_class', [GroupClass.PROJECT, GroupClass.FILTER])
170 .addNotIn('uuid', this.props.excludedParticipants)
171 .addILike('name', value)
174 const groupItems: ListResults<any> = await groupsService.list({ filters: filterGroups, limit, count: "none" });
176 suggestions: this.props.onlyPeople
178 : userItems.items.concat(groupItems.items)