Skip to content

fix: pressist search state inbox #873

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

Merged
merged 3 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions frontend/src/components/inbox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
Expand All @@ -26,7 +26,7 @@ import { AssignedTo } from "./types";

export const Inbox = () => {
const { t } = useTranslate();
const { onSearch, searchPayload } = useSearch<ISubscriber>({
const { onSearch, searchPayload, searchText } = useSearch<ISubscriber>({
$or: ["first_name", "last_name"],
});
const [channels, setChannels] = useState<string[]>([]);
Expand All @@ -48,6 +48,7 @@ export const Inbox = () => {
<Sidebar position="left">
<Grid paddingX={1} paddingTop={1}>
<Search
value={searchText}
onClearClick={() => onSearch("")}
className="changeColor"
onChange={(v) => onSearch(v)}
Expand Down
47 changes: 37 additions & 10 deletions frontend/src/hooks/useSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/

import { debounce } from "@mui/material";
import { ChangeEvent, useState } from "react";
import { useRouter } from "next/router";
import { ChangeEvent, useCallback, useEffect, useState } from "react";

import {
TParamItem,
TBuildParamProps,
TBuildInitialParamProps,
TBuildParamProps,
TParamItem,
} from "@/types/search.types";

const buildOrParams = <T,>({ params, searchText }: TBuildParamProps<T>) => ({
Expand Down Expand Up @@ -52,13 +53,38 @@ const buildNeqInitialParams = <T,>({
);

export const useSearch = <T,>(params: TParamItem<T>) => {
const [searchText, setSearchText] = useState<string>("");
const onSearch = debounce(
(e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement> | string) => {
setSearchText(typeof e === "string" ? e : e.target.value);
},
300,
const router = useRouter();
const [searchText, setSearchText] = useState<string>(
(router.query.search as string) || "",
);

useEffect(() => {
if (router.query.search !== searchText) {
setSearchText((router.query.search as string) || "");
}
}, [router.query.search]);

const updateQueryParams = useCallback(
debounce(async (newSearchText: string) => {
await router.replace(
{
pathname: router.pathname,
query: { ...router.query, search: newSearchText || undefined },
},
undefined,
{ shallow: true },
);
}, 300),
[router],
);
const onSearch = (
e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement> | string,
) => {
const newSearchText = typeof e === "string" ? e : e.target.value;

setSearchText(newSearchText);
updateQueryParams(newSearchText);
};
const {
$eq: eqInitialParams,
$iLike: iLikeParams,
Expand All @@ -67,6 +93,7 @@ export const useSearch = <T,>(params: TParamItem<T>) => {
} = params;

return {
searchText,
onSearch,
searchPayload: {
where: {
Expand Down