WIP: value suggestion is added

This commit is contained in:
Palash gupta 2022-03-29 00:02:56 +05:30
parent 225a345baa
commit d4d1104a53
No known key found for this signature in database
GPG Key ID: 8FD05AE6F9150AD6
5 changed files with 123 additions and 14 deletions

View File

@ -0,0 +1,63 @@
import { Select, Spin } from 'antd';
import { SelectProps } from 'antd/es/select';
import debounce from 'lodash-es/debounce';
import React, { useRef, useState } from 'react';
export interface DebounceSelectProps<ValueType = any>
extends Omit<SelectProps<ValueType>, 'options' | 'children'> {
fetchOptions: (search: string) => Promise<ValueType[]>;
debounceTimeout: number;
}
function DebounceSelect<
ValueType extends {
key?: string;
label: React.ReactNode;
value: string | number;
} = never
>({
fetchOptions,
debounceTimeout = 800,
...props
}: DebounceSelectProps): JSX.Element {
const [fetching, setFetching] = useState(false);
const [options, setOptions] = useState<ValueType[]>([]);
const fetchRef = useRef(0);
const debounceFetcher = React.useMemo(() => {
const loadOptions = (value: string): void => {
fetchRef.current += 1;
const fetchId = fetchRef.current;
setOptions([]);
setFetching(true);
fetchOptions(value).then((newOptions) => {
if (fetchId !== fetchRef.current) {
// for fetch callback order
return;
}
setOptions(newOptions);
setFetching(false);
});
};
return debounce(loadOptions, debounceTimeout);
}, [fetchOptions, debounceTimeout]);
return (
<Select<ValueType>
labelInValue
filterOption={false}
onSearch={debounceFetcher}
notFoundContent={fetching ? <Spin size="small" /> : null}
style={{ minWidth: '170px' }}
// as all other props are from SelectProps only
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
options={options}
/>
);
}
export default DebounceSelect;

View File

@ -0,0 +1,32 @@
import getTagValue from 'api/trace/getTagValue';
// Usage of DebounceSelect
export interface TagValue {
label: string;
value: string;
}
export async function fetchTag(
globalStart: number,
globalEnd: number,
tagKey: string,
): Promise<TagValue[]> {
const response = await getTagValue({
end: globalEnd,
start: globalStart,
tagKey,
});
if (response.statusCode !== 200 || !response.payload) {
return [];
}
console.log(response.payload);
return [
{
label: 'asd',
value: 'asd',
},
];
}

View File

@ -3,14 +3,12 @@ import { Select } from 'antd';
import React from 'react';
import { useSelector } from 'react-redux';
import { AppState } from 'store/reducers';
import { GlobalReducer } from 'types/reducer/globalTime';
import { TraceReducer } from 'types/reducer/trace';
import {
Container,
IconContainer,
SelectComponent,
ValueSelect,
} from './styles';
import { fetchTag, TagValue } from './config';
import DebounceSelect from './DebounceSelect';
import { Container, IconContainer, SelectComponent } from './styles';
import TagsKey from './TagKey';
const { Option } = Select;
@ -35,6 +33,9 @@ const AllMenu: AllMenuProps[] = [
function SingleTags(props: AllTagsProps): JSX.Element {
const traces = useSelector<AppState, TraceReducer>((state) => state.traces);
const globalReducer = useSelector<AppState, GlobalReducer>(
(state) => state.globalTime,
);
const { tag, onCloseHandler, setLocalSelectedTags, index } = props;
const {
@ -80,7 +81,15 @@ function SingleTags(props: AllTagsProps): JSX.Element {
))}
</SelectComponent>
<ValueSelect
<DebounceSelect
fetchOptions={(): Promise<TagValue[]> =>
fetchTag(globalReducer.minTime, globalReducer.maxTime, selectedKey[0])
}
debounceTimeout={300}
mode="tags"
/>
{/* <ValueSelect
value={selectedValues}
onChange={(value): void => {
setLocalSelectedTags((tags) => [
@ -94,7 +103,7 @@ function SingleTags(props: AllTagsProps): JSX.Element {
]);
}}
mode="tags"
/>
/> */}
<IconContainer role="button" onClick={(): void => onDeleteTagHandler(index)}>
<CloseOutlined />

View File

@ -15,12 +15,6 @@ export const SelectComponent = styled(Select)`
}
`;
export const ValueSelect = styled(Select)`
&&& {
width: 100%;
}
`;
export const Container = styled.div`
&&& {
display: flex;

View File

@ -0,0 +1,11 @@
import { GlobalReducer } from 'types/reducer/globalTime';
export interface Props {
start: GlobalReducer['minTime'];
end: GlobalReducer['maxTime'];
tagKey: string;
}
export interface PayloadProps {
key: string;
}