quivr/frontend/app/brains-management/[brainId]/hooks/useBrainsList.ts
Mamadou DICKO 8172704b61
feat: add public brain page (#1230)
* feat: add brain library button

* feat(Field): add inputClassName key

* feat: add GET brains/public route

* feat: add brains library layout

* feat: add brain subscriber count
2023-09-21 09:35:53 +02:00

56 lines
1.4 KiB
TypeScript

import { UUID } from "crypto";
import { redirect, useParams, usePathname } from "next/navigation";
import { useEffect, useState } from "react";
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
import { useDevice } from "@/lib/hooks/useDevice";
import { sortBrainsByName } from "../utils/sortByName";
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useBrainsList = () => {
const { isMobile } = useDevice();
const [open, setOpen] = useState(!isMobile);
const [searchQuery, setSearchQuery] = useState("");
const { allBrains } = useBrainContext();
const { currentBrainId } = useBrainContext();
const params = useParams();
const brainId = params?.brainId as UUID | undefined;
const pathname = usePathname();
useEffect(() => {
setOpen(!isMobile);
}, [isMobile, pathname]);
const brains = allBrains.filter((brain) => {
const query = searchQuery.toLowerCase();
const name = brain.name.toLowerCase();
return name.includes(query);
});
useEffect(() => {
if (brainId !== undefined) {
return;
}
if (
currentBrainId !== null &&
pathname !== null &&
!pathname.includes("library")
) {
redirect(`/brains-management/${currentBrainId}`);
}
}, [brainId, currentBrainId, pathname]);
return {
open,
setOpen,
searchQuery,
setSearchQuery,
brains: sortBrainsByName(brains),
};
};