quivr/frontend/app/brains-management/[brainId]/hooks/useBrainsList.ts
Mamadou DICKO 77ea2f2aa6
Brain management 4.2 (#777)
* feat: remove save changes button

* feat: refetch brains list on update

* feat: update forn initialization logic

* feat: prevent viewer to access people tab

* feat: set current brain asbrain management default selected brain
2023-07-26 16:54:03 +02:00

52 lines
1.3 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) {
redirect(`/brains-management/${currentBrainId}`);
}
}, [currentBrainId]);
return {
open,
setOpen,
searchQuery,
setSearchQuery,
brains: sortBrainsByName(brains),
};
};