mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-20 04:42:18 +03:00
41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
|
import { UUID } from "crypto";
|
||
|
import { useEffect } from "react";
|
||
|
import { useFormContext } from "react-hook-form";
|
||
|
|
||
|
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
|
||
|
|
||
|
import { getBrainPermissions } from "../../../utils/getBrainPermissions";
|
||
|
|
||
|
type UsePermissionsControllerProps = {
|
||
|
brainId: UUID;
|
||
|
};
|
||
|
|
||
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||
|
export const usePermissionsController = ({
|
||
|
brainId,
|
||
|
}: UsePermissionsControllerProps) => {
|
||
|
const { allBrains } = useBrainContext();
|
||
|
|
||
|
const { setValue } = useFormContext<{
|
||
|
isApiDefinitionReadOnly: boolean;
|
||
|
isUpdatingApiDefinition: boolean;
|
||
|
}>();
|
||
|
|
||
|
const { hasEditRights, isOwnedByCurrentUser, isPublicBrain } =
|
||
|
getBrainPermissions({
|
||
|
brainId,
|
||
|
userAccessibleBrains: allBrains,
|
||
|
});
|
||
|
|
||
|
useEffect(() => {
|
||
|
setValue("isApiDefinitionReadOnly", !hasEditRights);
|
||
|
setValue("isUpdatingApiDefinition", true);
|
||
|
}, [hasEditRights, setValue]);
|
||
|
|
||
|
return {
|
||
|
hasEditRights,
|
||
|
isOwnedByCurrentUser,
|
||
|
isPublicBrain,
|
||
|
};
|
||
|
};
|