quivr/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/hooks/useBrainFetcher.ts
Mamadou DICKO 1643b54b7b
feat: allow user to unsubscribe from a brain (#1254)
* feat: update translations

* feat: add <DeleteOrUnsubscribeConfirmationModal />

* test(DeleteOrUnsubscribeConfirmationModal): update tests

* feat: redirect to /brains-management on invalid brain id

* refactor: move delete_brain_user to delete_brain_users

* feat: add /POST '/brains/{brain_id}/subscribe'

* feat: handle public brain unsubscription
2023-09-25 14:22:59 +02:00

39 lines
922 B
TypeScript

import { useQuery } from "@tanstack/react-query";
import { UUID } from "crypto";
import { useRouter } from "next/navigation";
import { getBrainDataKey } from "@/lib/api/brain/config";
import { useBrainApi } from "@/lib/api/brain/useBrainApi";
type UseBrainFetcherProps = {
brainId?: UUID;
};
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useBrainFetcher = ({ brainId }: UseBrainFetcherProps) => {
const { getBrain } = useBrainApi();
const router = useRouter();
const fetchBrain = async () => {
try {
if (brainId === undefined) {
return undefined;
}
return await getBrain(brainId);
} catch (error) {
router.push("/brains-management");
}
};
const { data: brain } = useQuery({
queryKey: [getBrainDataKey(brainId!)],
queryFn: fetchBrain,
enabled: brainId !== undefined,
});
return {
brain,
};
};