quivr/frontend/app/brains-management/library/hooks/useBrainsLibrary.tsx
Mamadou DICKO 2c9a0c1ed2
feat: activate public brain subscription (#1241)
* feat: add public brain details modal

* feat(brain): add subscription route

* feat: activate subscription button

* feat: add last_update column to brain table

* feat: display last update on public brain details page

* feat: change RBAC rule for public brains

* feat: maintain brain last_update time
2023-09-22 11:44:09 +02:00

44 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 = [] } = 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,
};
};