import { cloneElement } from "react"; import { Controller, useFieldArray, useFormContext } from "react-hook-form"; import { faChevronDown, faChevronRight, faInfoCircle, faPlus, faTrash } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { twMerge } from "tailwind-merge"; import { Button, Checkbox, Select, SelectItem, Tag, Tooltip } from "@app/components/v2"; import { ProjectPermissionSub } from "@app/context"; import { useToggle } from "@app/hooks"; import { isConditionalSubjects, TFormSchema, TProjectPermissionObject } from "./ProjectRoleModifySection.utils"; type Props = { title: string; subject: T; actions: TProjectPermissionObject[T]["actions"]; children?: JSX.Element; isDisabled?: boolean; }; export const GeneralPermissionPolicies = >({ subject, actions, children, title, isDisabled }: Props) => { const { control } = useFormContext(); const items = useFieldArray({ control, name: `permissions.${subject}` }); const [isOpen, setIsOpen] = useToggle(); if (!items.fields.length) return
; return (
setIsOpen.toggle()} onKeyDown={(e) => { if (e.key === "Enter") { setIsOpen.toggle(); } }} >
{title}
{items.fields.length > 1 && (
{items.fields.length} rules
)}
{isOpen && (
{items.fields.map((el, rootIndex) => (
{isConditionalSubjects(subject) && (
Permission
( )} />

Whether to allow or forbid the selected actions when the following conditions (if any) are met.

Forbid rules must come after allow rules.

} >
)}
Actions
{actions.map(({ label, value }) => { if (typeof value !== "string") return undefined; return ( (
{label}
)} /> ); })}
{children && cloneElement(children, { position: rootIndex })}
{!isDisabled && isConditionalSubjects(subject) && ( )} {!isDisabled && ( )}{" "}
))}
)}
); };