|
1 |
| -import React, { useCallback, useEffect, useMemo, useReducer, useState } from 'react'; |
| 1 | +import React, { useCallback, useMemo, useReducer, useState } from 'react'; |
2 | 2 |
|
3 | 3 | import { SelectableValue } from '@grafana/data';
|
4 | 4 | import { AsyncMultiSelect, AsyncSelect } from '@grafana/ui';
|
5 | 5 | import { inject, observer } from 'mobx-react';
|
6 | 6 |
|
7 | 7 | import { makeRequest, isNetworkError } from 'network';
|
8 | 8 | import { UserAction, generateMissingPermissionMessage } from 'utils/authorization';
|
| 9 | +import { useDebouncedCallback } from 'utils/hooks'; |
9 | 10 |
|
10 | 11 | interface RemoteSelectProps {
|
11 | 12 | autoFocus?: boolean;
|
@@ -67,24 +68,20 @@ const RemoteSelect = inject('store')(
|
67 | 68 |
|
68 | 69 | const [options, setOptions] = useReducer(mergeOptions, []);
|
69 | 70 |
|
70 |
| - const loadOptionsCallback = useCallback(async (query?: string): Promise<SelectableValue[]> => { |
| 71 | + const loadOptionsCallback = useDebouncedCallback(async (query: string, cb) => { |
71 | 72 | try {
|
72 | 73 | const data = await makeRequest(href, { params: { search: query } });
|
73 | 74 | const options = getOptions(data.results || data);
|
74 | 75 | setOptions(options);
|
75 | 76 |
|
76 |
| - return options; |
| 77 | + cb(options); |
77 | 78 | } catch (e) {
|
78 | 79 | if (isNetworkError(e) && e.response.status === 403 && requiredUserAction) {
|
79 | 80 | setNoOptionsMessage(generateMissingPermissionMessage(requiredUserAction));
|
80 | 81 | }
|
81 |
| - return []; |
| 82 | + cb([]); |
82 | 83 | }
|
83 |
| - }, []); |
84 |
| - |
85 |
| - useEffect(() => { |
86 |
| - loadOptionsCallback(); |
87 |
| - }, []); |
| 84 | + }, 250); |
88 | 85 |
|
89 | 86 | const onChangeCallback = useCallback(
|
90 | 87 | (option) => {
|
@@ -127,7 +124,7 @@ const RemoteSelect = inject('store')(
|
127 | 124 | isSearchable={showSearch}
|
128 | 125 | value={value}
|
129 | 126 | onChange={onChangeCallback}
|
130 |
| - defaultOptions={options} |
| 127 | + defaultOptions |
131 | 128 | loadOptions={loadOptionsCallback}
|
132 | 129 | getOptionLabel={getOptionLabel}
|
133 | 130 | noOptionsMessage={noOptionsMessage}
|
|
0 commit comments