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 "../OrgRoleModifySection.utils"; type Props = { isEditable: boolean; setValue: UseFormSetValue; control: Control; }; enum Permission { NoAccess = "no-access", Custom = "custom" } const PERMISSION_ACTIONS = [{ action: "manage-settings", label: "Manage settings" }] as const; export const OrgPermissionSecretShareRow = ({ isEditable, control, setValue }: Props) => { const [isRowExpanded, setIsRowExpanded] = useToggle(); const [isCustom, setIsCustom] = useToggle(); const rule = useWatch({ control, name: "permissions.secret-share" }); const selectedPermissionCategory = useMemo(() => { if (rule?.["manage-settings"]) { 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.secret-share", { "manage-settings": false }, { shouldDirty: true }); } }; return ( <> setIsRowExpanded.toggle()} > Secret Share {isRowExpanded && (
{PERMISSION_ACTIONS.map(({ action, label }) => { return ( ( { if (!isEditable) { createNotification({ type: "error", text: "Failed to update default role" }); return; } field.onChange(e); }} id={`permissions.secret-share.${action}`} > {label} )} /> ); })}
)} ); };