Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Uptime] Increase debounce and add immediate submit to useQueryBar #99675

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export const isValidKuery = (query: string) => {
export const QueryBar = () => {
const { search: urlValue } = useGetUrlParams();

const { query, setQuery } = useQueryBar();
const { query, setQuery, submitImmediately } = useQueryBar();

const { index_pattern: indexPattern } = useIndexPattern();

const [inputVal, setInputVal] = useState<string>(query.query);
const [inputVal, setInputVal] = useState<string>(query.query as string);

const isInValid = () => {
if (query.language === SyntaxType.text) {
Expand Down Expand Up @@ -66,6 +66,7 @@ export const QueryBar = () => {
}}
onSubmit={(queryN) => {
if (queryN) setQuery({ query: queryN.query as string, language: queryN.language });
submitImmediately();
}}
query={{ ...query, query: inputVal }}
aria-label={i18n.translate('xpack.uptime.filterBar.ariaLabel', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
* 2.0.
*/

import { useCallback, useEffect, useState } from 'react';
import React, { useCallback, useState } from 'react';
import { useDebounce } from 'react-use';
import { useDispatch } from 'react-redux';
import { Query } from 'src/plugins/data/common';
import { useGetUrlParams, useUpdateKueryString, useUrlParams } from '../../../hooks';
import { setEsKueryString } from '../../../state/actions';
import { useIndexPattern } from './use_index_pattern';
Expand All @@ -20,7 +21,26 @@ export enum SyntaxType {
}
const SYNTAX_STORAGE = 'uptime:queryBarSyntax';

export const useQueryBar = () => {
const DEFAULT_QUERY_UPDATE_DEBOUNCE_INTERVAL = 800;

interface UseQueryBarUtils {
// The Query object used by the search bar
query: Query;
// Update the Query object
setQuery: React.Dispatch<React.SetStateAction<Query>>;
/**
* By default the search bar uses a debounce to delay submitting input;
* this function will cancel the debounce and submit immediately.
*/
submitImmediately: () => void;
}

/**
* Provides state management and automatic dispatching of a Query object.
*
* @returns {UseQueryBarUtils}
*/
export const useQueryBar = (): UseQueryBarUtils => {
const dispatch = useDispatch();

const { absoluteDateRangeStart, absoluteDateRangeEnd, ...params } = useGetUrlParams();
Expand All @@ -30,7 +50,7 @@ export const useQueryBar = () => {
services: { storage },
} = useKibana<UptimePluginServices>();

const [query, setQuery] = useState(
const [query, setQuery] = useState<Query>(
queryParam
? {
query: queryParam,
Expand Down Expand Up @@ -59,23 +79,37 @@ export const useQueryBar = () => {
[dispatch]
);

useEffect(() => {
setEsKueryFilters(esFilters ?? '');
}, [esFilters, setEsKueryFilters]);
const setEs = useCallback(() => setEsKueryFilters(esFilters ?? ''), [
esFilters,
setEsKueryFilters,
]);
const [, cancelEsKueryUpdate] = useDebounce(setEs, DEFAULT_QUERY_UPDATE_DEBOUNCE_INTERVAL, [
esFilters,
setEsKueryFilters,
]);

useDebounce(
() => {
if (query.language === SyntaxType.text && queryParam !== query.query) {
updateUrlParams({ query: query.query as string });
}
if (query.language === SyntaxType.kuery) {
updateUrlParams({ query: '' });
}
},
350,
const handleQueryUpdate = useCallback(() => {
if (query.language === SyntaxType.text && queryParam !== query.query) {
updateUrlParams({ query: query.query as string });
}
if (query.language === SyntaxType.kuery) {
updateUrlParams({ query: '' });
}
}, [query.language, query.query, queryParam, updateUrlParams]);

const [, cancelQueryUpdate] = useDebounce(
handleQueryUpdate,
DEFAULT_QUERY_UPDATE_DEBOUNCE_INTERVAL,
[query]
);

const submitImmediately = useCallback(() => {
cancelQueryUpdate();
cancelEsKueryUpdate();
handleQueryUpdate();
setEs();
}, [cancelEsKueryUpdate, cancelQueryUpdate, handleQueryUpdate, setEs]);

useDebounce(
() => {
if (query.language === SyntaxType.kuery && !error && esFilters) {
Expand All @@ -92,5 +126,5 @@ export const useQueryBar = () => {
[esFilters, error]
);

return { query, setQuery };
return { query, setQuery, submitImmediately };
};