import { useEffect, useMemo } from "react"; import { Control, Controller, UseFormSetValue, useWatch } from "react-hook-form"; import { faChevronDown, faChevronRight } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { createNotification } from "@app/components/notifications"; import { Checkbox, Select, SelectItem, Td, Tr } from "@app/components/v2"; import { useToggle } from "@app/hooks"; import { TFormSchema } from "@app/views/Org/RolePage/components/OrgRoleModifySection.utils"; type Props = { isEditable: boolean; setValue: UseFormSetValue; control: Control; }; enum Permission { NoAccess = "no-access", Custom = "custom" } const PERMISSION_ACTIONS = [ { action: "access-all-projects", label: "Access all organization projects" } ] as const; export const OrgPermissionAdminConsoleRow = ({ isEditable, control, setValue }: Props) => { const [isRowExpanded, setIsRowExpanded] = useToggle(); const [isCustom, setIsCustom] = useToggle(); const rule = useWatch({ control, name: "permissions.organization-admin-console" }); const selectedPermissionCategory = useMemo(() => { if (rule?.["access-all-projects"]) { return Permission.Custom; } return Permission.NoAccess; }, [rule, isCustom]); useEffect(() => { if (selectedPermissionCategory === Permission.Custom) setIsCustom.on(); else setIsCustom.off(); }, [selectedPermissionCategory]); useEffect(() => { const isRowCustom = selectedPermissionCategory === Permission.Custom; if (isRowCustom) { setIsRowExpanded.on(); } }, []); const handlePermissionChange = (val: Permission) => { if (!val) return; if (val === Permission.Custom) { setIsRowExpanded.on(); setIsCustom.on(); return; } setIsCustom.off(); if (val === Permission.NoAccess) { setValue( "permissions.organization-admin-console", { "access-all-projects": false }, { shouldDirty: true } ); } }; return ( <> setIsRowExpanded.toggle()} > Organization Admin Console {isRowExpanded && (
{PERMISSION_ACTIONS.map(({ action, label }) => { return ( ( { if (!isEditable) { createNotification({ type: "error", text: "Failed to update default role" }); return; } field.onChange(e); }} id={`permissions.organization-admin-console.${action}`} > {label} )} /> ); })}
)} ); };