mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-15 17:43:03 +03:00
61bb7b2e02
# Description Please include a summary of the changes and the related issue. Please also include relevant motivation and context. ## Checklist before requesting a review Please delete options that are not relevant. - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented hard-to-understand areas - [ ] I have ideally added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged ## Screenshots (if appropriate):
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { useQuery, useQueryClient } 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 queryClient = useQueryClient();
|
|
const router = useRouter();
|
|
|
|
const fetchBrain = async () => {
|
|
try {
|
|
if (brainId === undefined) {
|
|
return undefined;
|
|
}
|
|
|
|
return await getBrain(brainId);
|
|
} catch (error) {
|
|
router.push("/studio");
|
|
}
|
|
};
|
|
|
|
const { data: brain, isLoading } = useQuery({
|
|
queryKey: brainId ? [getBrainDataKey(brainId)] : [],
|
|
queryFn: fetchBrain,
|
|
enabled: brainId !== undefined,
|
|
});
|
|
|
|
const invalidateBrainQuery = () => {
|
|
void queryClient.invalidateQueries({
|
|
queryKey: brainId ? [getBrainDataKey(brainId)] : [],
|
|
});
|
|
};
|
|
|
|
return {
|
|
brain,
|
|
refetchBrain: invalidateBrainQuery,
|
|
isLoading,
|
|
};
|
|
};
|