2023-09-21 10:35:53 +03:00
|
|
|
import { useQuery } from "@tanstack/react-query";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
import { PUBLIC_BRAINS_KEY } from "@/lib/api/brain/config";
|
|
|
|
import { useBrainApi } from "@/lib/api/brain/useBrainApi";
|
2023-09-22 12:44:09 +03:00
|
|
|
import { PublicBrain } from "@/lib/context/BrainProvider/types";
|
2023-09-21 10:35:53 +03:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export const useBrainsLibrary = () => {
|
|
|
|
const [searchBarText, setSearchBarText] = useState("");
|
|
|
|
const { getPublicBrains } = useBrainApi();
|
|
|
|
const { data: publicBrains = [] } = useQuery({
|
|
|
|
queryKey: [PUBLIC_BRAINS_KEY],
|
|
|
|
queryFn: getPublicBrains,
|
|
|
|
});
|
|
|
|
|
2023-09-22 12:44:09 +03:00
|
|
|
const [displayingPublicBrains, setDisplayingPublicBrains] = useState<
|
|
|
|
PublicBrain[]
|
|
|
|
>([]);
|
2023-09-21 10:35:53 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setDisplayingPublicBrains(publicBrains);
|
|
|
|
}, [publicBrains]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (searchBarText === "") {
|
|
|
|
setDisplayingPublicBrains(publicBrains);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setDisplayingPublicBrains(
|
|
|
|
publicBrains.filter((brain) =>
|
|
|
|
brain.name.toLowerCase().includes(searchBarText.toLowerCase())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}, [publicBrains, searchBarText]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
displayingPublicBrains,
|
|
|
|
searchBarText,
|
|
|
|
setSearchBarText,
|
|
|
|
};
|
|
|
|
};
|