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 '@material-ui/core';
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 = 'Share' } = 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}
91 onBlur={this.props.onBlur}
92 renderChipValue={this.renderChipValue}
93 renderChipTooltip={this.renderChipTooltip}
94 renderSuggestion={this.renderSuggestion}
95 disabled={this.props.disabled}/>
99 renderChipValue(chipValue: Participant) {
100 const { name, uuid } = chipValue;
104 renderChipTooltip(item: Participant) {
108 renderSuggestion(item: ParticipantResource) {
111 <Typography noWrap>{getDisplayName(item, true)}</Typography>
116 handleDelete = (_: Participant, index: number) => {
117 const { onDelete = noop } = this.props;
121 handleCreate = () => {
122 const { onCreate } = this.props;
124 this.setState({ value: '', suggestions: [] });
128 uuid: this.state.value,
133 handleSelect = (selection: ParticipantResource) => {
134 const { uuid } = selection;
135 const { onSelect = noop } = this.props;
136 this.setState({ value: '', suggestions: [] });
138 name: getDisplayName(selection, false),
139 tooltip: getDisplayTooltip(selection),
144 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
145 this.setState({ value: event.target.value }, this.getSuggestions);
148 getSuggestions = debounce(() => this.props.dispatch<any>(this.requestSuggestions), 500);
150 requestSuggestions = async (_: void, __: void, { userService, groupsService }: ServiceRepository) => {
151 const { value } = this.state;
152 const limit = 5; // FIXME: Does this provide a good UX?
154 const filterUsers = new FilterBuilder()
155 .addILike('any', value)
156 .addEqual('is_active', this.props.onlyActive || undefined)
157 .addNotIn('uuid', this.props.excludedParticipants)
159 const userItems: ListResults<any> = await userService.list({ filters: filterUsers, limit, count: "none" });
161 const filterGroups = new FilterBuilder()
162 .addNotIn('group_class', [GroupClass.PROJECT, GroupClass.FILTER])
163 .addNotIn('uuid', this.props.excludedParticipants)
164 .addILike('name', value)
167 const groupItems: ListResults<any> = await groupsService.list({ filters: filterGroups, limit, count: "none" });
169 suggestions: this.props.onlyPeople
171 : userItems.items.concat(groupItems.items)