import { useEffect, useState, memo, useMemo } from "react";
import TabContainer from "./TabContainer";
import { useNavigate } from "@remix-run/react";
import { onSearch } from "../../utils/helpers";
import { Search, X } from "lucide-react";
import { useDebounce } from "../../hooks/useDebounce";
import { useSearchAutocomplete } from "../../requestHandler/use-search-hooks";
import { AutocompleteSuggestionTypeEnum } from "../../../src/api/types";
const SearchSection = memo(function SearchSection({
searchValue,
}: {
searchValue: string;
}) {
const tabs = useMemo(() => ["همه نتایج", "محصولات", "فروشگاهها"], []);
const [activeSearchTab, setActiveSearchTab] = useState(0);
return (
{searchValue ? (
) : (
)}
);
});
const SearchSuggestions = memo(function SearchSuggestions({
activeSearchTab,
searchValue,
}: {
activeSearchTab: number;
searchValue: string;
}) {
const navigate = useNavigate();
const debouncedSearchValue = useDebounce(searchValue, 500); // Reduced from 800ms to 500ms
const { data: autocompleteData, isLoading } =
useSearchAutocomplete(debouncedSearchValue);
// Memoize filtered suggestions to avoid recalculation on every render
const filterSuggestions = useMemo(() => {
if (!autocompleteData?.suggestions) return [];
return autocompleteData.suggestions
.filter((sug) => {
return (
activeSearchTab === 0 ||
(sug.type === AutocompleteSuggestionTypeEnum.Product &&
activeSearchTab === 1) ||
(sug.type === AutocompleteSuggestionTypeEnum.Seller &&
activeSearchTab === 2)
);
})
.map((sug) => ({
...sug,
text:
sug.type === AutocompleteSuggestionTypeEnum.Product
? sug.text
: sug.name,
}));
}, [autocompleteData?.suggestions, activeSearchTab]);
if (isLoading) {
return (
);
}
return (
{filterSuggestions.length > 0 ? (
<>
{filterSuggestions.map((sug, index) => {
const matchIndex = sug.text
? sug.text
.toLowerCase()
.indexOf(searchValue ? searchValue.toLowerCase() : "")
: -1;
const beforeMatch =
matchIndex >= 0 ? sug.text?.substring(0, matchIndex) : "";
const matchText =
matchIndex >= 0
? sug.text?.substring(
matchIndex,
matchIndex + searchValue.length
)
: sug.text;
const afterMatch =
matchIndex >= 0
? sug.text?.substring(matchIndex + searchValue.length)
: "";
return (
{}}
className="flex w-full h-[44px] justify-between items-center cursor-pointer hover:bg-WHITE2"
>
{sug.type === AutocompleteSuggestionTypeEnum.Product ? (
{
if (sug.text) {
onSearch(sug.text, navigate);
}
}}
onKeyDown={() => {}}
role="button"
tabIndex={index}
className="flex w-full items-center gap-2"
>
{beforeMatch}
{matchText}
{afterMatch}
) : (
{
if (sug.id) {
navigate(`/seller/${sug.id}`);
}
}}
className="flex w-full items-center gap-2"
onKeyDown={() => {}}
role="button"
tabIndex={index}
>
{beforeMatch}
{matchText}
{afterMatch}
)}
);
})}
>
) : (
// Empty state
{
onSearch(searchValue, navigate);
}}
onKeyDown={() => {}}
role="button"
tabIndex={0}
className="flex w-full items-center gap-2"
>
{searchValue}
)}
);
});
const SearchHistorySection = () => {
const navigate = useNavigate();
const [searchHistory, setSearchHistory] = useState([]);
useEffect(() => {
const history = localStorage.getItem("searchValues");
if (history) {
setSearchHistory(history.split("&&"));
}
}, []);
const deleteSearchHistory = (searchValue: string) => {
localStorage.setItem(
"searchValues",
searchHistory.filter((value) => value !== searchValue).join("&&") || ""
);
setSearchHistory(searchHistory.filter((value) => value !== searchValue));
};
const deleteAllSearchHistory = () => {
localStorage.removeItem("searchValues");
setSearchHistory([]);
};
return (
{searchHistory.length > 0 ? (
<>
تاریخچه جستجو
{searchHistory.length > 0 && (
)}
{searchHistory.map((searchValue, index) => {
return (
{}}
className="flex w-full h-[44px] justify-between items-center"
>
{
onSearch(searchValue, navigate);
}}
onKeyDown={() => {}}
role="button"
tabIndex={index}
className="flex w-full items-center gap-2"
>
{searchValue}
);
})}
>
) : // Empty state
null}
);
};
export default SearchSection;