1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { Autocomplete, AutocompleteCat } 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[];
34 category?: AutocompleteCat;
36 onBlur?: (event: React.FocusEvent<HTMLInputElement>) => void;
37 onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;
38 onCreate?: (person: Participant) => void;
39 onDelete?: (index: number) => void;
40 onSelect?: (person: Participant) => void;
43 interface ParticipantSelectState {
45 suggestions: ParticipantResource[];
48 const getDisplayName = (item: GroupResource | UserResource, detailed: boolean) => {
50 case ResourceKind.USER:
51 return getUserDisplayName(item, detailed, detailed);
52 case ResourceKind.GROUP:
53 return item.name + `(${`(${(item as Resource).uuid})`})`;
55 return (item as Resource).uuid;
59 const getDisplayTooltip = (item: GroupResource | UserResource) => {
61 case ResourceKind.USER:
62 return getUserDetailsString(item);
63 case ResourceKind.GROUP:
64 return item.name + `(${`(${(item as Resource).uuid})`})`;
66 return (item as Resource).uuid;
70 export const ParticipantSelect = connect()(
71 class ParticipantSelect extends React.Component<ParticipantSelectProps & DispatchProp, ParticipantSelectState> {
72 state: ParticipantSelectState = {
78 const { label = 'Add people and groups' } = this.props;
83 value={this.state.value}
84 items={this.props.items}
85 suggestions={this.state.suggestions}
86 autofocus={this.props.autofocus}
87 onChange={this.handleChange}
88 onCreate={this.handleCreate}
89 onSelect={this.handleSelect}
90 onDelete={this.props.onDelete && !this.props.disabled ? this.handleDelete : undefined}
91 onFocus={this.props.onFocus || this.onFocus}
93 renderChipValue={this.renderChipValue}
94 renderChipTooltip={this.renderChipTooltip}
95 renderSuggestion={this.renderSuggestion}
96 category={this.props.category}
97 disabled={this.props.disabled} />
102 this.getSuggestions();
106 if (this.props.onBlur) {
107 this.props.onBlur(e);
109 setTimeout(() => this.setState({ value: '', suggestions: [] }), 200);
112 renderChipValue(chipValue: Participant) {
113 const { name, uuid } = chipValue;
117 renderChipTooltip(item: Participant) {
121 renderSuggestion(item: ParticipantResource) {
124 <Typography noWrap>{getDisplayName(item, true)}</Typography>
129 handleDelete = (_: Participant, index: number) => {
130 const { onDelete = noop } = this.props;
134 handleCreate = () => {
135 const { onCreate } = this.props;
137 this.setState({ value: '', suggestions: [] });
141 uuid: this.state.value,
146 handleSelect = (selection: ParticipantResource) => {
147 if (!selection) return;
148 const { uuid } = selection;
149 const { onSelect = noop } = this.props;
150 this.setState({ value: '', suggestions: [] });
152 name: getDisplayName(selection, false),
153 tooltip: getDisplayTooltip(selection),
158 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
159 this.setState({ value: event.target.value }, this.getSuggestions);
162 getSuggestions = debounce(() => this.props.dispatch<any>(this.requestSuggestions), 500);
164 requestSuggestions = async (_: void, __: void, { userService, groupsService }: ServiceRepository) => {
165 const { value } = this.state;
166 const limit = 10; // FIXME: Does this provide a good UX?
168 const filterUsers = new FilterBuilder()
169 .addILike('any', value)
170 .addEqual('is_active', this.props.onlyActive || undefined)
171 .addNotIn('uuid', this.props.excludedParticipants)
173 const userItems: ListResults<any> = await userService.list({ filters: filterUsers, limit, count: "none" });
175 const filterGroups = new FilterBuilder()
176 .addNotIn('group_class', [GroupClass.PROJECT, GroupClass.FILTER])
177 .addNotIn('uuid', this.props.excludedParticipants)
178 .addILike('name', value)
181 const groupItems: ListResults<any> = await groupsService.list({ filters: filterGroups, limit, count: "none" });
183 suggestions: this.props.onlyPeople
185 : userItems.items.concat(groupItems.items)