quivr/frontend/app/brains-management/[brainId]/hooks/useBrainsList.ts
Mamadou DICKO cf376fb59f
Brain management 1 (#752)
* feat: add brain management button

* feat: add brains list

* feat: add brain search bar

* feat: sort brain list by name

* refactor: update brains management page structure

* feat(BrainManagement): add new brain button

* feat: update import links
2023-07-24 14:17:21 +02:00

37 lines
984 B
TypeScript

import { 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 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);
});
return {
open,
setOpen,
searchQuery,
setSearchQuery,
brains: sortBrainsByName(brains),
};
};