quivr/frontend/app/studio/[brainId]/BrainManagementTabs/components/SettingsTab/hooks/usePermissionsController.ts

41 lines
1.0 KiB
TypeScript
Raw Normal View History

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,
};
};