mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-17 03:19:59 +03:00
e885574d1e
Issue: https://github.com/StanGirard/quivr/issues/1889 Demo: https://github.com/StanGirard/quivr/assets/63923024/f9853b7e-e358-4c98-9281-4b0752a29632
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
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";
|
|
import { PublicBrain } from "@/lib/context/BrainProvider/types";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export const useBrainsLibrary = () => {
|
|
const [searchBarText, setSearchBarText] = useState("");
|
|
const { getPublicBrains } = useBrainApi();
|
|
const { data: publicBrains = [], isLoading } = useQuery({
|
|
queryKey: [PUBLIC_BRAINS_KEY],
|
|
queryFn: getPublicBrains,
|
|
});
|
|
|
|
const [displayingPublicBrains, setDisplayingPublicBrains] = useState<
|
|
PublicBrain[]
|
|
>([]);
|
|
|
|
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,
|
|
isLoading,
|
|
};
|
|
};
|