import { createContext, ReactNode, useContext } from "react"; import { useGetUserOrgPermissions } from "@app/hooks/api"; import { useOrganization } from "../OrganizationContext"; import { TPermission } from "./types"; type Props = { children: ReactNode; }; const PermissionContext = createContext(null); export const OrgPermissionProvider = ({ children }: Props): JSX.Element => { const { currentOrg } = useOrganization(); const orgId = currentOrg?._id || ""; const { data: permission, isLoading } = useGetUserOrgPermissions({ orgId }); if (isLoading) { return (
infisical loading indicator
); } if (!permission) { return (
Failed to load user permissions
); } return {children}; }; export const useOrgPermission = () => { const ctx = useContext(PermissionContext); if (!ctx) { throw new Error("useOrgPermission to be used within "); } return ctx; };