mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-18 08:02:03 +03:00
77ea2f2aa6
* 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
52 lines
1.3 KiB
TypeScript
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),
|
|
};
|
|
};
|