Skip to content

Commit

Permalink
fix(web): fix search query hooks (labring#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
newfish-cmyk authored Jun 28, 2023
1 parent e0b920c commit f46a8a0
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions web/src/pages/functionTemplate/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { AddIcon, ChevronDownIcon, Search2Icon } from "@chakra-ui/icons";
Expand Down Expand Up @@ -67,10 +67,12 @@ export default function FunctionTemplate(props: { isModal?: boolean }) {
isModal ? { text: "", value: "" } : { text: t("Template.Recommended"), value: "recommended" },
);
const [queryData, setQueryData] = useState(defaultQueryData);
const [setQueryDataDebounced] = useState(() =>
debounce((value) => {
setQueryData({ ...defaultQueryData, keyword: value });
}, 500),
const setQueryDataDebounced = useMemo(
() =>
debounce((value) => {
setQueryData({ ...queryData, keyword: value });
}, 500),
[queryData],
);
const [sorting, setSorting] = useState(sortList[0]);
const [page, setPage] = useState(1);
Expand Down Expand Up @@ -141,15 +143,14 @@ export default function FunctionTemplate(props: { isModal?: boolean }) {

const handleSideBarClick = (item: any) => {
setSelectedItem(item);
setQueryData(defaultQueryData);
setSorting(sortList[0]);
setSearchKey("");
if (item.value === "my") {
setQueryData(defaultQueryData);
} else if (item.value === "stared") {
if (item.value === "stared") {
setQueryData({ ...defaultQueryData, type: "stared" });
} else if (item.value === "recent") {
setQueryData({ ...defaultQueryData, type: "recentUsed" });
} else {
setQueryData(defaultQueryData);
}
setPage(1);
window.history.replaceState(
Expand Down Expand Up @@ -228,15 +229,15 @@ export default function FunctionTemplate(props: { isModal?: boolean }) {
value={searchKey || ""}
onKeyDown={(e) => {
if (e.key === "Enter") {
setQueryData({ ...defaultQueryData, keyword: searchKey });
setQueryData({ ...queryData, keyword: searchKey });
}
}}
/>
<InputRightElement width={"5.1rem"}>
<Button
className="!h-9"
onClick={() => {
setQueryData({ ...defaultQueryData, keyword: searchKey });
setQueryData({ ...queryData, keyword: searchKey });
}}
>
{t("Search")}
Expand Down

0 comments on commit f46a8a0

Please sign in to comment.