mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-18 11:51:41 +03:00
37 lines
984 B
TypeScript
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),
|
||
|
};
|
||
|
};
|