2022-08-22 23:51:33 +08:00
|
|
|
import { debounce, DebouncedFuncLeading, isNil } from 'lodash';
|
2022-04-22 21:33:13 +08:00
|
|
|
import React, { Component } from 'react';
|
2018-12-14 19:38:36 +08:00
|
|
|
|
2022-04-22 21:33:13 +08:00
|
|
|
import { SelectableValue } from '@grafana/data';
|
2020-01-21 17:08:07 +08:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2022-04-22 21:33:13 +08:00
|
|
|
import { AsyncSelect } from '@grafana/ui';
|
2021-07-22 15:09:55 +08:00
|
|
|
import { OrgUser } from 'app/types';
|
2018-01-10 20:14:43 +08:00
|
|
|
|
2018-07-12 02:23:07 +08:00
|
|
|
export interface Props {
|
2021-07-22 15:09:55 +08:00
|
|
|
onSelected: (user: SelectableValue<OrgUser['userId']>) => void;
|
2018-01-31 23:44:14 +08:00
|
|
|
className?: string;
|
2021-09-30 22:46:10 +08:00
|
|
|
inputId?: string;
|
2017-12-20 23:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-12 02:23:07 +08:00
|
|
|
export interface State {
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class UserPicker extends Component<Props, State> {
|
2022-08-22 23:51:33 +08:00
|
|
|
debouncedSearch: DebouncedFuncLeading<typeof this.search>;
|
2017-12-20 23:52:43 +08:00
|
|
|
|
2019-05-12 20:15:23 +08:00
|
|
|
constructor(props: Props) {
|
2017-12-20 23:52:43 +08:00
|
|
|
super(props);
|
2018-07-12 02:23:07 +08:00
|
|
|
this.state = { isLoading: false };
|
2018-01-17 23:52:18 +08:00
|
|
|
this.search = this.search.bind(this);
|
2018-01-18 22:49:15 +08:00
|
|
|
|
2018-01-17 23:52:18 +08:00
|
|
|
this.debouncedSearch = debounce(this.search, 300, {
|
2017-12-20 23:52:43 +08:00
|
|
|
leading: true,
|
2018-02-09 17:42:37 +08:00
|
|
|
trailing: true,
|
2017-12-20 23:52:43 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 23:52:18 +08:00
|
|
|
search(query?: string) {
|
2018-07-12 02:23:07 +08:00
|
|
|
this.setState({ isLoading: true });
|
|
|
|
|
|
2021-04-21 15:38:00 +08:00
|
|
|
if (isNil(query)) {
|
2018-12-17 21:31:43 +08:00
|
|
|
query = '';
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
return getBackendSrv()
|
2020-11-20 15:57:19 +08:00
|
|
|
.get(`/api/org/users/lookup?query=${query}&limit=100`)
|
2021-07-22 15:09:55 +08:00
|
|
|
.then((result: OrgUser[]) => {
|
|
|
|
|
return result.map((user) => ({
|
2018-10-08 03:08:22 +08:00
|
|
|
id: user.userId,
|
2018-12-14 19:38:36 +08:00
|
|
|
value: user.userId,
|
2019-08-13 02:03:48 +08:00
|
|
|
label: user.login,
|
2018-12-12 05:17:32 +08:00
|
|
|
imgUrl: user.avatarUrl,
|
2018-10-08 03:08:22 +08:00
|
|
|
login: user.login,
|
|
|
|
|
}));
|
2018-07-12 02:23:07 +08:00
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
this.setState({ isLoading: false });
|
2017-12-20 23:52:43 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2021-09-30 22:46:10 +08:00
|
|
|
const { className, onSelected, inputId } = this.props;
|
2018-07-12 02:23:07 +08:00
|
|
|
const { isLoading } = this.state;
|
|
|
|
|
|
2017-12-20 23:52:43 +08:00
|
|
|
return (
|
2020-09-21 21:33:50 +08:00
|
|
|
<div className="user-picker" data-testid="userPicker">
|
2018-10-08 03:08:22 +08:00
|
|
|
<AsyncSelect
|
2020-10-03 13:19:45 +08:00
|
|
|
isClearable
|
2018-12-14 19:38:36 +08:00
|
|
|
className={className}
|
2021-09-30 22:46:10 +08:00
|
|
|
inputId={inputId}
|
2018-01-17 23:52:18 +08:00
|
|
|
isLoading={isLoading}
|
2018-10-08 03:08:22 +08:00
|
|
|
defaultOptions={true}
|
2018-01-17 23:52:18 +08:00
|
|
|
loadOptions={this.debouncedSearch}
|
2018-10-08 03:08:22 +08:00
|
|
|
onChange={onSelected}
|
2020-11-20 15:57:19 +08:00
|
|
|
placeholder="Start typing to search for user"
|
2020-10-03 13:19:45 +08:00
|
|
|
noOptionsMessage="No users found"
|
2021-10-20 21:54:00 +08:00
|
|
|
aria-label="User picker"
|
2017-12-20 23:52:43 +08:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|