import { useCallback, useState } from "react"; 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 { twMerge } from "tailwind-merge"; import { createNotification } from "@app/components/notifications"; import { ProjectPermissionCan } from "@app/components/permissions"; import { DeleteActionModal, IconButton, Tooltip } from "@app/components/v2"; import { InfisicalSecretInput } from "@app/components/v2/InfisicalSecretInput"; import { ProjectPermissionActions, ProjectPermissionSub } from "@app/context"; import { useToggle } from "@app/hooks"; import { SecretType } from "@app/hooks/api/types"; type Props = { defaultValue?: string | null; secretName: string; secretId?: string; isOverride?: boolean; isCreatable?: boolean; isVisible?: boolean; isImportedSecret: boolean; environment: string; secretPath: string; onSecretCreate: (env: string, key: string, value: string) => Promise; onSecretUpdate: ( env: string, key: string, value: string, type?: SecretType, secretId?: string ) => Promise; onSecretDelete: (env: string, key: string, secretId?: string) => Promise; }; export const SecretEditRow = ({ defaultValue, isCreatable, isOverride, isImportedSecret, 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 [isModalOpen, setIsModalOpen] = useState(false); const toggleModal = useCallback(() => { setIsModalOpen((prev) => !prev); }, []); 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, isOverride ? SecretType.Personal : SecretType.Shared, secretId ); } } reset({ value }); }; const handleDeleteSecret = useCallback(async () => { setIsDeleting.on(); setIsModalOpen(false); try { await onSecretDelete(environment, secretName, secretId); reset({ value: null }); } finally { setIsDeleting.off(); } }, [onSecretDelete, environment, secretName, secretId, reset, setIsDeleting]); return (
( )} />
{isDirty ? ( <> {(isAllowed) => (
)}
) : ( <>
{(isAllowed) => (
)}
)}
); };