import { Controller, useForm } from "react-hook-form"; import { subject } from "@casl/ability"; import { faCheck, faCopy, faTrash, faXmark } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { useNotificationContext } from "@app/components/context/Notifications/NotificationProvider"; import { ProjectPermissionCan } from "@app/components/permissions"; import { IconButton, SecretInput, Tooltip } from "@app/components/v2"; import { ProjectPermissionActions, ProjectPermissionSub } from "@app/context"; import { useToggle } from "@app/hooks"; type Props = { defaultValue?: string | null; secretName: string; secretId?: string; isCreatable?: boolean; isVisible?: boolean; environment: string; secretPath: string; onSecretCreate: (env: string, key: string, value: string) => Promise; onSecretUpdate: (env: string, key: string, value: string, secretId?: string) => Promise; onSecretDelete: (env: string, key: string, secretId?: string) => Promise; }; export const SecretEditRow = ({ defaultValue, isCreatable, onSecretUpdate, secretName, onSecretCreate, onSecretDelete, environment, secretPath, isVisible, secretId }: Props) => { const { handleSubmit, control, reset, getValues, formState: { isDirty, isSubmitting } } = useForm({ values: { value: defaultValue || null } }); const [isDeleting, setIsDeleting] = useToggle(); const { createNotification } = useNotificationContext(); const handleFormReset = () => { reset(); }; const handleCopySecretToClipboard = async () => { const { value } = getValues(); if (value) { try { await window.navigator.clipboard.writeText(value); createNotification({ type: "success", text: "Copied secret to clipboard" }); } catch (error) { console.log(error); createNotification({ type: "error", text: "Failed to copy secret to clipboard" }); } } }; const handleFormSubmit = async ({ value }: { value?: string | null }) => { if ((value || value === "") && secretName) { if (isCreatable) { await onSecretCreate(environment, secretName, value); } else { await onSecretUpdate(environment, secretName, value, secretId); } } reset({ value }); }; const handleDeleteSecret = async () => { setIsDeleting.on(); try { await onSecretDelete(environment, secretName, secretId); reset({ value: null }); } finally { setIsDeleting.off(); } }; return (
( )} />
{isDirty ? ( <> {(isAllowed) => (
)}
) : ( <>
{(isAllowed) => (
)}
)}
); };