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

feat(search): Add debounce to search #3703

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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 @@ -3,17 +3,20 @@ import Container from "@mui/material/Container";
import Typography from "@mui/material/Typography";
import { useFormik } from "formik";
import { useSearch } from "hooks/useSearch";
import { debounce } from "lodash";
import { useStore } from "pages/FlowEditor/lib/store";
import React, { useEffect } from "react";
import React, { useEffect, useMemo } from "react";
import ChecklistItem from "ui/shared/ChecklistItem";
import Input from "ui/shared/Input";

import { ExternalPortalList } from "./ExternalPortalList";
import { DATA_FACETS } from "./facets";
import { NodeSearchResults } from "./NodeSearchResults";

const DEBOUNCE_MS = 500;

interface SearchNodes {
input: string;
pattern: string;
facets: typeof DATA_FACETS;
}

Expand All @@ -28,9 +31,9 @@ const Search: React.FC = () => {
}, [orderedFlow, setOrderedFlow]);

const formik = useFormik<SearchNodes>({
initialValues: { input: "", facets: DATA_FACETS },
onSubmit: ({ input }) => {
search(input);
initialValues: { pattern: "", facets: DATA_FACETS },
onSubmit: ({ pattern }) => {
debouncedSearch(pattern);
},
});

Expand All @@ -39,24 +42,32 @@ const Search: React.FC = () => {
keys: formik.values.facets,
});

const debouncedSearch = useMemo(
() => debounce((pattern: string) => {
console.debug("Search term: ", pattern)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentionally here whilst I continue to work on this.

return search(pattern);
}, DEBOUNCE_MS),
[search]
);

return (
<Container component={Box} p={3}>
<form onSubmit={formik.handleSubmit}>
<Typography
component={"label"}
htmlFor="search"
htmlFor="pattern"
variant="h3"
mb={1}
display={"block"}
>
Search this flow and internal portals
</Typography>
<Input
id="search"
name="search"
value={formik.values.input}
id="pattern"
name="pattern"
value={formik.values.pattern}
onChange={(e) => {
formik.setFieldValue("input", e.target.value);
formik.setFieldValue("pattern", e.target.value);
formik.handleSubmit();
}}
inputProps={{ spellCheck: false }}
Expand All @@ -66,10 +77,10 @@ const Search: React.FC = () => {
id={"search-data-field-facet"}
checked
inputProps={{ disabled: true }}
onChange={() => {}}
onChange={() => { }}
/>
<Box pt={3}>
{formik.values.input && (
{formik.values.pattern && (
<>
<NodeSearchResults results={results} />
<ExternalPortalList />
Expand Down
Loading